mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
#30 RTU comms code updated for new comms design
This commit is contained in:
parent
912011bfed
commit
554f09c817
45
rtu/rtu.lua
45
rtu/rtu.lua
@ -133,10 +133,21 @@ function rtu_comms(modem, local_port, server_port)
|
|||||||
|
|
||||||
-- PRIVATE FUNCTIONS --
|
-- PRIVATE FUNCTIONS --
|
||||||
|
|
||||||
local _send = function (protocol, msg)
|
local _send = function (msg_type, msg)
|
||||||
local packet = comms.scada_packet()
|
local s_pkt = comms.scada_packet()
|
||||||
packet.make(self.seq_num, protocol, msg)
|
local m_pkt = comms.mgmt_packet()
|
||||||
self.modem.transmit(self.s_port, self.l_port, packet.raw())
|
|
||||||
|
m_pkt.make(msg_type, msg)
|
||||||
|
s_pkt.make(self.seq_num, PROTOCOLS.SCADA_MGMT, m_pkt.raw_sendable())
|
||||||
|
|
||||||
|
self.modem.transmit(self.s_port, self.l_port, s_pkt.raw_sendable())
|
||||||
|
self.seq_num = self.seq_num + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
local _send_modbus = function (m_pkt)
|
||||||
|
local s_pkt = comms.scada_packet()
|
||||||
|
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.seq_num = self.seq_num + 1
|
self.seq_num = self.seq_num + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -171,24 +182,29 @@ function rtu_comms(modem, local_port, server_port)
|
|||||||
return pkt
|
return pkt
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- handle a MODBUS/SCADA packet
|
||||||
local handle_packet = function(packet, units, ref)
|
local handle_packet = function(packet, units, ref)
|
||||||
if packet ~= nil then
|
if packet ~= nil then
|
||||||
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 reply = modbus.reply__neg_ack(packet)
|
||||||
|
|
||||||
-- MODBUS instruction
|
-- 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]
|
||||||
local return_code, response = unit.modbus_io.handle_packet(packet)
|
local return_code, reply = unit.modbus_io.handle_packet(packet)
|
||||||
_send(PROTOCOLS.MODBUS_TCP, response)
|
|
||||||
|
|
||||||
if not return_code then
|
if not return_code then
|
||||||
log._warning("MODBUS operation failed")
|
log._warning("MODBUS operation failed")
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- unit ID out of range?
|
-- unit ID out of range?
|
||||||
|
reply = modbus.reply__gw_unavailable(packet)
|
||||||
log._error("MODBUS packet requesting non-existent unit")
|
log._error("MODBUS packet requesting non-existent unit")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
_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.REMOTE_LINKED then
|
if packet.type == SCADA_MGMT_TYPES.REMOTE_LINKED then
|
||||||
@ -210,10 +226,7 @@ function rtu_comms(modem, local_port, server_port)
|
|||||||
|
|
||||||
-- send capability advertisement
|
-- send capability advertisement
|
||||||
local send_advertisement = function (units)
|
local send_advertisement = function (units)
|
||||||
local advertisement = {
|
local advertisement = {}
|
||||||
type = SCADA_MGMT_TYPES.RTU_ADVERT,
|
|
||||||
units = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i = 1, #units do
|
for i = 1, #units do
|
||||||
local type = nil
|
local type = nil
|
||||||
@ -230,7 +243,7 @@ function rtu_comms(modem, local_port, server_port)
|
|||||||
|
|
||||||
if type ~= nil then
|
if type ~= nil then
|
||||||
if type == RTU_ADVERT_TYPES.REDSTONE then
|
if type == RTU_ADVERT_TYPES.REDSTONE then
|
||||||
table.insert(advertisement.units, {
|
table.insert(advertisement, {
|
||||||
unit = i,
|
unit = i,
|
||||||
type = type,
|
type = type,
|
||||||
index = units[i].index,
|
index = units[i].index,
|
||||||
@ -238,7 +251,7 @@ function rtu_comms(modem, local_port, server_port)
|
|||||||
rsio = units[i].device
|
rsio = units[i].device
|
||||||
})
|
})
|
||||||
else
|
else
|
||||||
table.insert(advertisement.units, {
|
table.insert(advertisement, {
|
||||||
unit = i,
|
unit = i,
|
||||||
type = type,
|
type = type,
|
||||||
index = units[i].index,
|
index = units[i].index,
|
||||||
@ -249,15 +262,11 @@ function rtu_comms(modem, local_port, server_port)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
_send(PROTOCOLS.SCADA_MGMT, advertisement)
|
_send(SCADA_MGMT_TYPES.RTU_ADVERT, advertisement)
|
||||||
end
|
end
|
||||||
|
|
||||||
local send_heartbeat = function ()
|
local send_heartbeat = function ()
|
||||||
local heartbeat = {
|
_send(SCADA_MGMT_TYPES.RTU_HEARTBEAT, {})
|
||||||
type = SCADA_MGMT_TYPES.RTU_HEARTBEAT
|
|
||||||
}
|
|
||||||
|
|
||||||
_send(PROTOCOLS.SCADA_MGMT, heartbeat)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user