Skip to content

Commit ff45c2a

Browse files
committed
Done many little things
1 parent 31921eb commit ff45c2a

12 files changed

+98
-74
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
--color
2+
-f d

lib/rmodbus/options.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# RModBus - free implementation of ModBus protocol in Ruby.
2+
#
3+
# Copyright (C) 2011 Timin Aleksey
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
15+
module ModBus
16+
module Options
17+
attr_accessor :raise_exception_on_mismatch,
18+
:read_retries, :read_retry_timeout
19+
end
20+
end
21+

lib/rmodbus/rtu.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def read_rtu_response(io)
2323
msg = nil
2424
while msg.nil?
2525
msg = io.read(2)
26+
sleep(0.01)
2627
end
2728

2829
function_code = msg.getbyte(1)
@@ -44,6 +45,32 @@ def read_rtu_response(io)
4445
end
4546
end
4647

48+
def send_rtu_pdu(pdu)
49+
msg = @uid.chr + pdu
50+
msg << crc16(msg).to_word
51+
@io.write msg
52+
53+
log "Tx (#{msg.size} bytes): " + logging_bytes(msg)
54+
end
55+
56+
def read_rtu_pdu
57+
msg = read_rtu_response(@io)
58+
59+
log "Rx (#{msg.size} bytes): " + logging_bytes(msg)
60+
61+
if msg.getbyte(0) == @uid
62+
return msg[1..-3] if msg[-2,2].unpack('n')[0] == crc16(msg[0..-3])
63+
log "Ignore package: don't match CRC"
64+
else
65+
log "Ignore package: don't match uid ID"
66+
end
67+
loop do
68+
#waite timeout
69+
sleep(0.1)
70+
end
71+
end
72+
73+
4774
def read_rtu_request(io)
4875
# Read the slave_id and function code
4976
msg = io.read(2)
@@ -83,6 +110,7 @@ def serv_rtu_requests(io, &blk)
83110
log "Server TX (#{resp.size} bytes): #{logging_bytes(resp)}"
84111
io.write resp
85112
end
113+
sleep(0.01)
86114
end
87115
end
88116

lib/rmodbus/rtu_slave.rb

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,17 @@ module ModBus
2626
class RTUSlave < Slave
2727
include RTU
2828

29-
protected
30-
29+
private
3130
# overide method for RTU implamentaion
3231
# @see Slave#query
3332
def send_pdu(pdu)
34-
msg = @uid.chr + pdu
35-
msg << crc16(msg).to_word
36-
@io.write msg
37-
38-
log "Tx (#{msg.size} bytes): " + logging_bytes(msg)
33+
send_rtu_pdu(pdu)
3934
end
4035

4136
# overide method for RTU implamentaion
4237
# @see Slave#query
4338
def read_pdu
44-
msg = read_rtu_response(@io)
45-
46-
log "Rx (#{msg.size} bytes): " + logging_bytes(msg)
47-
48-
if msg.getbyte(0) == @uid
49-
return msg[1..-3] if msg[-2,2].unpack('n')[0] == crc16(msg[0..-3])
50-
log "Ignore package: don't match CRC"
51-
else
52-
log "Ignore package: don't match uid ID"
53-
end
54-
loop do
55-
#waite timeout
56-
end
39+
read_rtu_pdu
5740
end
5841
end
5942
end

lib/rmodbus/rtu_via_tcp_slave.rb

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,17 @@ module ModBus
2626
class RTUViaTCPSlave < Slave
2727
include RTU
2828

29-
protected
30-
# overide method for RTU over TCP implamentaion
29+
private
30+
# overide method for RTU implamentaion
3131
# @see Slave#query
32-
def send_pdu(pdu)
33-
msg = @uid.chr + pdu
34-
msg << crc16(msg).to_word
35-
@io.write msg
32+
def send_pdu(pdu)
33+
send_rtu_pdu(pdu)
34+
end
3635

