Modbus RTU Configuration Commands
To configure Modbus RTU communication in the controller, follow these steps:
-
Create a Modbus RTU object:
require('luamodbus') mb = luamodbus.rtu()
-
Open Modbus RTU connection:
-
Set the communication parameters (baud rate, parity, data bits, stop bits, and duplex mode).
-
Example (19200 baud rate, even parity, 8 data bits, 1 stop bit, half duplex):
mb:open('/dev/RS485', 19200, 'E', 8, 1, 'H') mb:connect()
-
-
Terminal name:
-
The terminal name is
/dev/RS485
.
-
-
Supported baud rates:
-
Choose from the following baud rates:
-
300 bit/s
-
600 bit/s
-
1200 bit/s
-
2400 bit/s
-
4800 bit/s
-
9600 bit/s
-
19200 bit/s
-
38400 bit/s
-
57600 bit/s
-
115200 bit/s
-
230400 bit/s
-
-
-
Parity:
-
Set the parity mode:
-
“N” (None)
-
“E” (Even)
-
“O” (Odd)
-
-
-
Data bits and stop bits:
-
Data bits: Choose from 5, 6, 7, or 8.
-
Stop bits: Choose either 1 or 2.
-
Duplex mode:
-
“H” (Half duplex)
-
“F” (Full duplex, not supported in RS-485)
-
-
-
Baud rate considerations:
-
The baud rate affects communication distance:
-
9600 bit/sec: Max distance for 1-15 Modbus RTU devices is 1200 meters.
-
19200 bit/sec: Max distance is 900 meters (typical with Belden 3105A cables).
Baudrate setting
Maximum communication distance for 1 to 15 Modbus RTU devices (Typical with Belden 3105A cables)
9600 bit/sec
1200 m
19200 bit/sec
900 m
-
-
-
Parity explanation:
-
Parity checks for successful transmission.
-
Odd parity: Odd number of 1s.
-
Even parity: Even number of 1s.
-
Both gateway and meter must match (odd, even, or none).
-
-
Delay between frames:
-
Some devices need extra time after a response.
-
Use the delay command (e.g.,
os.sleep(1.5)
).
-
-
Set extension address:
-
Set slave address (e.g.,
mb:setslave(123)
).
-
-
Read registers:
-
Read from address 1000 and store the value:
value = mb:readregisters(1000)
-
-
Close Modbus connection:
mb:close()
-
Timeout intervals:
-
Adjust timeout intervals as needed:
-
Byte timeout:
mb:getbytetimeout()
mb:setbytetimeout(timeout)
-
Response timeout:
mb:getresponsetimeout()
mb:setresponsetimeout(timeout)
-
Receive timeout (extension mode only):
mb:getreceivetimeout()
mb:setreceivetimeout(timeout)
-
-
-
Example initialization:
-
Initialize Modbus on first script execution:
if not mb then require('luamodbus') mb = luamodbus.rtu() mb:open('/dev/RS485', 38400, 'E', 8, 1, 'H') mb:connect() end mb:setslave(30) mb:flush()
-