mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
#47 RTU luadoc, bugfixes
This commit is contained in:
parent
5ad14205f3
commit
c6987f6f67
@ -7,14 +7,22 @@ local MODBUS_FCODE = types.MODBUS_FCODE
|
|||||||
local MODBUS_EXCODE = types.MODBUS_EXCODE
|
local MODBUS_EXCODE = types.MODBUS_EXCODE
|
||||||
|
|
||||||
-- new modbus comms handler object
|
-- new modbus comms handler object
|
||||||
|
---@param rtu_dev rtu RTU device
|
||||||
|
---@param use_parallel_read boolean whether or not to use parallel calls when reading
|
||||||
modbus.new = function (rtu_dev, use_parallel_read)
|
modbus.new = function (rtu_dev, use_parallel_read)
|
||||||
local self = {
|
local self = {
|
||||||
rtu = rtu_dev,
|
rtu = rtu_dev,
|
||||||
use_parallel = use_parallel_read
|
use_parallel = use_parallel_read
|
||||||
}
|
}
|
||||||
|
|
||||||
|
---@class modbus
|
||||||
|
local public = {}
|
||||||
|
|
||||||
local insert = table.insert
|
local insert = table.insert
|
||||||
|
|
||||||
|
---@param c_addr_start integer
|
||||||
|
---@param count integer
|
||||||
|
---@return boolean ok, table readings
|
||||||
local _1_read_coils = function (c_addr_start, count)
|
local _1_read_coils = function (c_addr_start, count)
|
||||||
local tasks = {}
|
local tasks = {}
|
||||||
local readings = {}
|
local readings = {}
|
||||||
@ -58,6 +66,9 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
return return_ok, readings
|
return return_ok, readings
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param di_addr_start integer
|
||||||
|
---@param count integer
|
||||||
|
---@return boolean ok, table readings
|
||||||
local _2_read_discrete_inputs = function (di_addr_start, count)
|
local _2_read_discrete_inputs = function (di_addr_start, count)
|
||||||
local tasks = {}
|
local tasks = {}
|
||||||
local readings = {}
|
local readings = {}
|
||||||
@ -101,6 +112,9 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
return return_ok, readings
|
return return_ok, readings
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param hr_addr_start integer
|
||||||
|
---@param count integer
|
||||||
|
---@return boolean ok, table readings
|
||||||
local _3_read_multiple_holding_registers = function (hr_addr_start, count)
|
local _3_read_multiple_holding_registers = function (hr_addr_start, count)
|
||||||
local tasks = {}
|
local tasks = {}
|
||||||
local readings = {}
|
local readings = {}
|
||||||
@ -144,6 +158,9 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
return return_ok, readings
|
return return_ok, readings
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param ir_addr_start integer
|
||||||
|
---@param count integer
|
||||||
|
---@return boolean ok, table readings
|
||||||
local _4_read_input_registers = function (ir_addr_start, count)
|
local _4_read_input_registers = function (ir_addr_start, count)
|
||||||
local tasks = {}
|
local tasks = {}
|
||||||
local readings = {}
|
local readings = {}
|
||||||
@ -187,6 +204,9 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
return return_ok, readings
|
return return_ok, readings
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param c_addr integer
|
||||||
|
---@param value any
|
||||||
|
---@return boolean ok, MODBUS_EXCODE|nil
|
||||||
local _5_write_single_coil = function (c_addr, value)
|
local _5_write_single_coil = function (c_addr, value)
|
||||||
local response = nil
|
local response = nil
|
||||||
local _, coils, _, _ = self.rtu.io_count()
|
local _, coils, _, _ = self.rtu.io_count()
|
||||||
@ -206,6 +226,9 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
return return_ok, response
|
return return_ok, response
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param hr_addr integer
|
||||||
|
---@param value any
|
||||||
|
---@return boolean ok, MODBUS_EXCODE|nil
|
||||||
local _6_write_single_holding_register = function (hr_addr, value)
|
local _6_write_single_holding_register = function (hr_addr, value)
|
||||||
local response = nil
|
local response = nil
|
||||||
local _, _, _, hold_regs = self.rtu.io_count()
|
local _, _, _, hold_regs = self.rtu.io_count()
|
||||||
@ -222,9 +245,12 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
response = MODBUS_EXCODE.ILLEGAL_DATA_ADDR
|
response = MODBUS_EXCODE.ILLEGAL_DATA_ADDR
|
||||||
end
|
end
|
||||||
|
|
||||||
return return_ok
|
return return_ok, response
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param c_addr_start integer
|
||||||
|
---@param values any
|
||||||
|
---@return boolean ok, MODBUS_EXCODE|nil
|
||||||
local _15_write_multiple_coils = function (c_addr_start, values)
|
local _15_write_multiple_coils = function (c_addr_start, values)
|
||||||
local response = nil
|
local response = nil
|
||||||
local _, coils, _, _ = self.rtu.io_count()
|
local _, coils, _, _ = self.rtu.io_count()
|
||||||
@ -249,6 +275,9 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
return return_ok, response
|
return return_ok, response
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param hr_addr_start integer
|
||||||
|
---@param values any
|
||||||
|
---@return boolean ok, MODBUS_EXCODE|nil
|
||||||
local _16_write_multiple_holding_registers = function (hr_addr_start, values)
|
local _16_write_multiple_holding_registers = function (hr_addr_start, values)
|
||||||
local response = nil
|
local response = nil
|
||||||
local _, _, _, hold_regs = self.rtu.io_count()
|
local _, _, _, hold_regs = self.rtu.io_count()
|
||||||
@ -274,7 +303,9 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- validate a request without actually executing it
|
-- validate a request without actually executing it
|
||||||
local check_request = function (packet)
|
---@param packet modbus_frame
|
||||||
|
---@return boolean return_code, modbus_packet reply
|
||||||
|
public.check_request = function (packet)
|
||||||
local return_code = true
|
local return_code = true
|
||||||
local response = { MODBUS_EXCODE.ACKNOWLEDGE }
|
local response = { MODBUS_EXCODE.ACKNOWLEDGE }
|
||||||
|
|
||||||
@ -314,7 +345,9 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- handle a MODBUS TCP packet and generate a reply
|
-- handle a MODBUS TCP packet and generate a reply
|
||||||
local handle_packet = function (packet)
|
---@param packet modbus_frame
|
||||||
|
---@return boolean return_code, modbus_packet reply
|
||||||
|
public.handle_packet = function (packet)
|
||||||
local return_code = true
|
local return_code = true
|
||||||
local response = nil
|
local response = nil
|
||||||
|
|
||||||
@ -369,7 +402,8 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- return a SERVER_DEVICE_BUSY error reply
|
-- return a SERVER_DEVICE_BUSY error reply
|
||||||
local reply__srv_device_busy = function (packet)
|
---@return modbus_packet reply
|
||||||
|
public.reply__srv_device_busy = function (packet)
|
||||||
-- reply back with error flag and exception code
|
-- reply back with error flag and exception code
|
||||||
local reply = comms.modbus_packet()
|
local reply = comms.modbus_packet()
|
||||||
local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG)
|
local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG)
|
||||||
@ -379,7 +413,8 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- return a NEG_ACKNOWLEDGE error reply
|
-- return a NEG_ACKNOWLEDGE error reply
|
||||||
local reply__neg_ack = function (packet)
|
---@return modbus_packet reply
|
||||||
|
public.reply__neg_ack = function (packet)
|
||||||
-- reply back with error flag and exception code
|
-- reply back with error flag and exception code
|
||||||
local reply = comms.modbus_packet()
|
local reply = comms.modbus_packet()
|
||||||
local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG)
|
local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG)
|
||||||
@ -389,7 +424,8 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- return a GATEWAY_PATH_UNAVAILABLE error reply
|
-- return a GATEWAY_PATH_UNAVAILABLE error reply
|
||||||
local reply__gw_unavailable = function (packet)
|
---@return modbus_packet reply
|
||||||
|
public.reply__gw_unavailable = function (packet)
|
||||||
-- reply back with error flag and exception code
|
-- reply back with error flag and exception code
|
||||||
local reply = comms.modbus_packet()
|
local reply = comms.modbus_packet()
|
||||||
local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG)
|
local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG)
|
||||||
@ -398,13 +434,7 @@ modbus.new = function (rtu_dev, use_parallel_read)
|
|||||||
return reply
|
return reply
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return public
|
||||||
check_request = check_request,
|
|
||||||
handle_packet = handle_packet,
|
|
||||||
reply__srv_device_busy = reply__srv_device_busy,
|
|
||||||
reply__neg_ack = reply__neg_ack,
|
|
||||||
reply__gw_unavailable = reply__gw_unavailable
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return modbus
|
return modbus
|
||||||
|
183
rtu/rtu.lua
183
rtu/rtu.lua
@ -19,6 +19,7 @@ local println = util.println
|
|||||||
local print_ts = util.print_ts
|
local print_ts = util.print_ts
|
||||||
local println_ts = util.println_ts
|
local println_ts = util.println_ts
|
||||||
|
|
||||||
|
-- create a new RTU
|
||||||
rtu.init_unit = function ()
|
rtu.init_unit = function ()
|
||||||
local self = {
|
local self = {
|
||||||
discrete_inputs = {},
|
discrete_inputs = {},
|
||||||
@ -28,28 +29,36 @@ rtu.init_unit = function ()
|
|||||||
io_count_cache = { 0, 0, 0, 0 }
|
io_count_cache = { 0, 0, 0, 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
---@class rtu
|
||||||
|
local public = {}
|
||||||
|
|
||||||
local insert = table.insert
|
local insert = table.insert
|
||||||
|
|
||||||
local _count_io = function ()
|
local _count_io = function ()
|
||||||
self.io_count_cache = { #self.discrete_inputs, #self.coils, #self.input_regs, #self.holding_regs }
|
self.io_count_cache = { #self.discrete_inputs, #self.coils, #self.input_regs, #self.holding_regs }
|
||||||
end
|
end
|
||||||
|
|
||||||
-- return : IO count table
|
-- return IO counts
|
||||||
local io_count = function ()
|
---@return integer discrete_inputs, integer coils, integer input_regs, integer holding_regs
|
||||||
|
public.io_count = function ()
|
||||||
return self.io_count_cache[0], self.io_count_cache[1], self.io_count_cache[2], self.io_count_cache[3]
|
return self.io_count_cache[0], self.io_count_cache[1], self.io_count_cache[2], self.io_count_cache[3]
|
||||||
end
|
end
|
||||||
|
|
||||||
-- discrete inputs: single bit read-only
|
-- discrete inputs: single bit read-only
|
||||||
|
|
||||||
-- return : count of discrete inputs
|
-- connect discrete input
|
||||||
local connect_di = function (f)
|
---@param f function
|
||||||
|
---@return integer count count of discrete inputs
|
||||||
|
public.connect_di = function (f)
|
||||||
insert(self.discrete_inputs, f)
|
insert(self.discrete_inputs, f)
|
||||||
_count_io()
|
_count_io()
|
||||||
return #self.discrete_inputs
|
return #self.discrete_inputs
|
||||||
end
|
end
|
||||||
|
|
||||||
-- return : value, access fault
|
-- read discrete input
|
||||||
local read_di = function (di_addr)
|
---@param di_addr integer
|
||||||
|
---@return any value, boolean access_fault
|
||||||
|
public.read_di = function (di_addr)
|
||||||
ppm.clear_fault()
|
ppm.clear_fault()
|
||||||
local value = self.discrete_inputs[di_addr]()
|
local value = self.discrete_inputs[di_addr]()
|
||||||
return value, ppm.is_faulted()
|
return value, ppm.is_faulted()
|
||||||
@ -57,22 +66,30 @@ rtu.init_unit = function ()
|
|||||||
|
|
||||||
-- coils: single bit read-write
|
-- coils: single bit read-write
|
||||||
|
|
||||||
-- return : count of coils
|
-- connect coil
|
||||||
local connect_coil = function (f_read, f_write)
|
---@param f_read function
|
||||||
|
---@param f_write function
|
||||||
|
---@return integer count count of coils
|
||||||
|
public.connect_coil = function (f_read, f_write)
|
||||||
insert(self.coils, { read = f_read, write = f_write })
|
insert(self.coils, { read = f_read, write = f_write })
|
||||||
_count_io()
|
_count_io()
|
||||||
return #self.coils
|
return #self.coils
|
||||||
end
|
end
|
||||||
|
|
||||||
-- return : value, access fault
|
-- read coil
|
||||||
local read_coil = function (coil_addr)
|
---@param coil_addr integer
|
||||||
|
---@return any value, boolean access_fault
|
||||||
|
public.read_coil = function (coil_addr)
|
||||||
ppm.clear_fault()
|
ppm.clear_fault()
|
||||||
local value = self.coils[coil_addr].read()
|
local value = self.coils[coil_addr].read()
|
||||||
return value, ppm.is_faulted()
|
return value, ppm.is_faulted()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- return : access fault
|
-- write coil
|
||||||
local write_coil = function (coil_addr, value)
|
---@param coil_addr integer
|
||||||
|
---@param value any
|
||||||
|
---@return boolean access_fault
|
||||||
|
public.write_coil = function (coil_addr, value)
|
||||||
ppm.clear_fault()
|
ppm.clear_fault()
|
||||||
self.coils[coil_addr].write(value)
|
self.coils[coil_addr].write(value)
|
||||||
return ppm.is_faulted()
|
return ppm.is_faulted()
|
||||||
@ -80,15 +97,19 @@ rtu.init_unit = function ()
|
|||||||
|
|
||||||
-- input registers: multi-bit read-only
|
-- input registers: multi-bit read-only
|
||||||
|
|
||||||
-- return : count of input registers
|
-- connect input register
|
||||||
local connect_input_reg = function (f)
|
---@param f function
|
||||||
|
---@return integer count count of input registers
|
||||||
|
public.connect_input_reg = function (f)
|
||||||
insert(self.input_regs, f)
|
insert(self.input_regs, f)
|
||||||
_count_io()
|
_count_io()
|
||||||
return #self.input_regs
|
return #self.input_regs
|
||||||
end
|
end
|
||||||
|
|
||||||
-- return : value, access fault
|
-- read input register
|
||||||
local read_input_reg = function (reg_addr)
|
---@param reg_addr integer
|
||||||
|
---@return any value, boolean access_fault
|
||||||
|
public.read_input_reg = function (reg_addr)
|
||||||
ppm.clear_fault()
|
ppm.clear_fault()
|
||||||
local value = self.coils[reg_addr]()
|
local value = self.coils[reg_addr]()
|
||||||
return value, ppm.is_faulted()
|
return value, ppm.is_faulted()
|
||||||
@ -96,42 +117,43 @@ rtu.init_unit = function ()
|
|||||||
|
|
||||||
-- holding registers: multi-bit read-write
|
-- holding registers: multi-bit read-write
|
||||||
|
|
||||||
-- return : count of holding registers
|
-- connect holding register
|
||||||
local connect_holding_reg = function (f_read, f_write)
|
---@param f_read function
|
||||||
|
---@param f_write function
|
||||||
|
---@return integer count count of holding registers
|
||||||
|
public.connect_holding_reg = function (f_read, f_write)
|
||||||
insert(self.holding_regs, { read = f_read, write = f_write })
|
insert(self.holding_regs, { read = f_read, write = f_write })
|
||||||
_count_io()
|
_count_io()
|
||||||
return #self.holding_regs
|
return #self.holding_regs
|
||||||
end
|
end
|
||||||
|
|
||||||
-- return : value, access fault
|
-- read holding register
|
||||||
local read_holding_reg = function (reg_addr)
|
---@param reg_addr integer
|
||||||
|
---@return any value, boolean access_fault
|
||||||
|
public.read_holding_reg = function (reg_addr)
|
||||||
ppm.clear_fault()
|
ppm.clear_fault()
|
||||||
local value = self.coils[reg_addr].read()
|
local value = self.coils[reg_addr].read()
|
||||||
return value, ppm.is_faulted()
|
return value, ppm.is_faulted()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- return : access fault
|
-- write holding register
|
||||||
local write_holding_reg = function (reg_addr, value)
|
---@param reg_addr integer
|
||||||
|
---@param value any
|
||||||
|
---@return boolean access_fault
|
||||||
|
public.write_holding_reg = function (reg_addr, value)
|
||||||
ppm.clear_fault()
|
ppm.clear_fault()
|
||||||
self.coils[reg_addr].write(value)
|
self.coils[reg_addr].write(value)
|
||||||
return ppm.is_faulted()
|
return ppm.is_faulted()
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return public
|
||||||
io_count = io_count,
|
|
||||||
connect_di = connect_di,
|
|
||||||
read_di = read_di,
|
|
||||||
connect_coil = connect_coil,
|
|
||||||
read_coil = read_coil,
|
|
||||||
write_coil = write_coil,
|
|
||||||
connect_input_reg = connect_input_reg,
|
|
||||||
read_input_reg = read_input_reg,
|
|
||||||
connect_holding_reg = connect_holding_reg,
|
|
||||||
read_holding_reg = read_holding_reg,
|
|
||||||
write_holding_reg = write_holding_reg
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- RTU Communications
|
||||||
|
---@param modem table
|
||||||
|
---@param local_port integer
|
||||||
|
---@param server_port integer
|
||||||
|
---@param conn_watchdog watchdog
|
||||||
rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
||||||
local self = {
|
local self = {
|
||||||
seq_num = 0,
|
seq_num = 0,
|
||||||
@ -143,6 +165,9 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
conn_watchdog = conn_watchdog
|
conn_watchdog = conn_watchdog
|
||||||
}
|
}
|
||||||
|
|
||||||
|
---@class rtu_comms
|
||||||
|
local public = {}
|
||||||
|
|
||||||
local insert = table.insert
|
local insert = table.insert
|
||||||
|
|
||||||
-- open modem
|
-- open modem
|
||||||
@ -152,6 +177,9 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
|
|
||||||
-- PRIVATE FUNCTIONS --
|
-- PRIVATE FUNCTIONS --
|
||||||
|
|
||||||
|
-- send a scada management packet
|
||||||
|
---@param msg_type SCADA_MGMT_TYPES
|
||||||
|
---@param msg any
|
||||||
local _send = function (msg_type, msg)
|
local _send = function (msg_type, msg)
|
||||||
local s_pkt = comms.scada_packet()
|
local s_pkt = comms.scada_packet()
|
||||||
local m_pkt = comms.mgmt_packet()
|
local m_pkt = comms.mgmt_packet()
|
||||||
@ -164,6 +192,7 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- keep alive ack
|
-- keep alive ack
|
||||||
|
---@param srv_time integer
|
||||||
local _send_keep_alive_ack = function (srv_time)
|
local _send_keep_alive_ack = function (srv_time)
|
||||||
_send(SCADA_MGMT_TYPES.KEEP_ALIVE, { srv_time, util.time() })
|
_send(SCADA_MGMT_TYPES.KEEP_ALIVE, { srv_time, util.time() })
|
||||||
end
|
end
|
||||||
@ -171,7 +200,8 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
-- PUBLIC FUNCTIONS --
|
-- PUBLIC FUNCTIONS --
|
||||||
|
|
||||||
-- send a MODBUS TCP packet
|
-- send a MODBUS TCP packet
|
||||||
local send_modbus = function (m_pkt)
|
---@param m_pkt modbus_packet
|
||||||
|
public.send_modbus = function (m_pkt)
|
||||||
local s_pkt = comms.scada_packet()
|
local s_pkt = comms.scada_packet()
|
||||||
s_pkt.make(self.seq_num, PROTOCOLS.MODBUS_TCP, m_pkt.raw_sendable())
|
s_pkt.make(self.seq_num, PROTOCOLS.MODBUS_TCP, m_pkt.raw_sendable())
|
||||||
self.modem.transmit(self.s_port, self.l_port, s_pkt.raw_sendable())
|
self.modem.transmit(self.s_port, self.l_port, s_pkt.raw_sendable())
|
||||||
@ -179,7 +209,9 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- reconnect a newly connected modem
|
-- reconnect a newly connected modem
|
||||||
local reconnect_modem = function (modem)
|
---@param modem table
|
||||||
|
---@diagnostic disable-next-line: redefined-local
|
||||||
|
public.reconnect_modem = function (modem)
|
||||||
self.modem = modem
|
self.modem = modem
|
||||||
|
|
||||||
-- open modem
|
-- open modem
|
||||||
@ -189,42 +221,43 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- unlink from the server
|
-- unlink from the server
|
||||||
local unlink = function (rtu_state)
|
---@param rtu_state rtu_state
|
||||||
|
public.unlink = function (rtu_state)
|
||||||
rtu_state.linked = false
|
rtu_state.linked = false
|
||||||
self.r_seq_num = nil
|
self.r_seq_num = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- close the connection to the server
|
-- close the connection to the server
|
||||||
local close = function (rtu_state)
|
---@param rtu_state rtu_state
|
||||||
|
public.close = function (rtu_state)
|
||||||
self.conn_watchdog.cancel()
|
self.conn_watchdog.cancel()
|
||||||
unlink(rtu_state)
|
public.unlink(rtu_state)
|
||||||
_send(SCADA_MGMT_TYPES.CLOSE, {})
|
_send(SCADA_MGMT_TYPES.CLOSE, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- send capability advertisement
|
-- send capability advertisement
|
||||||
local send_advertisement = function (units)
|
---@param units table
|
||||||
|
public.send_advertisement = function (units)
|
||||||
local advertisement = {}
|
local advertisement = {}
|
||||||
|
|
||||||
for i = 1, #units do
|
for i = 1, #units do
|
||||||
local unit = units[i]
|
local unit = units[i] ---@type rtu_unit_registry_entry
|
||||||
local type = comms.rtu_t_to_advert_type(unit.type)
|
local type = comms.rtu_t_to_advert_type(unit.type)
|
||||||
|
|
||||||
if type ~= nil then
|
if type ~= nil then
|
||||||
|
---@class rtu_advertisement
|
||||||
|
local advert = {
|
||||||
|
type = type, ---@type integer
|
||||||
|
index = unit.index, ---@type integer
|
||||||
|
reactor = unit.reactor, ---@type integer
|
||||||
|
rsio = nil ---@type table|nil
|
||||||
|
}
|
||||||
|
|
||||||
if type == RTU_ADVERT_TYPES.REDSTONE then
|
if type == RTU_ADVERT_TYPES.REDSTONE then
|
||||||
insert(advertisement, {
|
advert.rsio = unit.device
|
||||||
type = type,
|
|
||||||
index = unit.index,
|
|
||||||
reactor = unit.for_reactor,
|
|
||||||
rsio = unit.device
|
|
||||||
})
|
|
||||||
else
|
|
||||||
insert(advertisement, {
|
|
||||||
type = type,
|
|
||||||
index = unit.index,
|
|
||||||
reactor = unit.for_reactor,
|
|
||||||
rsio = nil
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
insert(advertisement, advert)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -232,7 +265,13 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- parse a MODBUS/SCADA packet
|
-- parse a MODBUS/SCADA packet
|
||||||
local parse_packet = function(side, sender, reply_to, message, distance)
|
---@param side string
|
||||||
|
---@param sender integer
|
||||||
|
---@param reply_to integer
|
||||||
|
---@param message any
|
||||||
|
---@param distance integer
|
||||||
|
---@return modbus_frame|mgmt_frame|nil packet
|
||||||
|
public.parse_packet = function(side, sender, reply_to, message, distance)
|
||||||
local pkt = nil
|
local pkt = nil
|
||||||
local s_pkt = comms.scada_packet()
|
local s_pkt = comms.scada_packet()
|
||||||
|
|
||||||
@ -261,10 +300,11 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- handle a MODBUS/SCADA packet
|
-- handle a MODBUS/SCADA packet
|
||||||
local handle_packet = function(packet, units, rtu_state)
|
---@param packet modbus_frame|mgmt_frame
|
||||||
|
---@param units table
|
||||||
|
---@param rtu_state rtu_state
|
||||||
|
public.handle_packet = function(packet, units, rtu_state)
|
||||||
if packet ~= nil then
|
if packet ~= nil then
|
||||||
local seq_ok = true
|
|
||||||
|
|
||||||
-- check sequence number
|
-- check sequence number
|
||||||
if self.r_seq_num == nil then
|
if self.r_seq_num == nil then
|
||||||
self.r_seq_num = packet.scada_frame.seq_num()
|
self.r_seq_num = packet.scada_frame.seq_num()
|
||||||
@ -281,26 +321,27 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
local protocol = packet.scada_frame.protocol()
|
local protocol = packet.scada_frame.protocol()
|
||||||
|
|
||||||
if protocol == PROTOCOLS.MODBUS_TCP then
|
if protocol == PROTOCOLS.MODBUS_TCP then
|
||||||
|
local return_code = false
|
||||||
local reply = modbus.reply__neg_ack(packet)
|
local reply = modbus.reply__neg_ack(packet)
|
||||||
|
|
||||||
-- handle MODBUS instruction
|
-- handle MODBUS instruction
|
||||||
if packet.unit_id <= #units then
|
if packet.unit_id <= #units then
|
||||||
local unit = units[packet.unit_id]
|
local unit = units[packet.unit_id] ---@type rtu_unit_registry_entry
|
||||||
if unit.name == "redstone_io" then
|
if unit.name == "redstone_io" then
|
||||||
-- immediately execute redstone RTU requests
|
-- immediately execute redstone RTU requests
|
||||||
local return_code, reply = unit.modbus_io.handle_packet(packet)
|
return_code, reply = unit.modbus_io.handle_packet(packet)
|
||||||
if not return_code then
|
if not return_code then
|
||||||
log.warning("requested MODBUS operation failed")
|
log.warning("requested MODBUS operation failed")
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- check validity then pass off to unit comms thread
|
-- check validity then pass off to unit comms thread
|
||||||
local return_code, reply = unit.modbus_io.check_request(packet)
|
return_code, reply = unit.modbus_io.check_request(packet)
|
||||||
if return_code then
|
if return_code then
|
||||||
-- check if an operation is already in progress for this unit
|
-- check if an operation is already in progress for this unit
|
||||||
if unit.modbus_busy then
|
if unit.modbus_busy then
|
||||||
reply = unit.modbus_io.reply__srv_device_busy(packet)
|
reply = unit.modbus_io.reply__srv_device_busy(packet)
|
||||||
else
|
else
|
||||||
unit.pkt_queue.push(packet)
|
unit.pkt_queue.push_packet(packet)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
log.warning("cannot perform requested MODBUS operation")
|
log.warning("cannot perform requested MODBUS operation")
|
||||||
@ -312,7 +353,7 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
log.error("MODBUS packet requesting non-existent unit")
|
log.error("MODBUS packet requesting non-existent unit")
|
||||||
end
|
end
|
||||||
|
|
||||||
send_modbus(reply)
|
public.send_modbus(reply)
|
||||||
elseif protocol == PROTOCOLS.SCADA_MGMT then
|
elseif protocol == PROTOCOLS.SCADA_MGMT then
|
||||||
-- SCADA management packet
|
-- SCADA management packet
|
||||||
if packet.type == SCADA_MGMT_TYPES.KEEP_ALIVE then
|
if packet.type == SCADA_MGMT_TYPES.KEEP_ALIVE then
|
||||||
@ -334,7 +375,7 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
elseif packet.type == SCADA_MGMT_TYPES.CLOSE then
|
elseif packet.type == SCADA_MGMT_TYPES.CLOSE then
|
||||||
-- close connection
|
-- close connection
|
||||||
self.conn_watchdog.cancel()
|
self.conn_watchdog.cancel()
|
||||||
unlink(rtu_state)
|
public.unlink(rtu_state)
|
||||||
println_ts("server connection closed by remote host")
|
println_ts("server connection closed by remote host")
|
||||||
log.warning("server connection closed by remote host")
|
log.warning("server connection closed by remote host")
|
||||||
elseif packet.type == SCADA_MGMT_TYPES.REMOTE_LINKED then
|
elseif packet.type == SCADA_MGMT_TYPES.REMOTE_LINKED then
|
||||||
@ -343,7 +384,7 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
self.r_seq_num = nil
|
self.r_seq_num = nil
|
||||||
elseif packet.type == SCADA_MGMT_TYPES.RTU_ADVERT then
|
elseif packet.type == SCADA_MGMT_TYPES.RTU_ADVERT then
|
||||||
-- request for capabilities again
|
-- request for capabilities again
|
||||||
send_advertisement(units)
|
public.send_advertisement(units)
|
||||||
else
|
else
|
||||||
-- not supported
|
-- not supported
|
||||||
log.warning("RTU got unexpected SCADA message type " .. packet.type)
|
log.warning("RTU got unexpected SCADA message type " .. packet.type)
|
||||||
@ -355,15 +396,7 @@ rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return public
|
||||||
send_modbus = send_modbus,
|
|
||||||
reconnect_modem = reconnect_modem,
|
|
||||||
parse_packet = parse_packet,
|
|
||||||
handle_packet = handle_packet,
|
|
||||||
send_advertisement = send_advertisement,
|
|
||||||
unlink = unlink,
|
|
||||||
close = close
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return rtu
|
return rtu
|
||||||
|
@ -22,7 +22,7 @@ local imatrix_rtu = require("rtu.dev.imatrix_rtu")
|
|||||||
local turbine_rtu = require("rtu.dev.turbine_rtu")
|
local turbine_rtu = require("rtu.dev.turbine_rtu")
|
||||||
local turbinev_rtu = require("rtu.dev.turbinev_rtu")
|
local turbinev_rtu = require("rtu.dev.turbinev_rtu")
|
||||||
|
|
||||||
local RTU_VERSION = "alpha-v0.6.2"
|
local RTU_VERSION = "alpha-v0.6.3"
|
||||||
|
|
||||||
local rtu_t = types.rtu_t
|
local rtu_t = types.rtu_t
|
||||||
|
|
||||||
@ -45,8 +45,10 @@ println(">> RTU " .. RTU_VERSION .. " <<")
|
|||||||
-- mount connected devices
|
-- mount connected devices
|
||||||
ppm.mount_all()
|
ppm.mount_all()
|
||||||
|
|
||||||
|
---@class rtu_shared_memory
|
||||||
local __shared_memory = {
|
local __shared_memory = {
|
||||||
-- RTU system state flags
|
-- RTU system state flags
|
||||||
|
---@class rtu_state
|
||||||
rtu_state = {
|
rtu_state = {
|
||||||
linked = false,
|
linked = false,
|
||||||
shutdown = false
|
shutdown = false
|
||||||
@ -59,9 +61,9 @@ local __shared_memory = {
|
|||||||
|
|
||||||
-- system objects
|
-- system objects
|
||||||
rtu_sys = {
|
rtu_sys = {
|
||||||
rtu_comms = nil,
|
rtu_comms = nil, ---@type rtu_comms
|
||||||
conn_watchdog = nil,
|
conn_watchdog = nil, ---@type watchdog
|
||||||
units = {}
|
units = {} ---@type table
|
||||||
},
|
},
|
||||||
|
|
||||||
-- message queues
|
-- message queues
|
||||||
@ -140,7 +142,8 @@ for reactor_idx = 1, #rtu_redstone do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(units, {
|
---@class rtu_unit_registry_entry
|
||||||
|
local unit = {
|
||||||
name = "redstone_io",
|
name = "redstone_io",
|
||||||
type = rtu_t.redstone,
|
type = rtu_t.redstone,
|
||||||
index = 1,
|
index = 1,
|
||||||
@ -151,7 +154,9 @@ for reactor_idx = 1, #rtu_redstone do
|
|||||||
modbus_busy = false,
|
modbus_busy = false,
|
||||||
pkt_queue = nil,
|
pkt_queue = nil,
|
||||||
thread = nil
|
thread = nil
|
||||||
})
|
}
|
||||||
|
|
||||||
|
table.insert(units, unit)
|
||||||
|
|
||||||
log.debug("init> initialized RTU unit #" .. #units .. ": redstone_io (redstone) [1] for reactor " .. rtu_redstone[reactor_idx].for_reactor)
|
log.debug("init> initialized RTU unit #" .. #units .. ": redstone_io (redstone) [1] for reactor " .. rtu_redstone[reactor_idx].for_reactor)
|
||||||
end
|
end
|
||||||
@ -201,6 +206,7 @@ for i = 1, #rtu_devices do
|
|||||||
end
|
end
|
||||||
|
|
||||||
if rtu_iface ~= nil then
|
if rtu_iface ~= nil then
|
||||||
|
---@class rtu_unit_registry_entry
|
||||||
local rtu_unit = {
|
local rtu_unit = {
|
||||||
name = rtu_devices[i].name,
|
name = rtu_devices[i].name,
|
||||||
type = rtu_type,
|
type = rtu_type,
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
local comms = require("scada-common.comms")
|
|
||||||
local log = require("scada-common.log")
|
local log = require("scada-common.log")
|
||||||
local mqueue = require("scada-common.mqueue")
|
local mqueue = require("scada-common.mqueue")
|
||||||
local ppm = require("scada-common.ppm")
|
local ppm = require("scada-common.ppm")
|
||||||
local types = require("scada-common.types")
|
local types = require("scada-common.types")
|
||||||
local util = require("scada-common.util")
|
local util = require("scada-common.util")
|
||||||
|
|
||||||
local redstone_rtu = require("rtu.dev.redstone_rtu")
|
|
||||||
local boiler_rtu = require("rtu.dev.boiler_rtu")
|
local boiler_rtu = require("rtu.dev.boiler_rtu")
|
||||||
local boilerv_rtu = require("rtu.dev.boilerv_rtu")
|
local boilerv_rtu = require("rtu.dev.boilerv_rtu")
|
||||||
local energymachine_rtu = require("rtu.dev.energymachine_rtu")
|
local energymachine_rtu = require("rtu.dev.energymachine_rtu")
|
||||||
@ -24,12 +22,11 @@ local println = util.println
|
|||||||
local print_ts = util.print_ts
|
local print_ts = util.print_ts
|
||||||
local println_ts = util.println_ts
|
local println_ts = util.println_ts
|
||||||
|
|
||||||
local psleep = util.psleep
|
|
||||||
|
|
||||||
local MAIN_CLOCK = 2 -- (2Hz, 40 ticks)
|
local MAIN_CLOCK = 2 -- (2Hz, 40 ticks)
|
||||||
local COMMS_SLEEP = 150 -- (150ms, 3 ticks)
|
local COMMS_SLEEP = 150 -- (150ms, 3 ticks)
|
||||||
|
|
||||||
-- main thread
|
-- main thread
|
||||||
|
---@param smem rtu_shared_memory
|
||||||
threads.thread__main = function (smem)
|
threads.thread__main = function (smem)
|
||||||
-- execute thread
|
-- execute thread
|
||||||
local exec = function ()
|
local exec = function ()
|
||||||
@ -42,7 +39,7 @@ threads.thread__main = function (smem)
|
|||||||
local rtu_state = smem.rtu_state
|
local rtu_state = smem.rtu_state
|
||||||
local rtu_dev = smem.rtu_dev
|
local rtu_dev = smem.rtu_dev
|
||||||
local rtu_comms = smem.rtu_sys.rtu_comms
|
local rtu_comms = smem.rtu_sys.rtu_comms
|
||||||
local conn_watchdog = smem.rtu_sys.conn_watchdog ---@type watchdog
|
local conn_watchdog = smem.rtu_sys.conn_watchdog
|
||||||
local units = smem.rtu_sys.units
|
local units = smem.rtu_sys.units
|
||||||
|
|
||||||
-- start clock
|
-- start clock
|
||||||
@ -116,7 +113,7 @@ threads.thread__main = function (smem)
|
|||||||
else
|
else
|
||||||
-- relink lost peripheral to correct unit entry
|
-- relink lost peripheral to correct unit entry
|
||||||
for i = 1, #units do
|
for i = 1, #units do
|
||||||
local unit = units[i]
|
local unit = units[i] ---@type rtu_unit_registry_entry
|
||||||
|
|
||||||
-- find disconnected device to reconnect
|
-- find disconnected device to reconnect
|
||||||
if unit.name == param1 then
|
if unit.name == param1 then
|
||||||
@ -137,7 +134,7 @@ threads.thread__main = function (smem)
|
|||||||
unit.rtu = imatrix_rtu.new(device)
|
unit.rtu = imatrix_rtu.new(device)
|
||||||
end
|
end
|
||||||
|
|
||||||
unit.modbus_io = modbus.new(unit.rtu)
|
unit.modbus_io = modbus.new(unit.rtu, true)
|
||||||
|
|
||||||
println_ts("reconnected the " .. unit.type .. " on interface " .. unit.name)
|
println_ts("reconnected the " .. unit.type .. " on interface " .. unit.name)
|
||||||
end
|
end
|
||||||
@ -159,6 +156,7 @@ threads.thread__main = function (smem)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- communications handler thread
|
-- communications handler thread
|
||||||
|
---@param smem rtu_shared_memory
|
||||||
threads.thread__comms = function (smem)
|
threads.thread__comms = function (smem)
|
||||||
-- execute thread
|
-- execute thread
|
||||||
local exec = function ()
|
local exec = function ()
|
||||||
@ -179,14 +177,16 @@ threads.thread__comms = function (smem)
|
|||||||
while comms_queue.ready() and not rtu_state.shutdown do
|
while comms_queue.ready() and not rtu_state.shutdown do
|
||||||
local msg = comms_queue.pop()
|
local msg = comms_queue.pop()
|
||||||
|
|
||||||
if msg.qtype == mqueue.TYPE.COMMAND then
|
if msg ~= nil then
|
||||||
-- received a command
|
if msg.qtype == mqueue.TYPE.COMMAND then
|
||||||
elseif msg.qtype == mqueue.TYPE.DATA then
|
-- received a command
|
||||||
-- received data
|
elseif msg.qtype == mqueue.TYPE.DATA then
|
||||||
elseif msg.qtype == mqueue.TYPE.PACKET then
|
-- received data
|
||||||
-- received a packet
|
elseif msg.qtype == mqueue.TYPE.PACKET then
|
||||||
-- handle the packet (rtu_state passed to allow setting link flag)
|
-- received a packet
|
||||||
rtu_comms.handle_packet(msg.message, units, rtu_state)
|
-- handle the packet (rtu_state passed to allow setting link flag)
|
||||||
|
rtu_comms.handle_packet(msg.message, units, rtu_state)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- quick yield
|
-- quick yield
|
||||||
@ -209,6 +209,8 @@ threads.thread__comms = function (smem)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- per-unit communications handler thread
|
-- per-unit communications handler thread
|
||||||
|
---@param smem rtu_shared_memory
|
||||||
|
---@param unit rtu_unit_registry_entry
|
||||||
threads.thread__unit_comms = function (smem, unit)
|
threads.thread__unit_comms = function (smem, unit)
|
||||||
-- execute thread
|
-- execute thread
|
||||||
local exec = function ()
|
local exec = function ()
|
||||||
@ -227,16 +229,18 @@ threads.thread__unit_comms = function (smem, unit)
|
|||||||
while packet_queue.ready() and not rtu_state.shutdown do
|
while packet_queue.ready() and not rtu_state.shutdown do
|
||||||
local msg = packet_queue.pop()
|
local msg = packet_queue.pop()
|
||||||
|
|
||||||
if msg.qtype == mqueue.TYPE.COMMAND then
|
if msg ~= nil then
|
||||||
-- received a command
|
if msg.qtype == mqueue.TYPE.COMMAND then
|
||||||
elseif msg.qtype == mqueue.TYPE.DATA then
|
-- received a command
|
||||||
-- received data
|
elseif msg.qtype == mqueue.TYPE.DATA then
|
||||||
elseif msg.qtype == mqueue.TYPE.PACKET then
|
-- received data
|
||||||
-- received a packet
|
elseif msg.qtype == mqueue.TYPE.PACKET then
|
||||||
unit.modbus_busy = true
|
-- received a packet
|
||||||
local return_code, reply = unit.modbus_io.handle_packet(msg.message)
|
unit.modbus_busy = true
|
||||||
rtu_comms.send_modbus(reply)
|
local _, reply = unit.modbus_io.handle_packet(msg.message)
|
||||||
unit.modbus_busy = false
|
rtu_comms.send_modbus(reply)
|
||||||
|
unit.modbus_busy = false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- quick yield
|
-- quick yield
|
||||||
|
Loading…
Reference in New Issue
Block a user