37-
log "Tx (#{msg.size} bytes): " + logging_bytes(msg)
38-
end
39-
40-
# overide method for RTU over TCP implamentaion
36+
# overide method for RTU implamentaion
4137
# @see Slave#query
42-
def read_pdu
43-
# Read the response appropriately
44-
msg = read_rtu_response(@io)
45-
46-
log "Rx (#{msg.size} bytes): " + logging_bytes(msg)
47-
if msg.getbyte(0) == @uid
48-
return msg[1..-3] if msg[-2,2].unpack('n')[0] == crc16(msg[0..-3])
49-
log "Ignore package: don't match CRC"
50-
else
51-
log "Ignore package: don't match uid ID"
52-
end
53-
loop do
54-
#waite timeout
55-
end
56-
end
38+
def read_pdu
39+
read_rtu_pdu
40+
end
5741
end
5842
end

lib/rmodbus/slave.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def check_response_mismatch(request, response)
273273
read_func = response.getbyte(0)
274274
data = response[2..-1]
275275
#Mismatch functional code
276-
send_func = request[0]
276+
send_func = request.getbyte(0)
277277
if read_func != send_func
278278
msg = "Function code is mismatch (expected #{send_func}, got #{read_func})"
279279
end

lib/rmodbus/tcp_server.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,21 @@ def initialize(port = 502, uid = 1, opts = {})
4343
# Serve requests
4444
# @param [TCPSocket] io socket
4545
def serve(io)
46-
loop do
46+
while not stopped?
4747
req = io.read(7)
48-
if req[2,2] != "\x00\x00" or req.getbyte(6) != @uid
49-
io.close
50-
break
51-
end
48+
if req[2,2] == "\x00\x00" or req.getbyte(6) == @uid
49+
tr = req[0,2]
50+
len = req[4,2].unpack('n')[0]
51+
req = io.read(len - 1)
52+
log "Server RX (#{req.size} bytes): #{logging_bytes(req)}"
5253

53-
tr = req[0,2]
54-
len = req[4,2].unpack('n')[0]
55-
req = io.read(len - 1)
56-
log "Server RX (#{req.size} bytes): #{logging_bytes(req)}"
54+
pdu = exec_req(req, @coils, @discrete_inputs, @holding_registers, @input_registers)
5755

58-
pdu = exec_req(req, @coils, @discrete_inputs, @holding_registers, @input_registers)
59-
60-
resp = tr + "\0\0" + (pdu.size + 1).to_word + @uid.chr + pdu
61-
log "Server TX (#{resp.size} bytes): #{logging_bytes(resp)}"
62-
io.write resp
56+
resp = tr + "\0\0" + (pdu.size + 1).to_word + @uid.chr + pdu
57+
log "Server TX (#{resp.size} bytes): #{logging_bytes(resp)}"
58+
io.write resp
59+
end
60+
sleep(0.01)
6361
end
6462
end
6563
end

rmodbus.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ Gem::Specification.new do |gem|
2020
gem.add_development_dependency 'rcov'
2121
gem.add_development_dependency 'yard'
2222
gem.add_development_dependency 'rdiscount'
23-
gem.add_development_dependency 'serialport'
23+
gem.add_development_dependency 'pry'
24+
gem.add_development_dependency 'serialport' unless RUBY_PLATFORM == 'java'
2425
end

spec/rtu_client_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@cl = RTUClient.new("/dev/port1", 9600, :data_bits => 8, :stop_bits => 1, :parity => SerialPort::NONE)
1212
@slave = @cl.with_slave(1)
13-
@slave.read_retries = 0
13+
@slave.read_retries = 1
1414
end
1515

1616
it "should ignore frame with other UID" do

spec/rtu_via_tcp_client_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@cl = RTUViaTCPClient.new("127.0.0.1")
1313
@slave = @cl.with_slave(1)
14-
@slave.read_retries = 0
14+
@slave.read_retries = 1
1515
end
1616

1717
it "should ignore frame with other UID" do

0 commit comments

Comments
 (0)