Application note aims to explain, how to connect multiple Modbus or RS-485 devices to Acrios Modbus (RS-485) converter.
Multiple devices can be connected to the converter in parallel, as shown in the picture below. To have the best results, it's best if there's termination resistor connected at the end (usually 100 or 120 ohm).
Here's an example of LUA script which requests information from two different devices.
-----------------------
--- CONFIGURATION -----
-----------------------
----- LoRaWAN ----------
ack = 0
-- 1 for acknowledged, 0 for non-acknowledged
port = 100
-- transmit port
receiveTimeout = 10000
-- the maximum execution time in milliseconds
----- Modbus -----------
baudrate = 9600
-- baudrate: up to 921600 baud
parity = 2
-- communication parity: 0 for none, 1 for odd and 2 for even parity
stopBits = 1
-- number of stop bits: 1 or 2
dataBits = 8
-- number of data bits: 7 or 8
rxTimeout = 3000
-- slave device receive timeout in ms
------ Timing ---------
-- device wakeup interval
periodHours = 0
periodMinutes = 15
-----------------------
-----------------------
-- CONFIGURATION END --
-----------------------
frame={}
frame[1] = pack.pack('<b6', 0x01, 0x03, 0x40, 0x00, 0x00, 0x02)
frame[2] = pack.pack('<b6', 0xAA, 0x04, 0x00, 0x08, 0x00, 0x01)
devnum = 2 --number of devices (equals number of arrays)
function onWake ()
-- set link parameters - e.g. 9600 baud, 8N1
api.rs485Setup(baudrate,parity,stopBits,dataBits)
api.rs485State(1)
for i=1, devnum do
-- request frame[i]
req = frame[i]
crc = api.modbusCrc(req)
req = req .. crc -- add CRC, request is complete and ready for sending
if req == nil then
print("Please provide configuration!")
ans = 0
else
print("To RS485: ")
api.dumpArray(req)
api.rs485Send(req)
ans,length=api.rs485Receive(rxTimeout)
print("From RS485: ")
api.dumpArray(ans)
api.delayms(500)
end
if #ans < 1 then
buf = "NO DATA RECEIVED"
else
buf = buf .. ans
end
end
api.rs485State(0)
print("To LoRaWAN: ")
api.dumpArray(buf)
print("Sending to LoRaWAN")
res, rxport, rcvd = api.loraSend(ack, receiveTimeout, buf, port)
print("Done sending")
print("Sleep now, wake in " .. tostring(periodHours) .. "hrs:" .. tostring(periodMinutes) .."mins.....")
api.wakeUpIn(0,periodHours,periodMinutes,0)
end
function onStartup()
print("Starting up LoRaWAN multi Modbus script")
api.setVerbosity(4)
end
Here's an example of LUA script which listenes on the line to all devices. When Modbus frame is registered, send the information to LoRaWAN.
You can use same script that's used here.