libmodbus4r 0.3.0 → 2.0.0

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/ext/slave.h DELETED
@@ -1,44 +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 SLAVE_H
15
- #define SLAVE_H
16
-
17
- typedef struct {
18
- pthread_t tid;
19
- int listen_sock;
20
- modbus_param_t *mb_param;
21
- modbus_mapping_t *mb_map;
22
- VALUE coil_status;
23
- VALUE input_status;
24
- VALUE holding_registers;
25
- VALUE input_registers;
26
- } modbus_slave_t;
27
-
28
- extern void mb_push_coil_status(modbus_slave_t *mb_slave);
29
- extern void mb_pull_coil_status(modbus_slave_t *mb_slave);
30
- extern void mb_push_input_status(modbus_slave_t *mb_slave);
31
- extern void mb_push_holding_registers(modbus_slave_t *mb_slave);
32
- extern void mb_pull_holding_registers(modbus_slave_t *mb_slave);
33
- extern void mb_push_input_registers(modbus_slave_t *mb_slave);
34
- extern VALUE mb_sl_is_stoped(VALUE self);
35
- extern VALUE mb_sl_get_coil_status(VALUE self);
36
- extern VALUE mb_sl_set_coil_status(VALUE self, VALUE value);
37
- extern VALUE mb_sl_get_input_status(VALUE self);
38
- extern VALUE mb_sl_set_input_status(VALUE self, VALUE value);
39
- extern VALUE mb_sl_get_holding_registers(VALUE self);
40
- extern VALUE mb_sl_set_holding_registers(VALUE self, VALUE value);
41
- extern VALUE mb_sl_get_input_registers(VALUE self);
42
- extern VALUE mb_sl_set_input_registers(VALUE self, VALUE value);
43
- extern VALUE mb_sl_join(VALUE self);
44
- #endif
data/ext/tcp_master.c DELETED
@@ -1,29 +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_tcp_mstr_new(VALUE self, VALUE ip_address, VALUE port, VALUE slave)
18
- {
19
- modbus_param_t *mb_param;
20
-
21
- mb_param = malloc(sizeof(modbus_param_t));
22
- ip_address = rb_funcall(ip_address, rb_intern("to_s"), 0);
23
- port = rb_funcall(port, rb_intern("to_i"), 0);
24
- slave = rb_funcall(slave, rb_intern("to_i"), 0);
25
-
26
- modbus_init_tcp(mb_param, RSTRING_PTR(ip_address), FIX2INT(port), FIX2INT(slave));
27
-
28
- return Data_Wrap_Struct(self, 0, mb_mstr_free, mb_param);
29
- }
data/ext/tcp_master.h DELETED
@@ -1,20 +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 TCP_MASTER_H
15
- #define TCP_MASTER_H
16
-
17
- extern VALUE mb_tcp_mstr_new(VALUE self,
18
- VALUE ip_address, VALUE port, VALUE slave);
19
-
20
- #endif
data/ext/tcp_slave.c DELETED
@@ -1,134 +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_tcp_sl_free(modbus_slave_t *mb_slave)
21
- {
22
- close(mb_slave->listen_sock);
23
- pthread_cancel(mb_slave->tid);
24
-
25
- modbus_close(mb_slave->mb_param);
26
-
27
- modbus_mapping_free(mb_slave->mb_map);
28
-
29
- #ifndef RUBY_1_8
30
- rb_ary_free(mb_slave->coil_status);
31
- rb_ary_free(mb_slave->input_status);
32
- rb_ary_free(mb_slave->holding_registers);
33
- rb_ary_free(mb_slave->input_registers);
34
- #else
35
- free(RARRAY_PTR(mb_slave->coil_status));
36
- free(RARRAY_PTR(mb_slave->input_status));
37
- free(RARRAY_PTR(mb_slave->holding_registers));
38
- free(RARRAY_PTR(mb_slave->input_registers));
39
- #endif
40
-
41
- free(mb_slave);
42
- }
43
-
44
- void *mb_tcp_serv(void *arg)
45
- {
46
- modbus_slave_t *mb_slave = (modbus_slave_t *)arg;
47
-
48
- mb_slave->listen_sock = modbus_slave_listen_tcp(mb_slave->mb_param, 1);
49
-
50
- while (1) {
51
- modbus_slave_accept_tcp(mb_slave->mb_param,
52
- &mb_slave->listen_sock);
53
- uint8_t query[MAX_MESSAGE_LENGTH];
54
- int query_size;
55
- while(1) {
56
- int ret = modbus_slave_receive(mb_slave->mb_param, -1,
57
- query, &query_size);
58
-
59
- if (ret == 0) {
60
- mb_push_coil_status(mb_slave);
61
- mb_push_input_status(mb_slave);
62
- mb_push_holding_registers(mb_slave);
63
- mb_push_input_registers(mb_slave);
64
-
65
- modbus_slave_manage(mb_slave->mb_param, query,
66
- query_size, mb_slave->mb_map);
67
-
68
- mb_pull_coil_status(mb_slave);
69
- mb_pull_holding_registers(mb_slave);
70
- } else if (ret == CONNECTION_CLOSED) {
71
- modbus_close(mb_slave->mb_param);
72
- break;
73
- } else {
74
- continue;
75
- }
76
- }
77
- }
78
- }
79
-
80
- VALUE mb_tcp_sl_new(VALUE self, VALUE ip_address, VALUE port, VALUE slave)
81
- {
82
- modbus_slave_t *mb_slave;
83
- mb_slave = malloc(sizeof(modbus_slave_t));
84
- mb_slave->mb_param = malloc(sizeof(modbus_param_t));
85
- mb_slave->mb_map = malloc(sizeof(modbus_mapping_t));
86
-
87
- ip_address = rb_funcall(ip_address, rb_intern("to_s"), 0);
88
- port = rb_funcall(port, rb_intern("to_i"), 0);
89
- slave = rb_funcall(slave, rb_intern("to_i"), 0);
90
-
91
- modbus_init_tcp(mb_slave->mb_param, RSTRING_PTR(ip_address), FIX2INT(port), FIX2INT(slave));
92
-
93
- int ret = modbus_mapping_new(mb_slave->mb_map, 0, 0, 0, 0);
94
- if (ret < 0) {
95
- rb_raise(rb_eStandardError, "Memory allocation failed");
96
- }
97
-
98
- mb_slave->coil_status = rb_ary_new();
99
- mb_slave->input_status = rb_ary_new();
100
- mb_slave->holding_registers = rb_ary_new();
101
- mb_slave->input_registers = rb_ary_new();
102
-
103
- return Data_Wrap_Struct(self, 0, mb_tcp_sl_free, mb_slave);
104
- }
105
-
106
- VALUE mb_tcp_sl_start(VALUE self)
107
- {
108
- modbus_slave_t *mb_slave;
109
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
110
-
111
- int ret = pthread_create(&mb_slave->tid, NULL, mb_tcp_serv,
112
- (void *)mb_slave);
113
- if (ret != 0) {
114
- rb_raise(rb_eStandardError, "Slave has not started");
115
- }
116
-
117
- return self;
118
- }
119
-
120
- VALUE mb_tcp_sl_stop(VALUE self)
121
- {
122
- modbus_slave_t *mb_slave;
123
- Data_Get_Struct(self, modbus_slave_t, mb_slave);
124
-
125
- int ret = pthread_cancel(mb_slave->tid);
126
- if (ret != 0) {
127
- rb_raise(rb_eStandardError, "Slave has not stoped");
128
- }
129
-
130
- close(mb_slave->listen_sock);
131
- modbus_close(mb_slave->mb_param);
132
-
133
- return self;
134
- }
data/ext/tcp_slave.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 TCP_SLAVE_H
15
- #define TCP_SLAVE_H
16
-
17
- extern VALUE mb_tcp_sl_new(VALUE self, VALUE ip_address, VALUE port, VALUE id);
18
- extern VALUE mb_tcp_sl_start(VALUE self);
19
- extern VALUE mb_tcp_sl_stop(VALUE self);
20
-
21
- #endif
data/lib/modbus4r.rb DELETED
@@ -1,15 +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
- require '_modbus4r'
15
-
@@ -1,21 +0,0 @@
1
- require 'modbus4r'
2
-
3
- describe ModBus::RTUMaster do
4
-
5
- before(:all) do
6
- @mstr = ModBus::RTUMaster.new('/dev/ttyS3', 9600, 'none', 8, 1, 1)
7
- @mstr.connect
8
- end
9
-
10
- it "should be connect" do
11
- @mstr.closed?.should == false
12
- end
13
-
14
- after(:all) do
15
- @mstr.close unless @mstr.closed?
16
- end
17
-
18
- end
19
-
20
-
21
-
@@ -1,16 +0,0 @@
1
- require 'modbus4r'
2
-
3
- describe ModBus::RTUSlave do
4
- before(:all) do
5
- @sl = ModBus::RTUSlave.new('/dev/ttyS3', 9600, 'none', 8, 1, 1)
6
- @sl.start
7
- end
8
-
9
- it "should be start" do
10
- @sl.stoped?.should == false
11
- end
12
-
13
- after(:all) do
14
- @sl.stop unless @sl.stoped?
15
- end
16
- end
@@ -1,95 +0,0 @@
1
- require 'modbus4r'
2
-
3
- describe ModBus::TCPSlave do
4
-
5
- before(:each) do
6
- @sl = ModBus::TCPSlave.new('127.0.0.1', 1502, 1)
7
- @sl.start
8
-
9
- @mstr = ModBus::TCPMaster.new('127.0.0.1', 1502, 1)
10
- @mstr.connect
11
- end
12
-
13
- it "should have stoped" do
14
- sl = ModBus::TCPSlave.new('127.0.0.1', 1512, 1)
15
- sl.start.stoped? == false
16
-
17
-
18
- mstr = ModBus::TCPMaster.new('127.0.0.1', 1512, 1)
19
- mstr.connect.closed?.should == false
20
-
21
- sl.stop.stoped? == true
22
- lambda { mstr.connect }.should raise_error(ModBus::Errors::ModBusError)
23
- sl.start.stoped? == false
24
- end
25
-
26
- it "should have coil status" do
27
- @sl.coil_status.should == []
28
- @sl.coil_status = [false, true, false]
29
- @sl.coil_status.should == [false, true, false]
30
-
31
- @mstr.read_coil_status(0, 3).should == [false, true, false]
32
- @mstr.force_single_coil(2, true)
33
-
34
- @sl.coil_status.should == [false, true ,true]
35
-
36
- @mstr.force_multiple_coils(0, [true, true, true])
37
-
38
- @sl.coil_status.should == [true, true, true]
39
- @sl.coil_status[1] = false
40
- @sl.coil_status.should == [true, false, true]
41
-
42
- @mstr.read_coil_status(0, 3).should == [true, false, true]
43
- end
44
-
45
- it "should have input status" do
46
- @sl.input_status.should == []
47
- @sl.input_status = [false, false, false]
48
- @sl.input_status.should == [false, false, false]
49
-
50
- @mstr.read_input_status(0, 3).should == [false, false, false]
51
-
52
- @sl.input_status[1] = true
53
- @sl.input_status.should == [false, true, false]
54
-
55
- @mstr.read_input_status(0, 3).should == [false, true, false]
56
- end
57
-
58
- it "should have holding registers" do
59
- @sl.holding_registers.should == []
60
- @sl.holding_registers = [0, 0, 0]
61
- @sl.holding_registers.should == [0, 0, 0]
62
-
63
- @mstr.read_holding_registers(0, 3).should == [0, 0, 0]
64
- @mstr.preset_single_register(1, 10000)
65
-
66
- @sl.holding_registers.should == [0, 10000, 0]
67
-
68
- @mstr.preset_multiple_registers(0, [1, 2, 3])
69
-
70
- @sl.holding_registers.should == [1, 2, 3]
71
- @sl.holding_registers[2] = 55
72
- @sl.holding_registers.should == [1, 2, 55]
73
-
74
- @mstr.read_holding_registers(0, 3) == [1, 2, 55]
75
- end
76
-
77
- it "should have input registers" do
78
- @sl.input_registers.should == []
79
- @sl.input_registers = [0, 0, 0]
80
- @sl.input_registers.should == [0, 0, 0]
81
-
82
- @mstr.read_input_registers(0, 3).should == [0, 0, 0]
83
-
84
- @sl.input_registers[2] = 55
85
- @sl.input_registers.should == [0, 0, 55]
86
-
87
- @mstr.read_input_registers(0, 3) == [1, 2, 55]
88
- end
89
-
90
- after(:each) do
91
- @sl.stop unless @sl.stoped?
92
- @mstr.close unless @mstr.closed?
93
- end
94
-
95
- end