fixed acknowledge packets to use error flag, fixed 'static'-like function scope of modbus functions

This commit is contained in:
Mikayla Fischler 2022-09-18 22:02:17 -04:00
parent 3267e7ff13
commit 88c34d8bca
4 changed files with 41 additions and 42 deletions

View File

@ -332,10 +332,9 @@ function modbus.new(rtu_dev, use_parallel_read)
-- default is to echo back -- default is to echo back
local func_code = packet.func_code local func_code = packet.func_code
if not return_code then
-- echo back with error flag -- echo back with error flag, on success the "error" will be acknowledgement
func_code = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG) func_code = bit.bor(packet.func_code, MODBUS_FCODE.ERROR_FLAG)
end
-- create reply -- create reply
local reply = comms.modbus_packet() local reply = comms.modbus_packet()
@ -400,40 +399,40 @@ function modbus.new(rtu_dev, use_parallel_read)
return return_code, reply return return_code, reply
end 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 return public
end 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 return modbus

View File

@ -353,7 +353,7 @@ function rtu.comms(version, modem, local_port, server_port, conn_watchdog)
-- check if there are more than 3 active transactions -- check if there are more than 3 active transactions
-- still queue the packet, but this may indicate a problem -- still queue the packet, but this may indicate a problem
if unit.pkt_queue.length() > 3 then 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() .. log.debug("queueing new request with " .. unit.pkt_queue.length() ..
" transactions already in the queue" .. unit_dbg_tag) " transactions already in the queue" .. unit_dbg_tag)
end end

View File

@ -25,7 +25,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 = "beta-v0.7.11" local RTU_VERSION = "beta-v0.7.12"
local rtu_t = types.rtu_t local rtu_t = types.rtu_t

View File

@ -256,7 +256,7 @@ function threads.thread__unit_comms(smem, unit)
-- execute thread -- execute thread
function public.exec() 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 -- load in from shared memory
local rtu_state = smem.rtu_state local rtu_state = smem.rtu_state
@ -289,7 +289,7 @@ function threads.thread__unit_comms(smem, unit)
-- check for termination request -- check for termination request
if rtu_state.shutdown then 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 break
end end
@ -309,7 +309,7 @@ function threads.thread__unit_comms(smem, unit)
end end
if not rtu_state.shutdown then 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) util.psleep(5)
end end
end end