fixes to modbus_packet()

This commit is contained in:
Mikayla Fischler 2022-03-15 11:58:52 -04:00
parent 6e1e4c4685
commit 5642e3283d

View File

@ -1,5 +1,3 @@
-- #REQUIRES rtu.lua
-- modbus function codes
local MODBUS_FCODE = {
READ_COILS = 0x01,
@ -178,6 +176,8 @@ function modbus_init(rtu_dev)
end
function modbus_packet()
local MODBUS_TCP = 0
local self = {
txn_id = txn_id,
protocol = protocol,
@ -187,17 +187,7 @@ function modbus_packet()
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
return size_ok and self.protocol == comms.PROTOCOLS.MODBUS_TCP
end
local set = function (txn_id, protocol, length, unit_id, func_code, data)
local make = function (txn_id, protocol, length, unit_id, func_code, data)
self.txn_id = txn_id
self.protocol = protocol
self.length = length
@ -206,6 +196,16 @@ function modbus_packet()
self.data = data
end
local receive = function (raw)
local size_ok = #raw ~= 6
if size_ok then
make(raw[1], raw[2], raw[3], raw[4], raw[5], raw[6])
end
return size_ok and self.protocol == MODBUS_TCP
end
local get = function ()
return {
txn_id = self.txn_id,
@ -216,4 +216,10 @@ function modbus_packet()
data = self.data
}
end
return {
make = make,
receive = receive,
get = get
}
end