-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodbus.rb
75 lines (62 loc) · 1.47 KB
/
modbus.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# rmodbus
# digest
# digest-crc
# serialport
require "digest/crc16_modbus"
require "rmodbus"
require "serialport"
require "time"
def read(address, count)
ext = nil
ModBus::RTUClient.connect('/dev/ttyUSB0', 9600, :data_bits => 8, :stop_bits => 1, :parity => SerialPort::EVEN) do |cl|
cl.with_slave(1) do |slave|
ext = slave.read_holding_registers(address, count).pack("n*")
end
end
ext
end
def write(address, byte_string)
ext = nil
ModBus::RTUClient.connect('/dev/ttyUSB0', 9600, :data_bits => 8, :stop_bits => 1, :parity => SerialPort::EVEN) do |cl|
cl.with_slave(1) do |slave|
ext = slave.write_multiple_registers(address, byte_string.unpack("n*"))
end
end
ext
end
def decode_time(time_string)
tm = time_string.bytes.map { |b| '%2.2x' % b }.reverse.join
tm = tm[2..7]+tm[10..]
Time.strptime(tm, "%y%m%d%H%M%S")
end
def encode_time(time)
s = time.strftime("00%y%m%d0%u%H%M%S")
8.times.map { |i| s[2*i..2*i+1].to_i(16) }.reverse.pack("C*")
end
EMPTY_DATA = [
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0
].pack("C*")
TEMPO_DATA = [
0x06, 0, 1, # HH:MM Tariff
0x22, 0, 2,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0
].pack("C*")
TIME_INTERVALS = [0x300, 0x30C, 0x318, 0x324, 0x330, 0x33C, 0x348, 0x354, 0x360]
#write(0x300, TEMPO_DATA)
#9.times do |i|
# STDOUT.write read(0x300+i*12, 12)
#end
#STDOUT.write read(0x100, (0x15E-0x100)+2)
#STDOUT.write read(0x0, 0x41+1)