Class: ModBus::TCPServer

Inherits:
GServer
  • Object
show all
Defined in:
lib/rmodbus/tcp_server.rb

Constant Summary collapse

@@funcs =
[1,2,3,4,5,6,15,16]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = 502, uid = 1) ⇒ TCPServer

Returns a new instance of TCPServer.



26
27
28
29
30
31
32
33
# File 'lib/rmodbus/tcp_server.rb', line 26

def initialize(port = 502, uid = 1)
  @coils = []
  @discret_inputs = []
  @holding_registers =[]
  @input_registers = []
  @uid = uid
  super(port)
end

Instance Attribute Details

#coilsObject

Returns the value of attribute coils.



22
23
24
# File 'lib/rmodbus/tcp_server.rb', line 22

def coils
  @coils
end

#discret_inputsObject

Returns the value of attribute discret_inputs.



22
23
24
# File 'lib/rmodbus/tcp_server.rb', line 22

def discret_inputs
  @discret_inputs
end

#holding_registersObject

Returns the value of attribute holding_registers.



22
23
24
# File 'lib/rmodbus/tcp_server.rb', line 22

def holding_registers
  @holding_registers
end

#input_registersObject

Returns the value of attribute input_registers.



22
23
24
# File 'lib/rmodbus/tcp_server.rb', line 22

def input_registers
  @input_registers
end

Instance Method Details

#serve(io) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rmodbus/tcp_server.rb', line 35

def serve(io)
  req = io.read(7)
  if req[2,2] != "\x00\x00" or req[6].to_i != @uid
    io.close
    return
  end

  tr = req[0,2]
  len = req[4,2].to_int16
  req = io.read(len - 1)
  func = req[0].to_i

  unless @@funcs.include?(func)
    param = { :err => 1 }
  end
  
  case func
    when 1
      param = parse_read_func(req, @coils)
      if param[:err] == 0
        val = @coils[param[:addr],param[:quant]].bits_to_bytes
        res = func.chr + val.size.chr + val
      end
    when 2
      param = parse_read_func(req, @discret_inputs)
      if param[:err] == 0
        val = @discret_inputs[param[:addr],param[:quant]].bits_to_bytes
        res = func.chr + val.size.chr + val
      end
    when 3
      param = parse_read_func(req, @holding_registers)
      if param[:err] == 0
        res = func.chr + (param[:quant] * 2).chr + @holding_registers[param[:addr],param[:quant]].to_ints16
      end
    when 4
      param = parse_read_func(req, @input_registers)
      if param[:err] == 0
        res = func.chr + (param[:quant] * 2).chr + @input_registers[param[:addr],param[:quant]].to_ints16
      end
    when 5 
      param = parse_write_coil_func(req)
      if param[:err] == 0
        @coils[param[:addr]] = param[:val]
        res = func.chr + req
      end
    when 6
      param = parse_write_register_func(req)
      if param[:err] == 0
        @holding_registers[param[:addr]] = param[:val]
        res = func.chr + req
      end
    when 15
      param = parse_write_multiple_coils_func(req)
      if param[:err] == 0
        @coils[param[:addr],param[:quant]] = param[:val][0,param[:quant]]
        res = func.chr + req
      end
    when 16
      param = parse_write_multiple_registers_func(req)
      if param[:err] == 0
        @holding_registers[param[:addr],param[:quant]] = param[:val][0,param[:quant]]
        res = func.chr + req
      end
  end
  if param[:err] ==  0
    io.write tr + "\0\0" + (res.size + 1).to_bytes + @uid.chr + res
  else
    io.write tr + "\0\0\0\3" + @uid.chr + (func | 0x80).chr + param[:err].chr
  end 
end