libmodbus4r 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/NEWS +6 -0
- data/README +2 -2
- data/TODO +5 -0
- data/ext/errors.c +81 -0
- data/ext/errors.h +24 -0
- data/ext/extconf.rb +1 -1
- data/ext/master.c +225 -0
- data/ext/master.h +36 -0
- data/ext/modbus4r.c +79 -0
- data/ext/modbus4r.h +21 -0
- data/ext/rtu_master.c +38 -0
- data/ext/rtu_master.h +20 -0
- data/ext/tcp_master.c +33 -0
- data/ext/tcp_master.h +19 -0
- data/lib/modbus4r.rb +14 -0
- metadata +18 -5
- data/ChangeLog +0 -8
- data/lib/modbus4r.so +0 -0
data/NEWS
ADDED
data/README
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Binding use *libmodbus* (free implementation of modbus for Linux\MacOS) for Ruby.
|
4
4
|
|
5
|
-
==
|
5
|
+
== Features
|
6
6
|
|
7
7
|
* Use only Ruby-1.9.x
|
8
8
|
* Use C library *libmodbus*;
|
@@ -32,7 +32,7 @@ Aleksey Timin <atimin@gmail.com>
|
|
32
32
|
|
33
33
|
== Refernces
|
34
34
|
|
35
|
-
Home page projects : http://flipback.github.com/libmodbus4r
|
35
|
+
Home page projects : http://flipback.github.com/libmodbus4r/
|
36
36
|
Hosting on RubyForge : http://rubyforge.org/projects/libmodbus4r
|
37
37
|
Hosting on GitHub : http://github.com/flipback/libmodbus4r/tree/master
|
38
38
|
|
data/TODO
ADDED
data/ext/errors.c
ADDED
@@ -0,0 +1,81 @@
|
|
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
|
+
|
16
|
+
VALUE mErrors, eModBusError, eIllegalFunction,
|
17
|
+
eIllegalDataAddress, eIllegalDataValue,
|
18
|
+
eSlaveDeviceFailure, eAcknowledge,
|
19
|
+
eSlaveDeviceBusy, eNegativeAcknowledge,
|
20
|
+
eMemoryParityError, eGatewayProblemPath, eGatewayProblemTarget;
|
21
|
+
|
22
|
+
void mb_raise_error(int exception)
|
23
|
+
{
|
24
|
+
switch (exception) {
|
25
|
+
case ILLEGAL_FUNCTION:
|
26
|
+
rb_raise(eIllegalFunction, "Illegal function code (%i)", ILLEGAL_FUNCTION);
|
27
|
+
break;
|
28
|
+
case ILLEGAL_DATA_ADDRESS:
|
29
|
+
rb_raise(eIllegalDataAddress, "Illegal data address (%i)", ILLEGAL_DATA_ADDRESS);
|
30
|
+
break;
|
31
|
+
case ILLEGAL_DATA_VALUE:
|
32
|
+
rb_raise(eIllegalDataValue, "Illegal data value (%i)", ILLEGAL_DATA_VALUE);
|
33
|
+
break;
|
34
|
+
case SLAVE_DEVICE_FAILURE:
|
35
|
+
rb_raise(eSlaveDeviceFailure, "Slave device or server failure (%i)", SLAVE_DEVICE_FAILURE);
|
36
|
+
break;
|
37
|
+
case ACKNOWLEDGE:
|
38
|
+
rb_raise(eAcknowledge, "Acknowledge (%i)", ACKNOWLEDGE);
|
39
|
+
break;
|
40
|
+
case SLAVE_DEVICE_BUSY:
|
41
|
+
rb_raise(eSlaveDeviceBusy, "Slave device or server busy (%i)", SLAVE_DEVICE_BUSY);
|
42
|
+
break;
|
43
|
+
case NEGATIVE_ACKNOWLEDGE:
|
44
|
+
rb_raise(eNegativeAcknowledge, "Negative acknowledge (%i)", NEGATIVE_ACKNOWLEDGE);
|
45
|
+
break;
|
46
|
+
case MEMORY_PARITY_ERROR:
|
47
|
+
rb_raise(eMemoryParityError, "Memory parity error (%i)", MEMORY_PARITY_ERROR);
|
48
|
+
break;
|
49
|
+
case GATEWAY_PROBLEM_PATH:
|
50
|
+
rb_raise(eGatewayProblemPath, "Gateway problem path (%i)", GATEWAY_PROBLEM_PATH);
|
51
|
+
break;
|
52
|
+
case GATEWAY_PROBLEM_TARGET:
|
53
|
+
rb_raise(eGatewayProblemTarget, "Gateway problem target (%i)", GATEWAY_PROBLEM_TARGET);
|
54
|
+
break;
|
55
|
+
case COMM_TIME_OUT:
|
56
|
+
rb_raise(eModBusError, "Communication timeout (%i)", COMM_TIME_OUT);
|
57
|
+
break;
|
58
|
+
case PORT_SOCKET_FAILURE:
|
59
|
+
rb_raise(eModBusError, "Port socket failure (%i)", PORT_SOCKET_FAILURE);
|
60
|
+
break;
|
61
|
+
case SELECT_FAILURE:
|
62
|
+
rb_raise(eModBusError, "Select failure (%i)", SELECT_FAILURE);
|
63
|
+
break;
|
64
|
+
case TOO_MANY_DATA:
|
65
|
+
rb_raise(eModBusError, "Too many data (%i)", TOO_MANY_DATA);
|
66
|
+
break;
|
67
|
+
case INVALID_CRC:
|
68
|
+
rb_raise(eModBusError, "Invalid CRC (%i)", INVALID_CRC);
|
69
|
+
break;
|
70
|
+
case INVALID_EXCEPTION_CODE:
|
71
|
+
rb_raise(eModBusError, "Invalid exception code (%i)", INVALID_EXCEPTION_CODE);
|
72
|
+
break;
|
73
|
+
case CONNECTION_CLOSED:
|
74
|
+
rb_raise(eModBusError, "Connection closed (%i)", CONNECTION_CLOSED);
|
75
|
+
break;
|
76
|
+
default:
|
77
|
+
rb_raise(eModBusError, "Unknow exception with code %i", exception);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
|
data/ext/errors.h
ADDED
@@ -0,0 +1,24 @@
|
|
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 ERRORS_H
|
15
|
+
#define ERRORS_H
|
16
|
+
|
17
|
+
extern VALUE mErrors, eModBusError, eIllegalFunction,
|
18
|
+
eIllegalDataAddress, eIllegalDataValue,
|
19
|
+
eSlaveDeviceFailure, eAcknowledge,
|
20
|
+
eSlaveDeviceBusy, eNegativeAcknowledge,
|
21
|
+
eMemoryParityError, eGatewayProblemPath, eGatewayProblemTarget;
|
22
|
+
|
23
|
+
extern void mb_raise_error(int exception);
|
24
|
+
#endif
|
data/ext/extconf.rb
CHANGED
data/ext/master.c
ADDED
@@ -0,0 +1,225 @@
|
|
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 "errors.h"
|
16
|
+
|
17
|
+
VALUE mb_mstr_is_closed(VALUE self)
|
18
|
+
{
|
19
|
+
modbus_param_t *mb_param;
|
20
|
+
Data_Get_Struct(self, modbus_param_t, mb_param);
|
21
|
+
|
22
|
+
if (read(mb_param->fd, NULL, 0) || mb_param->fd == 0) {
|
23
|
+
return Qtrue;
|
24
|
+
}
|
25
|
+
return Qfalse;
|
26
|
+
}
|
27
|
+
|
28
|
+
VALUE mb_mstr_connect(VALUE self)
|
29
|
+
{
|
30
|
+
modbus_param_t *mb_param;
|
31
|
+
Data_Get_Struct(self, modbus_param_t, mb_param);
|
32
|
+
|
33
|
+
if (modbus_connect(mb_param)) {
|
34
|
+
rb_raise(eModBusError, "Connection refused");
|
35
|
+
}
|
36
|
+
return self;
|
37
|
+
}
|
38
|
+
|
39
|
+
VALUE mb_mstr_close(VALUE self)
|
40
|
+
{
|
41
|
+
modbus_param_t *mb_param;
|
42
|
+
Data_Get_Struct(self, modbus_param_t, mb_param);
|
43
|
+
|
44
|
+
modbus_close(mb_param);
|
45
|
+
|
46
|
+
return self;
|
47
|
+
}
|
48
|
+
|
49
|
+
VALUE mb_mstr_read_coil(VALUE self, VALUE slave,
|
50
|
+
VALUE start_addr, VALUE nb,
|
51
|
+
int (*func)(modbus_param_t*, int, int, int, uint8_t*))
|
52
|
+
{
|
53
|
+
modbus_param_t *mb_param;
|
54
|
+
Data_Get_Struct(self, modbus_param_t, mb_param);
|
55
|
+
|
56
|
+
slave = rb_funcall(slave, rb_intern("to_i"), 0);
|
57
|
+
start_addr = rb_funcall(start_addr, rb_intern("to_i"), 0);
|
58
|
+
nb = rb_funcall(nb, rb_intern("to_i"), 0);
|
59
|
+
|
60
|
+
uint8_t dest[FIX2INT(nb)];
|
61
|
+
int status = (*func)(mb_param, FIX2INT(slave), FIX2INT(start_addr), FIX2INT(nb), dest);
|
62
|
+
|
63
|
+
if (status < 0) {
|
64
|
+
mb_raise_error(status);
|
65
|
+
}
|
66
|
+
|
67
|
+
int i;
|
68
|
+
VALUE ret = rb_ary_new();
|
69
|
+
for (i = 0; i < FIX2INT(nb); i++) {
|
70
|
+
dest[i] == 1 ? rb_ary_push(ret, Qtrue) : rb_ary_push(ret, Qfalse);
|
71
|
+
}
|
72
|
+
|
73
|
+
return ret;
|
74
|
+
}
|
75
|
+
|
76
|
+
VALUE mb_mstr_read_coil_status(VALUE self, VALUE slave,
|
77
|
+
VALUE start_addr, VALUE nb)
|
78
|
+
{
|
79
|
+
return mb_mstr_read_coil(self, slave, start_addr, nb,
|
80
|
+
read_coil_status);
|
81
|
+
}
|
82
|
+
|
83
|
+
VALUE mb_mstr_read_input_status(VALUE self, VALUE slave,
|
84
|
+
VALUE start_addr, VALUE nb)
|
85
|
+
{
|
86
|
+
return mb_mstr_read_coil(self, slave, start_addr, nb,
|
87
|
+
read_input_status);
|
88
|
+
}
|
89
|
+
|
90
|
+
VALUE mb_mstr_read_registers(VALUE self, VALUE slave,
|
91
|
+
VALUE start_addr, VALUE nb,
|
92
|
+
int (*func)(modbus_param_t*, int, int, int, uint16_t*))
|
93
|
+
{
|
94
|
+
modbus_param_t *mb_param;
|
95
|
+
Data_Get_Struct(self, modbus_param_t, mb_param);
|
96
|
+
|
97
|
+
slave = rb_funcall(slave, rb_intern("to_i"), 0);
|
98
|
+
start_addr = rb_funcall(start_addr, rb_intern("to_i"), 0);
|
99
|
+
nb = rb_funcall(nb, rb_intern("to_i"), 0);
|
100
|
+
|
101
|
+
uint16_t dest[FIX2INT(nb)];
|
102
|
+
int status = (*func)(mb_param, FIX2INT(slave), FIX2INT(start_addr), FIX2INT(nb), dest);
|
103
|
+
|
104
|
+
if (status < 0) {
|
105
|
+
mb_raise_error(status);
|
106
|
+
}
|
107
|
+
|
108
|
+
int i;
|
109
|
+
VALUE ret = rb_ary_new();
|
110
|
+
for (i = 0; i < FIX2INT(nb); i++) {
|
111
|
+
rb_ary_push(ret, INT2FIX(dest[i]));
|
112
|
+
}
|
113
|
+
|
114
|
+
|
115
|
+
return ret;
|
116
|
+
}
|
117
|
+
|
118
|
+
VALUE mb_mstr_read_holding_registers(VALUE self, VALUE slave,
|
119
|
+
VALUE start_addr, VALUE nb)
|
120
|
+
{
|
121
|
+
return mb_mstr_read_registers(self, slave, start_addr, nb,
|
122
|
+
read_holding_registers);
|
123
|
+
}
|
124
|
+
|
125
|
+
VALUE mb_mstr_read_input_registers(VALUE self, VALUE slave,
|
126
|
+
VALUE start_addr, VALUE nb)
|
127
|
+
{
|
128
|
+
return mb_mstr_read_registers(self, slave, start_addr, nb,
|
129
|
+
read_input_registers);
|
130
|
+
}
|
131
|
+
|
132
|
+
VALUE mb_mstr_force_single_coil(VALUE self, VALUE slave,
|
133
|
+
VALUE coil_addr, VALUE state)
|
134
|
+
{
|
135
|
+
modbus_param_t *mb_param;
|
136
|
+
Data_Get_Struct(self, modbus_param_t, mb_param);
|
137
|
+
|
138
|
+
slave = rb_funcall(slave, rb_intern("to_i"), 0);
|
139
|
+
coil_addr = rb_funcall(coil_addr, rb_intern("to_i"), 0);
|
140
|
+
|
141
|
+
int status = force_single_coil(mb_param, FIX2INT(slave), FIX2INT(coil_addr), (state == Qfalse ? 0 : 1));
|
142
|
+
|
143
|
+
if (status < 0) {
|
144
|
+
mb_raise_error(status);
|
145
|
+
}
|
146
|
+
|
147
|
+
return self;
|
148
|
+
}
|
149
|
+
|
150
|
+
|
151
|
+
VALUE mb_mstr_preset_single_register(VALUE self, VALUE slave,
|
152
|
+
VALUE reg_addr, VALUE value)
|
153
|
+
{
|
154
|
+
modbus_param_t *mb_param;
|
155
|
+
Data_Get_Struct(self, modbus_param_t, mb_param);
|
156
|
+
|
157
|
+
slave = rb_funcall(slave, rb_intern("to_i"), 0);
|
158
|
+
reg_addr = rb_funcall(reg_addr, rb_intern("to_i"), 0);
|
159
|
+
value = rb_funcall(value, rb_intern("to_i"), 0);
|
160
|
+
|
161
|
+
int status = preset_single_register(mb_param, FIX2INT(slave), FIX2INT(reg_addr), FIX2INT(value));
|
162
|
+
|
163
|
+
if (status < 0) {
|
164
|
+
mb_raise_error(status);
|
165
|
+
}
|
166
|
+
|
167
|
+
return self;
|
168
|
+
}
|
169
|
+
|
170
|
+
VALUE mb_mstr_force_multiple_coils(VALUE self, VALUE slave,
|
171
|
+
VALUE start_addr, VALUE data)
|
172
|
+
{
|
173
|
+
modbus_param_t *mb_param;
|
174
|
+
Data_Get_Struct(self, modbus_param_t, mb_param);
|
175
|
+
|
176
|
+
slave = rb_funcall(slave, rb_intern("to_i"), 0);
|
177
|
+
start_addr = rb_funcall(start_addr, rb_intern("to_i"), 0);
|
178
|
+
data = rb_funcall(data, rb_intern("to_a"), 0);
|
179
|
+
|
180
|
+
uint8_t buf[RARRAY_LEN(data)];
|
181
|
+
VALUE *ary = RARRAY_PTR(data);
|
182
|
+
int i;
|
183
|
+
|
184
|
+
for (i = 0; i < RARRAY_LEN(data); i++) {
|
185
|
+
buf[i] = (*ary == Qfalse ? 0 : 1);
|
186
|
+
ary++;
|
187
|
+
}
|
188
|
+
|
189
|
+
int status = force_multiple_coils(mb_param, FIX2INT(slave), FIX2INT(start_addr), RARRAY_LEN(data), buf);
|
190
|
+
|
191
|
+
if (status < 0) {
|
192
|
+
mb_raise_error(status);
|
193
|
+
}
|
194
|
+
|
195
|
+
return self;
|
196
|
+
}
|
197
|
+
|
198
|
+
VALUE mb_mstr_preset_multiple_registers(VALUE self, VALUE slave,
|
199
|
+
VALUE start_addr, VALUE data)
|
200
|
+
{
|
201
|
+
modbus_param_t *mb_param;
|
202
|
+
Data_Get_Struct(self, modbus_param_t, mb_param);
|
203
|
+
|
204
|
+
slave = rb_funcall(slave, rb_intern("to_i"), 0);
|
205
|
+
start_addr = rb_funcall(start_addr, rb_intern("to_i"), 0);
|
206
|
+
data = rb_funcall(data, rb_intern("to_a"), 0);
|
207
|
+
|
208
|
+
uint16_t buf[RARRAY_LEN(data)];
|
209
|
+
VALUE *ary = RARRAY_PTR(data);
|
210
|
+
int i;
|
211
|
+
|
212
|
+
for (i = 0; i < RARRAY_LEN(data); i++) {
|
213
|
+
*ary = rb_funcall(*ary, rb_intern("to_i"), 0);
|
214
|
+
buf[i] = FIX2INT(*ary);
|
215
|
+
ary++;
|
216
|
+
}
|
217
|
+
|
218
|
+
int status = preset_multiple_registers(mb_param, FIX2INT(slave), FIX2INT(start_addr), RARRAY_LEN(data), buf);
|
219
|
+
|
220
|
+
if (status < 0) {
|
221
|
+
mb_raise_error(status);
|
222
|
+
}
|
223
|
+
|
224
|
+
return self;
|
225
|
+
}
|
data/ext/master.h
ADDED
@@ -0,0 +1,36 @@
|
|
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 MASTER_H
|
15
|
+
#define MASTER_H
|
16
|
+
|
17
|
+
extern VALUE mb_mstr_is_closed(VALUE self);
|
18
|
+
extern VALUE mb_mstr_connect(VALUE self);
|
19
|
+
extern VALUE mb_mstr_close(VALUE self);
|
20
|
+
extern VALUE mb_mstr_read_coil_status(VALUE self, VALUE slave,
|
21
|
+
VALUE start_addr, VALUE nb);
|
22
|
+
extern VALUE mb_mstr_read_input_status(VALUE self, VALUE slave,
|
23
|
+
VALUE start_addr, VALUE nb);
|
24
|
+
extern VALUE mb_mstr_read_holding_registers(VALUE self, VALUE slave,
|
25
|
+
VALUE start_addr, VALUE nb);
|
26
|
+
extern VALUE mb_mstr_read_input_registers(VALUE self, VALUE slave,
|
27
|
+
VALUE start_addr, VALUE nb);
|
28
|
+
extern VALUE mb_mstr_force_single_coil(VALUE self, VALUE slave,
|
29
|
+
VALUE coil_addr, VALUE state);
|
30
|
+
extern VALUE mb_mstr_preset_single_register(VALUE self, VALUE slave,
|
31
|
+
VALUE reg_addr, VALUE value);
|
32
|
+
extern VALUE mb_mstr_force_multiple_coils(VALUE self, VALUE slave,
|
33
|
+
VALUE start_addr, VALUE data);
|
34
|
+
extern VALUE mb_mstr_preset_multiple_registers(VALUE self, VALUE slave,
|
35
|
+
VALUE start_addr, VALUE data);
|
36
|
+
#endif
|
data/ext/modbus4r.c
ADDED
@@ -0,0 +1,79 @@
|
|
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 "errors.h"
|
16
|
+
#include "master.h"
|
17
|
+
#include "tcp_master.h"
|
18
|
+
#include "rtu_master.h"
|
19
|
+
|
20
|
+
VALUE mModBus, cMaster, cTCPMaster, cRTUMaster;
|
21
|
+
|
22
|
+
void Init__modbus4r()
|
23
|
+
{
|
24
|
+
mModBus = rb_define_module("ModBus");
|
25
|
+
/* Master */
|
26
|
+
cMaster = rb_define_class_under(mModBus, "Master", rb_cObject);
|
27
|
+
rb_define_method(cMaster, "closed?", mb_mstr_is_closed, 0);
|
28
|
+
rb_define_method(cMaster, "connect", mb_mstr_connect, 0);
|
29
|
+
rb_define_method(cMaster, "close", mb_mstr_close, 0);
|
30
|
+
rb_define_method(cMaster, "read_coil_status",
|
31
|
+
mb_mstr_read_coil_status, 3);
|
32
|
+
rb_define_method(cMaster, "read_input_status",
|
33
|
+
mb_mstr_read_input_status, 3);
|
34
|
+
rb_define_method(cMaster, "read_holding_registers",
|
35
|
+
mb_mstr_read_holding_registers, 3);
|
36
|
+
rb_define_method(cMaster, "read_input_registers",
|
37
|
+
mb_mstr_read_holding_registers, 3);
|
38
|
+
rb_define_method(cMaster, "force_single_coil",
|
39
|
+
mb_mstr_force_single_coil, 3);
|
40
|
+
rb_define_method(cMaster, "preset_single_register",
|
41
|
+
mb_mstr_preset_single_register, 3);
|
42
|
+
rb_define_method(cMaster, "force_multiple_coils",
|
43
|
+
mb_mstr_force_multiple_coils, 3);
|
44
|
+
rb_define_method(cMaster, "preset_multiple_registers",
|
45
|
+
mb_mstr_preset_multiple_registers, 3);
|
46
|
+
|
47
|
+
/* TCPMaster */
|
48
|
+
cTCPMaster = rb_define_class_under(mModBus, "TCPMaster", cMaster);
|
49
|
+
rb_define_singleton_method(cTCPMaster, "new", mb_tcp_mstr_new, 2);
|
50
|
+
|
51
|
+
/* RTUMaster */
|
52
|
+
cRTUMaster = rb_define_class_under(mModBus, "RTUMaster", cMaster);
|
53
|
+
rb_define_singleton_method(cRTUMaster, "new", mb_rtu_mstr_new, 5);
|
54
|
+
|
55
|
+
/* Errors */
|
56
|
+
mErrors = rb_define_module_under(mModBus, "Errors");
|
57
|
+
eModBusError = rb_define_class_under(mErrors, "ModBusError",
|
58
|
+
rb_eStandardError);
|
59
|
+
eIllegalFunction = rb_define_class_under(mErrors, "IllegalFunction",
|
60
|
+
eModBusError);
|
61
|
+
eIllegalDataAddress = rb_define_class_under(mErrors, "IllegalDataAddress",
|
62
|
+
eModBusError);
|
63
|
+
eIllegalDataValue = rb_define_class_under(mErrors, "IllegalDataValue",
|
64
|
+
eModBusError);
|
65
|
+
eSlaveDeviceFailure = rb_define_class_under(mErrors, "SlaveDeviceFailure",
|
66
|
+
eModBusError);
|
67
|
+
eAcknowledge = rb_define_class_under(mErrors, "Acknowledge",
|
68
|
+
eModBusError);
|
69
|
+
eSlaveDeviceBusy = rb_define_class_under(mErrors, "SlaveDeviceBusy",
|
70
|
+
eModBusError);
|
71
|
+
eNegativeAcknowledge = rb_define_class_under(mErrors, "NegativeAcknowledge",
|
72
|
+
eModBusError);
|
73
|
+
eMemoryParityError = rb_define_class_under(mErrors, "MemoryParityError",
|
74
|
+
eModBusError);
|
75
|
+
eGatewayProblemPath = rb_define_class_under(mErrors, "GatewayProblemPath",
|
76
|
+
eModBusError);
|
77
|
+
eGatewayProblemTarget = rb_define_class_under(mErrors, "GatewayProblemTarget",
|
78
|
+
eModBusError);
|
79
|
+
}
|
data/ext/modbus4r.h
ADDED
@@ -0,0 +1,21 @@
|
|
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 "ruby/ruby.h"
|
18
|
+
#include "ruby/intern.h"
|
19
|
+
#include "modbus/modbus.h"
|
20
|
+
|
21
|
+
#endif
|
data/ext/rtu_master.c
ADDED
@@ -0,0 +1,38 @@
|
|
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
|
+
|
16
|
+
void mb_rtu_mstr_free(modbus_param_t *mb_param )
|
17
|
+
{
|
18
|
+
modbus_close(mb_param);
|
19
|
+
free(mb_param);
|
20
|
+
}
|
21
|
+
|
22
|
+
VALUE mb_rtu_mstr_new(VALUE self, VALUE device, VALUE baud,
|
23
|
+
VALUE parity, VALUE data_bit, VALUE stop_bit)
|
24
|
+
{
|
25
|
+
modbus_param_t *mb_param;
|
26
|
+
|
27
|
+
mb_param = malloc(sizeof(modbus_param_t));
|
28
|
+
device = rb_funcall(device, rb_intern("to_s"), 0);
|
29
|
+
baud = rb_funcall(baud, rb_intern("to_i"), 0);
|
30
|
+
parity = rb_funcall(parity, rb_intern("to_s"), 0);
|
31
|
+
data_bit = rb_funcall(data_bit, rb_intern("to_i"), 0);
|
32
|
+
stop_bit = rb_funcall(stop_bit, rb_intern("to_i"), 0);
|
33
|
+
|
34
|
+
modbus_init_rtu(mb_param, RSTRING_PTR(device), FIX2INT(baud),
|
35
|
+
RSTRING_PTR(parity), FIX2INT(data_bit), FIX2INT(stop_bit));
|
36
|
+
|
37
|
+
return Data_Wrap_Struct(self, 0, mb_rtu_mstr_free, mb_param);
|
38
|
+
}
|
data/ext/rtu_master.h
ADDED
@@ -0,0 +1,20 @@
|
|
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, VALUE stop_bit);
|
19
|
+
|
20
|
+
#endif
|
data/ext/tcp_master.c
ADDED
@@ -0,0 +1,33 @@
|
|
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
|
+
|
16
|
+
void mb_tcp_mstr_free(modbus_param_t *mb_param )
|
17
|
+
{
|
18
|
+
modbus_close(mb_param);
|
19
|
+
free(mb_param);
|
20
|
+
}
|
21
|
+
|
22
|
+
VALUE mb_tcp_mstr_new(VALUE self, VALUE ip_address, VALUE port)
|
23
|
+
{
|
24
|
+
modbus_param_t *mb_param;
|
25
|
+
|
26
|
+
mb_param = malloc(sizeof(modbus_param_t));
|
27
|
+
ip_address = rb_funcall(ip_address, rb_intern("to_s"), 0);
|
28
|
+
port = rb_funcall(port, rb_intern("to_i"), 0);
|
29
|
+
|
30
|
+
modbus_init_tcp(mb_param, RSTRING_PTR(ip_address), FIX2INT(port));
|
31
|
+
|
32
|
+
return Data_Wrap_Struct(self, 0, mb_tcp_mstr_free, mb_param);
|
33
|
+
}
|
data/ext/tcp_master.h
ADDED
@@ -0,0 +1,19 @@
|
|
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 TCP_MASTER_H
|
15
|
+
#define TCP_MASTER_H
|
16
|
+
|
17
|
+
extern VALUE mb_tcp_mstr_new(VALUE self, VALUE ip_address, VALUE port);
|
18
|
+
|
19
|
+
#endif
|
data/lib/modbus4r.rb
ADDED
@@ -0,0 +1,14 @@
|
|
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
|
+
require '_modbus4r'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: libmodbus4r
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aleksey Timin
|
@@ -9,7 +9,7 @@ autorequire: libmodbus4r
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-10 00:00:00 +06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -22,16 +22,29 @@ extensions:
|
|
22
22
|
extra_rdoc_files:
|
23
23
|
- README
|
24
24
|
- LICENSE
|
25
|
-
-
|
25
|
+
- NEWS
|
26
|
+
- TODO
|
26
27
|
files:
|
27
|
-
- lib/modbus4r.
|
28
|
+
- lib/modbus4r.rb
|
28
29
|
- examples/use_rtu_master.rb
|
29
30
|
- examples/use_tcp_master.rb
|
30
31
|
- spec/rtu_master_spec.rb
|
31
32
|
- spec/tcp_master_spec.rb
|
33
|
+
- ext/tcp_master.c
|
34
|
+
- ext/errors.c
|
35
|
+
- ext/modbus4r.h
|
36
|
+
- ext/extconf.rb
|
37
|
+
- ext/master.h
|
38
|
+
- ext/rtu_master.c
|
39
|
+
- ext/tcp_master.h
|
40
|
+
- ext/rtu_master.h
|
41
|
+
- ext/master.c
|
42
|
+
- ext/errors.h
|
43
|
+
- ext/modbus4r.c
|
32
44
|
- README
|
33
45
|
- LICENSE
|
34
|
-
-
|
46
|
+
- NEWS
|
47
|
+
- TODO
|
35
48
|
has_rdoc: true
|
36
49
|
homepage:
|
37
50
|
licenses: []
|
data/ChangeLog
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
2009-07-30 Timin Aleksey <atimin@gmail.com>
|
4
|
-
* ext/master.c, ext/master.h, ext/tcp_master.c, ext/tcp_master.h, ext/rtu_master.c, ext/rtu_master,h : Create base Master class for TCPMaster and RTUMaster
|
5
|
-
* ext/error.c ext/error.h : Add suppot all exception of libmodbus
|
6
|
-
* spec/rtu_master_spec.rb: Add test for RTUMaster, but it not run!
|
7
|
-
* ChageLog: Start log of shanges.
|
8
|
-
|
data/lib/modbus4r.so
DELETED
Binary file
|