mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
some comms cleanup and added wrapper for generic packet
This commit is contained in:
parent
00a81ab4f0
commit
018b228976
@ -1,16 +1,86 @@
|
|||||||
|
PROTOCOLS = {
|
||||||
|
MODBUS_TCP = 0, -- our "modbus tcp"-esque protocol
|
||||||
|
RPLC = 1, -- reactor plc protocol
|
||||||
|
SCADA_MGMT = 2, -- SCADA supervisor intercommunication
|
||||||
|
COORD_DATA = 3 -- data packets for coordinators to/from supervisory controller
|
||||||
|
}
|
||||||
|
|
||||||
|
-- generic SCADA packet object
|
||||||
|
function scada_packet()
|
||||||
|
local self = {
|
||||||
|
modem_msg_in = nil,
|
||||||
|
raw = nil
|
||||||
|
seq_id = nil,
|
||||||
|
protocol = nil,
|
||||||
|
length = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
local receive = function (side, sender, reply_to, message, distance)
|
||||||
|
self.modem_msg_in = {
|
||||||
|
iface = side,
|
||||||
|
s_port = sender,
|
||||||
|
r_port = reply_to,
|
||||||
|
msg = message,
|
||||||
|
dist = distance
|
||||||
|
}
|
||||||
|
|
||||||
|
self.raw = self.modem_msg_in.msg
|
||||||
|
|
||||||
|
if #self.raw < 3 then
|
||||||
|
-- malformed
|
||||||
|
return false
|
||||||
|
else
|
||||||
|
self.seq_id = self.raw[0]
|
||||||
|
self.protocol = self.raw[1]
|
||||||
|
self.length = self.raw[2]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local seq_id = function (packet)
|
||||||
|
return self.seq_id
|
||||||
|
end
|
||||||
|
|
||||||
|
local protocol = function (packet)
|
||||||
|
return self.protocol
|
||||||
|
end
|
||||||
|
|
||||||
|
local length = function (packet)
|
||||||
|
return self.length
|
||||||
|
end
|
||||||
|
|
||||||
|
local raw = function (packet)
|
||||||
|
return self.raw
|
||||||
|
end
|
||||||
|
|
||||||
|
local modem_event = function (packet)
|
||||||
|
return self.modem_msg_in
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
receive = receive,
|
||||||
|
seq_id = seq_id,
|
||||||
|
protocol = protocol,
|
||||||
|
length = length,
|
||||||
|
raw = raw,
|
||||||
|
modem_event = modem_event
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
-- coordinator communications
|
||||||
function coord_comms()
|
function coord_comms()
|
||||||
local self = {
|
local self = {
|
||||||
reactor_struct_cache = nil
|
reactor_struct_cache = nil
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- supervisory controller communications
|
||||||
function superv_comms()
|
function superv_comms()
|
||||||
local self = {
|
local self = {
|
||||||
reactor_struct_cache = nil
|
reactor_struct_cache = nil
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- reactor PLC communications
|
||||||
function rplc_comms(id, modem, local_port, server_port, reactor)
|
function rplc_comms(id, modem, local_port, server_port, reactor)
|
||||||
local self = {
|
local self = {
|
||||||
_id = id,
|
_id = id,
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
-- #REQUIRES comms.lua
|
||||||
|
|
||||||
-- modbus function codes
|
-- modbus function codes
|
||||||
local MODBUS_FCODE = {
|
local MODBUS_FCODE = {
|
||||||
READ_COILS = 0x01,
|
READ_COILS = 0x01,
|
||||||
@ -175,9 +177,8 @@ function modbus_init(rtu_dev)
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- create new modbus packet
|
function modbus_packet()
|
||||||
function new_modbus_packet(txn_id, protocol, length, unit_id, func_code, data)
|
local self = {
|
||||||
return {
|
|
||||||
txn_id = txn_id,
|
txn_id = txn_id,
|
||||||
protocol = protocol,
|
protocol = protocol,
|
||||||
length = length,
|
length = length,
|
||||||
@ -185,25 +186,34 @@ function new_modbus_packet(txn_id, protocol, length, unit_id, func_code, data)
|
|||||||
func_code = func_code,
|
func_code = func_code,
|
||||||
data = data
|
data = data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local receive = function (raw)
|
||||||
|
local size_ok = #raw ~= 6
|
||||||
|
|
||||||
|
if size_ok then
|
||||||
|
set(raw[1], raw[2], raw[3], raw[4], raw[5], raw[6])
|
||||||
end
|
end
|
||||||
|
|
||||||
-- parse raw table data as a modbus packet
|
return size_ok and self.protocol == comms.PROTOCOLS.MODBUS_TCP
|
||||||
function parse_modbus_packet(raw)
|
|
||||||
if #raw ~= 6 then
|
|
||||||
return nil
|
|
||||||
else
|
|
||||||
return new_modbus_packet(raw[1], raw[2], raw[3], raw[4], raw[5], raw[6])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- create raw table data from a modbus packet
|
local set = function (txn_id, protocol, length, unit_id, func_code, data)
|
||||||
function modbus_to_raw(packet)
|
self.txn_id = txn_id
|
||||||
|
self.protocol = protocol
|
||||||
|
self.length = length
|
||||||
|
self.unit_id = unit_id
|
||||||
|
self.func_code = func_code
|
||||||
|
self.data = data
|
||||||
|
end
|
||||||
|
|
||||||
|
local get = function ()
|
||||||
return {
|
return {
|
||||||
packet.txn_id,
|
txn_id = self.txn_id,
|
||||||
packet.protocol,
|
protocol = self.protocol,
|
||||||
packet.length,
|
length = self.length,
|
||||||
packet.unit_id,
|
unit_id = self.unit_id,
|
||||||
packet.func_code,
|
func_code = self.func_code,
|
||||||
packet.data
|
data = self.data
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user