From 88c34d8bca0b2db23b022a8002a1cc662d660073 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Sun, 18 Sep 2022 22:02:17 -0400 Subject: [PATCH] fixed acknowledge packets to use error flag, fixed 'static'-like function scope of modbus functions --- rtu/modbus.lua | 73 ++++++++++++++++++++++++------------------------- rtu/rtu.lua | 2 +- rtu/startup.lua | 2 +- rtu/threads.lua | 6 ++-- 4 files changed, 41 insertions(+), 42 deletions(-) diff --git a/rtu/modbus.lua b/rtu/modbus.lua index 498dd19..c011ede 100644 --- a/rtu/modbus.lua +++ b/rtu/modbus.lua @@ -332,10 +332,9 @@ function modbus.new(rtu_dev, use_parallel_read) -- default is to echo back local func_code = packet.func_code - if not return_code then - -- echo back with error flag - func_code = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG) - end + + -- echo back with error flag, on success the "error" will be acknowledgement + func_code = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG) -- create reply local reply = comms.modbus_packet() @@ -400,40 +399,40 @@ function modbus.new(rtu_dev, use_parallel_read) return return_code, reply end - -- return a SERVER_DEVICE_BUSY error reply - ---@return modbus_packet reply - function public.reply__srv_device_busy(packet) - -- reply back with error flag and exception code - local reply = comms.modbus_packet() - local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG) - local data = { MODBUS_EXCODE.SERVER_DEVICE_BUSY } - reply.make(packet.txn_id, packet.unit_id, fcode, data) - return reply - end - - -- return a NEG_ACKNOWLEDGE error reply - ---@return modbus_packet reply - function public.reply__neg_ack(packet) - -- reply back with error flag and exception code - local reply = comms.modbus_packet() - local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG) - local data = { MODBUS_EXCODE.NEG_ACKNOWLEDGE } - reply.make(packet.txn_id, packet.unit_id, fcode, data) - return reply - end - - -- return a GATEWAY_PATH_UNAVAILABLE error reply - ---@return modbus_packet reply - function public.reply__gw_unavailable(packet) - -- reply back with error flag and exception code - local reply = comms.modbus_packet() - local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG) - local data = { MODBUS_EXCODE.GATEWAY_PATH_UNAVAILABLE } - reply.make(packet.txn_id, packet.unit_id, fcode, data) - return reply - end - return public end +-- return a SERVER_DEVICE_BUSY error reply +---@return modbus_packet reply +function modbus.reply__srv_device_busy(packet) + -- reply back with error flag and exception code + local reply = comms.modbus_packet() + local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG) + local data = { MODBUS_EXCODE.SERVER_DEVICE_BUSY } + reply.make(packet.txn_id, packet.unit_id, fcode, data) + return reply +end + +-- return a NEG_ACKNOWLEDGE error reply +---@return modbus_packet reply +function modbus.reply__neg_ack(packet) + -- reply back with error flag and exception code + local reply = comms.modbus_packet() + local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG) + local data = { MODBUS_EXCODE.NEG_ACKNOWLEDGE } + reply.make(packet.txn_id, packet.unit_id, fcode, data) + return reply +end + +-- return a GATEWAY_PATH_UNAVAILABLE error reply +---@return modbus_packet reply +function modbus.reply__gw_unavailable(packet) + -- reply back with error flag and exception code + local reply = comms.modbus_packet() + local fcode = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG) + local data = { MODBUS_EXCODE.GATEWAY_PATH_UNAVAILABLE } + reply.make(packet.txn_id, packet.unit_id, fcode, data) + return reply +end + return modbus diff --git a/rtu/rtu.lua b/rtu/rtu.lua index d59649d..cf69b58 100644 --- a/rtu/rtu.lua +++ b/rtu/rtu.lua @@ -353,7 +353,7 @@ function rtu.comms(version, modem, local_port, server_port, conn_watchdog) -- check if there are more than 3 active transactions -- still queue the packet, but this may indicate a problem if unit.pkt_queue.length() > 3 then - reply = unit.modbus_io.reply__srv_device_busy(packet) + reply = modbus.reply__srv_device_busy(packet) log.debug("queueing new request with " .. unit.pkt_queue.length() .. " transactions already in the queue" .. unit_dbg_tag) end diff --git a/rtu/startup.lua b/rtu/startup.lua index 9f9ee16..758ee84 100644 --- a/rtu/startup.lua +++ b/rtu/startup.lua @@ -25,7 +25,7 @@ local imatrix_rtu = require("rtu.dev.imatrix_rtu") local turbine_rtu = require("rtu.dev.turbine_rtu") local turbinev_rtu = require("rtu.dev.turbinev_rtu") -local RTU_VERSION = "beta-v0.7.11" +local RTU_VERSION = "beta-v0.7.12" local rtu_t = types.rtu_t diff --git a/rtu/threads.lua b/rtu/threads.lua index 37ce9bc..e20c337 100644 --- a/rtu/threads.lua +++ b/rtu/threads.lua @@ -256,7 +256,7 @@ function threads.thread__unit_comms(smem, unit) -- execute thread function public.exec() - log.debug("rtu unit thread start -> " .. unit.name .. "(" .. unit.type .. ")") + log.debug("rtu unit thread start -> " .. unit.type .. "(" .. unit.name .. ")") -- load in from shared memory local rtu_state = smem.rtu_state @@ -289,7 +289,7 @@ function threads.thread__unit_comms(smem, unit) -- check for termination request if rtu_state.shutdown then - log.info("rtu unit thread exiting -> " .. unit.name .. "(" .. unit.type .. ")") + log.info("rtu unit thread exiting -> " .. unit.type .. "(" .. unit.name .. ")") break end @@ -309,7 +309,7 @@ function threads.thread__unit_comms(smem, unit) end if not rtu_state.shutdown then - log.info(util.c("rtu unit thread ", unit.name, "(", unit.type, ") restarting in 5 seconds...")) + log.info(util.c("rtu unit thread ", unit.type, "(", unit.name, ") restarting in 5 seconds...")) util.psleep(5) end end