libmodbus4r 0.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/modbus4r.h DELETED
@@ -1,26 +0,0 @@
1
- /* libmodbus4r - binding use libmodbus for Ruby.
2
- Copyright (C) 2009 Timin Aleksey
3
-
4
- This program is free software: you can redistribute it and/or modify
5
- it under the terms of the GNU General Public License as published by
6
- the Free Software Foundation, either version 3 of the License, or
7
- (at your option) any later version.
8
-
9
- This program is distributed in the hope that it will be useful,
10
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- GNU General Public License for more details. */
13
-
14
- #ifndef MODBUS_H
15
- #define MODBUS_H
16
-
17
- #include "modbus.h"
18
- #ifdef RUBY_1_8
19
- #include <ruby.h>
20
- #include <intern.h>
21
- #else
22
- #include "ruby/ruby.h"
23
- #include "ruby/intern.h"
24
- #endif
25
-
26
- #endif
data/ext/rtu_master.c DELETED
@@ -1,36 +0,0 @@
1
- /* libmodbus4r - binding use libmodbus for Ruby.
2
- Copyright (C) 2009 Timin Aleksey
3
-
4
- This program is free software: you can redistribute it and/or modify
5
- it under the terms of the GNU General Public License as published by
6
- the Free Software Foundation, either version 3 of the License, or
7
- (at your option) any later version.
8
-
9
- This program is distributed in the hope that it will be useful,
10
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- GNU General Public License for more details. */
13
-
14
- #include "modbus4r.h"
15
- #include "master.h"
16
-
17
- VALUE mb_rtu_mstr_new(VALUE self, VALUE device, VALUE baud,
18
- VALUE parity, VALUE data_bit,
19
- VALUE stop_bit, VALUE slave)
20
- {
21
- modbus_param_t *mb_param;
22
-
23
- mb_param = malloc(sizeof(modbus_param_t));
24
- device = rb_funcall(device, rb_intern("to_s"), 0);
25
- baud = rb_funcall(baud, rb_intern("to_i"), 0);
26
- parity = rb_funcall(parity, rb_intern("to_s"), 0);
27
- data_bit = rb_funcall(data_bit, rb_intern("to_i"), 0);
28
- stop_bit = rb_funcall(stop_bit, rb_intern("to_i"), 0);
29
- slave = rb_funcall(slave, rb_intern("to_i"), 0);
30
-
31
- modbus_init_rtu(mb_param, RSTRING_PTR(device), FIX2INT(baud),
32
- RSTRING_PTR(parity), FIX2INT(data_bit),
33
- FIX2INT(stop_bit), FIX2INT(slave));
34
-
35
- return Data_Wrap_Struct(self, 0, mb_mstr_free, mb_param);
36
- }
data/ext/rtu_master.h DELETED
@@ -1,21 +0,0 @@
1
- /* libmodbus4r - binding use libmodbus for Ruby.
2
- Copyright (C) 2009 Timin Aleksey
3
-
4
- This program is free software: you can redistribute it and/or modify
5
- it under the terms of the GNU General Public License as published by
6
- the Free Software Foundation, either version 3 of the License, or
7
- (at your option) any later version.
8
-
9
- This program is distributed in the hope that it will be useful,
10
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- GNU General Public License for more details. */
13
-
14
- #ifndef RTU_MASTER_H
15
- #define RTU_MASTER_H
16
-
17
- extern VALUE mb_rtu_mstr_new(VALUE self, VALUE device, VALUE baud,
18
- VALUE parity, VALUE data_bit,
19
- VALUE stop_bit, VALUE slave);
20
-
21
- #endif
data/ext/rtu_slave.c DELETED
@@ -1,131 +0,0 @@
1
- /* libmodbus4r - binding use libmodbus for Ruby.
2
- Copyright (C) 2009 Timin Aleksey
3
-
4
- This program is free software: you can redistribute it and/or modify
5
- it under the terms of the GNU General Public License as published by
6
- the Free Software Foundation, either version 3 of the License, or
7
- (at your option) any later version.
8
-
9
- This program is distributed in the hope that it will be useful,
10
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- GNU General Public License for more details. */
13
-
14
- #include <pthread.h>
15
- #include <unistd.h>
16
- #include "modbus4r.h"
17
- #include "slave.h"
18
- #include "errors.h"
19
-
20
- void mb_rtu_sl_free(modbus_slave_t *mb_slave)
21
- {
22
- pthread_cancel(mb_slave->tid);
23
-
24
- modbus_close(mb_slave->mb_param);
25
-
26
- modbus_mapping_free(mb_slave->mb_map);
27
- #ifndef RUBY_1_8
28
- rb_ary_free(mb_slave->coil_status);
29
- rb_ary_free(mb_slave->input_status);
30
- rb_ary_free(mb_slave->holding_registers);
31
- rb_ary_free(mb_slave->input_registers);
32
- #else
33
- free(RARRAY_PTR(mb_slave->coil_status));
34
- free(RARRAY_PTR(mb_slave->input_status));
35
- free(RARRAY_PTR(mb_slave->holding_registers));
36
- free(RARRAY_PTR(mb_slave->input_registers));
37
- #endif
38
-
39
- free(mb_slave);
40
- }
41
-
42
- void *mb_rtu_serv(void *arg)
43
- {
44
- modbus_slave_t *mb_slave = (modbus_slave_t *)arg;
45
-
46
- uint8_t query[MAX_MESSAGE_LENGTH];
47
- int query_size;
48
- while (1) {
49
- int ret = modbus_slave_receive(mb_slave->mb_param, -1,
50
- query, &query_size);
51
-
52
- if (ret == 0) {
53
- mb_push_coil_status(mb_slave);
54
- mb_push_input_status(mb_slave);
55
- mb_push_holding_registers(mb_slave);
56
- mb_push_input_registers(mb_slave);
57
-
58
- modbus_slave_manage(mb_slave->mb_param, query,
59
- query_size, mb_slave->mb_map);
60
-
61
- mb_pull_coil_status(mb_slave);
62
- mb_pull_holding_registers(mb_slave);
63
- } else if (ret == CONNECTION_CLOSED) {
64
- break;
65
- } else {
66
- continue;
67
- }
68
- }
69
- }
70
-
71
- VALUE mb_rtu_sl_new(VALUE self, VALUE device, VALUE baud,
72
- VALUE parity, VALUE data_bit,
73
- VALUE stop_bit, VALUE slave)
74
- {
75
- modbus_slave_t *mb_slave;
76
-
77
- mb_slave = malloc(sizeof(modbus_slave_t));
78
- mb_slave->mb_param = malloc(sizeof(modbus_param_t));
79
- mb_slave->mb_map = malloc(sizeof(modbus_mapping_t));
80
-
81
- device = rb_funcall(device, rb_intern("to_s"), 0);
82
- baud = rb_funcall(baud, rb_intern("to_i"), 0);
83
- parity = rb_funcall(parity, rb_intern("to_s"), 0);
84
- data_bit = rb_funcall(data_bit, rb_intern("to_i"), 0);
85
- stop_bit = rb_funcall(stop_bit, rb_intern("to_i"), 0);
86
- slave = rb_funcall(slave, rb_intern("to_i"), 0);
87
-
88
- modbus_init_rtu(mb_slave->mb_param, RSTRING_PTR(device), FIX2INT(baud),
89
- RSTRING_PTR(parity), FIX2INT(data_bit),
90
- FIX2INT(stop_bit), FIX2INT(slave));
91
- int ret = modbus_mapping_new(mb_slave->mb_map, 0, 0, 0, 0);
92
- if (ret < 0) {
93
- rb_raise(rb_eStandardError, "Memory allocation failed");
94
- }
95
-
96
- mb_slave->coil_status = rb_ary_new();
97
- mb_slave->input_status = rb_ary_new();
98
- mb_slave->holding_registers = rb_ary_new();
99
- mb_slave->input_registers = rb_ary_new();
100
-
101
- return Data_Wrap_Struct(self, 0, mb_rtu_sl_free, mb_slave);
102
- }
103
-
104
- VALUE mb_rtu_sl_start(VALUE self)
105
- {
106
- modbus_slave_t *mb_slave;
107
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
108
-
109
- int ret = pthread_create(&mb_slave->tid, NULL, mb_rtu_serv,
110
- (void *)mb_slave);
111
- if (ret != 0) {
112
- rb_raise(rb_eStandardError, "Slave has not started");
113
- }
114
-
115
- return self;
116
- }
117
-
118
- VALUE mb_rtu_sl_stop(VALUE self)
119
- {
120
- modbus_slave_t *mb_slave;
121
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
122
-
123
- int ret = pthread_cancel(mb_slave->tid);
124
- if (ret != 0) {
125
- rb_raise(rb_eStandardError, "Slave has not stoped");
126
- }
127
-
128
- close(mb_slave->listen_sock);
129
-
130
- return self;
131
- }
data/ext/rtu_slave.h DELETED
@@ -1,23 +0,0 @@
1
- /* libmodbus4r - binding use libmodbus for Ruby.
2
- Copyright (C) 2009 Timin Aleksey
3
-
4
- This program is free software: you can redistribute it and/or modify
5
- it under the terms of the GNU General Public License as published by
6
- the Free Software Foundation, either version 3 of the License, or
7
- (at your option) any later version.
8
-
9
- This program is distributed in the hope that it will be useful,
10
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- GNU General Public License for more details. */
13
-
14
- #ifndef RTU_SLAVE_H
15
- #define RTU_SLAVE_H
16
-
17
- extern VALUE mb_rtu_sl_new(VALUE self, VALUE device, VALUE baud,
18
- VALUE parity, VALUE data_bit,
19
- VALUE stop_bit, VALUE slave);
20
- extern VALUE mb_rtu_sl_start(VALUE self);
21
- extern VALUE mb_rtu_sl_stop(VALUE self);
22
-
23
- #endif
data/ext/slave.c DELETED
@@ -1,215 +0,0 @@
1
- /* libmodbus4r - binding use libmodbus for Ruby.
2
- Copyright (C) 2009 Timin Aleksey
3
-
4
- This program is free software: you can redistribute it and/or modify
5
- it under the terms of the GNU General Public License as published by
6
- the Free Software Foundation, either version 3 of the License, or
7
- (at your option) any later version.
8
-
9
- This program is distributed in the hope that it will be useful,
10
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- GNU General Public License for more details. */
13
-
14
- #include <unistd.h>
15
- #include <pthread.h>
16
- #include "modbus4r.h"
17
- #include "slave.h"
18
-
19
- void mb_push_coil_status(modbus_slave_t *mb_slave)
20
- {
21
- int len = RARRAY_LEN(mb_slave->coil_status);
22
- VALUE *ptr_coil = RARRAY_PTR(mb_slave->coil_status);
23
- mb_slave->mb_map->nb_coil_status = len;
24
- mb_slave->mb_map->tab_coil_status = malloc(sizeof(uint8_t) * len);
25
- uint8_t *ptr_map = mb_slave->mb_map->tab_coil_status;
26
-
27
- int i;
28
- for (i = 0; i < len; i++) {
29
- *ptr_map = (*ptr_coil == Qtrue ? 1 : 0);
30
- ptr_coil++;
31
- ptr_map++;
32
- }
33
- }
34
-
35
- void mb_pull_coil_status(modbus_slave_t *mb_slave)
36
- {
37
- uint8_t *ptr_map = mb_slave->mb_map->tab_coil_status;
38
- VALUE *ptr_coil = RARRAY_PTR(mb_slave->coil_status);
39
- int i;
40
- for(i = 0; i < mb_slave->mb_map->nb_coil_status; i++) {
41
- *ptr_coil = (*ptr_map == 0 ? Qfalse : Qtrue);
42
- ptr_coil++;
43
- ptr_map++;
44
- }
45
- }
46
-
47
- void mb_push_input_status(modbus_slave_t *mb_slave)
48
- {
49
- int len = RARRAY_LEN(mb_slave->input_status);
50
- VALUE *ptr_input = RARRAY_PTR(mb_slave->input_status);
51
- mb_slave->mb_map->nb_input_status = len;
52
- mb_slave->mb_map->tab_input_status = malloc(sizeof(uint8_t) * len);
53
- uint8_t *ptr_map = mb_slave->mb_map->tab_input_status;
54
-
55
- int i;
56
- for (i = 0; i < len; i++) {
57
- *ptr_map = (*ptr_input == Qtrue ? 1 : 0);
58
- ptr_input++;
59
- ptr_map++;
60
- }
61
- }
62
-
63
- void mb_push_holding_registers(modbus_slave_t *mb_slave)
64
- {
65
- int len = RARRAY_LEN(mb_slave->holding_registers);
66
- VALUE *ptr_reg = RARRAY_PTR(mb_slave->holding_registers);
67
- mb_slave->mb_map->nb_holding_registers = len;
68
- mb_slave->mb_map->tab_holding_registers = malloc(sizeof(uint16_t) * len);
69
- uint16_t *ptr_map = mb_slave->mb_map->tab_holding_registers;
70
-
71
- int i;
72
- for (i = 0; i < len; i++) {
73
- *ptr_map = FIX2INT(*ptr_reg);
74
- ptr_reg++;
75
- ptr_map++;
76
- }
77
- }
78
-
79
- void mb_pull_holding_registers(modbus_slave_t *mb_slave)
80
- {
81
- uint16_t *ptr_map = mb_slave->mb_map->tab_holding_registers;
82
- VALUE *ptr_reg = RARRAY_PTR(mb_slave->holding_registers);
83
- int i;
84
- for(i = 0; i < mb_slave->mb_map->nb_holding_registers; i++) {
85
- *ptr_reg = INT2FIX(*ptr_map);
86
- ptr_reg++;
87
- ptr_map++;
88
- }
89
- }
90
-
91
- void mb_push_input_registers(modbus_slave_t *mb_slave)
92
- {
93
- int len = RARRAY_LEN(mb_slave->input_registers);
94
- VALUE *ptr_reg = RARRAY_PTR(mb_slave->input_registers);
95
- mb_slave->mb_map->nb_input_registers = len;
96
- mb_slave->mb_map->tab_input_registers = malloc(sizeof(uint16_t) * len);
97
- uint16_t *ptr_map = mb_slave->mb_map->tab_input_registers;
98
-
99
- int i;
100
- for (i = 0; i < len; i++) {
101
- *ptr_map = FIX2INT(*ptr_reg);
102
- ptr_reg++;
103
- ptr_map++;
104
- }
105
- }
106
-
107
- VALUE mb_sl_is_stoped(VALUE self)
108
- {
109
- modbus_slave_t *mb_slave;
110
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
111
-
112
- if (read(mb_slave->listen_sock, NULL, 0)
113
- || mb_slave->listen_sock == 0) {
114
- return Qtrue;
115
- }
116
- return Qfalse;
117
- }
118
-
119
- VALUE mb_sl_get_coil_status(VALUE self)
120
- {
121
- modbus_slave_t *mb_slave;
122
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
123
-
124
- return mb_slave->coil_status;
125
- }
126
-
127
- VALUE mb_sl_set_coil_status(VALUE self, VALUE value)
128
- {
129
- modbus_slave_t *mb_slave;
130
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
131
-
132
- value = rb_funcall(value, rb_intern("to_a"), 0);
133
- mb_slave->coil_status = rb_ary_dup(value);
134
-
135
- return mb_slave->coil_status;
136
- }
137
-
138
- VALUE mb_sl_get_input_status(VALUE self)
139
- {
140
- modbus_slave_t *mb_slave;
141
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
142
-
143
- return mb_slave->input_status;
144
- }
145
-
146
- VALUE mb_sl_set_input_status(VALUE self, VALUE value)
147
- {
148
- modbus_slave_t *mb_slave;
149
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
150
-
151
- value = rb_funcall(value, rb_intern("to_a"), 0);
152
- mb_slave->input_status = rb_ary_dup(value);
153
-
154
- return mb_slave->input_status;
155
- }
156
-
157
- VALUE mb_sl_get_holding_registers(VALUE self)
158
- {
159
- modbus_slave_t *mb_slave;
160
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
161
-
162
- return mb_slave->holding_registers;
163
- }
164
-
165
- VALUE mb_sl_set_holding_registers(VALUE self, VALUE value)
166
- {
167
- modbus_slave_t *mb_slave;
168
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
169
-
170
- value = rb_funcall(value, rb_intern("to_a"), 0);
171
- mb_slave->holding_registers = rb_ary_dup(value);
172
- VALUE *reg = RARRAY_PTR(mb_slave->holding_registers);
173
-
174
- int i;
175
- for (i = 0; i < RARRAY_LEN(mb_slave->holding_registers); i++) {
176
- *reg = rb_funcall(*reg, rb_intern("to_i"), 0);
177
- }
178
-
179
- return mb_slave->holding_registers;
180
- }
181
-
182
- VALUE mb_sl_get_input_registers(VALUE self)
183
- {
184
- modbus_slave_t *mb_slave;
185
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
186
-
187
- return mb_slave->input_registers;
188
- }
189
-
190
- VALUE mb_sl_set_input_registers(VALUE self, VALUE value)
191
- {
192
- modbus_slave_t *mb_slave;
193
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
194
-
195
- value = rb_funcall(value, rb_intern("to_a"), 0);
196
- mb_slave->input_registers = rb_ary_dup(value);
197
- VALUE *reg = RARRAY_PTR(mb_slave->input_registers);
198
-
199
- int i;
200
- for (i = 0; i < RARRAY_LEN(mb_slave->input_registers); i++) {
201
- *reg = rb_funcall(*reg, rb_intern("to_i"), 0);
202
- }
203
-
204
- return mb_slave->input_registers;
205
- }
206
-
207
- VALUE mb_sl_join(VALUE self)
208
- {
209
- modbus_slave_t *mb_slave;
210
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
211
-
212
- pthread_join(mb_slave->tid, NULL);
213
-
214
- return self;
215
- }