Modbus 3.0.8
Starting from L Master 3.0, the robot supports acting as a Modbus master (client) to connect with PLCs (Programmable Logic Controllers) for programming. Supports TCP/Socket underlying protocol based on network connections and RTU serial protocol.
WARNING
Due to communication instability and other reasons, errors may occur. In most cases, you need to use error handling mechanism to catch errors and prevent abnormal exit.
Compatibility Note
In older versions of L Master 2.x, an open source library was preset to handle Modbus communication:
require('libmodbus')
When serial or network communication is abnormal, this library may cause the system to freeze or crash. This usage is deprecated in version 3.0.
Connect Modbus/TCP Slave
Connects to a slave via Modbus/TCP IP.
Supports IPv4 and IPv6.
- Function:
new_tcpfrom the [modbus] module under lua module - Parameters:
- ip: [str] Slave IP address
- port: [int] Port number
- Returns: [Modbus] Slave instance, or throws error
Example
local mb = modbus.new_tcp("192.168.1.2", 10001)
Connect Modbus/RTU Slave
Connects to a slave via Modbus/RTU.
- Function:
new_rtufrom the [modbus] module under lua module - Parameters:
- com: [Serial] Serial port instance
- Returns: [Modbus] Slave instance, or throws error
Example
local com1 = serial.open("/dev/ttyS1")
com1:set_timeout(200)
com1:set_baud_rate(9600)
local mb = modbus.new_rtu(com1)
Connect Flange Slave
Connects to a slave via the flange's Modbus/RTU.
WARNING
- Due to underlying protocol forwarding, it takes an average of 20ms per 5 bytes. Please pay attention to the timeout setting.
- Only supports 8 data bits, 1 stop bit, no parity.
- Function:
new_flangefrom the [modbus] module under lua module - Parameters: none
- Returns: [Modbus] Slave instance, or throws error
Example
local mb = modbus.new_flange()
Set Flange Baud Rate
Sets the flange Modbus communication baud rate.
WARNING
For control box Modbus/RTU serial port baud rate, use serial module's set_baud_rate.
- Function:
set_flange_baud_ratefrom the lua module - Parameters:
- baud_rate: [int] Baud rate
- Returns: [bool] Whether set successfully
Example
set_flange_baud_rate(9600)
Set Slave ID
Sets the Modbus slave ID.
- Function:
set_slavemethod under [Modbus] - Parameters:
- id: [int] Slave ID
- Returns: none
Example
mb:set_slave(0x01)
Disconnect
Disconnects from the slave.
- Function:
disconnectmethod under [Modbus] - Parameters: none
- Returns: none
Example
mb:disconnect()
Set Timeout
Sets communication timeout.
- Function:
set_timeoutmethod under [Modbus] - Parameters:
- timeout: [int] Timeout in milliseconds (ms). Default 600ms. For flange, only supports 100~600
- Returns: none
Example
mb:set_timeout(500)
Set Timeout Retry Count
Sets communication timeout retry count.
- Function:
set_retrymethod under [Modbus] - Parameters:
- retry: [int] Timeout retry count. Default 0
- Returns: none
Example
mb:set_retry(2)
Write Single Coil
- Function:
write_single_coilmethod under [Modbus] - Parameters:
- addr: [int] Register address
- val: [bool] Value to write
- Returns: none, or throws connection error
Example
mb:write_single_coil(0x0000, true)
Write Multiple Coils
- Function:
write_multiple_coilsmethod under [Modbus] - Parameters:
- addr: [int] Starting register address
- vals: [list[bool]] Array of values to write
- Returns: none, or throws connection error
Example
mb:write_multiple_coils(0x0000, {true, true, true, true, true})
Read Multiple Coils
- Function:
read_coilsmethod under [Modbus] - Parameters:
- addr: [int] Starting register address
- num: [int] Number of reads
- Returns: [list[bool]] Boolean array, or throws connection error
Example
ret = mb:read_coils(0x0000, 5)
print(ret)
Read Multiple Discrete Inputs
- Function:
read_discrete_inputsmethod under [Modbus] - Parameters:
- addr: [int] Starting register address
- num: [int] Number of reads
- Returns: [list[bool]] Boolean array, or throws connection error
Example
ret = mb:read_discrete_inputs(0x0000, 5)
print(ret)
Write Single Holding Register
- Function:
write_single_registermethod under [Modbus] - Parameters:
- addr: [int] Register address
- val: [int] Value to write
- Returns: none, or throws connection error
Example
mb:write_single_register(0x0090, 0x0088)
Write Multiple Holding Registers
- Function:
write_multiple_registersmethod under [Modbus] - Parameters:
- addr: [int] Starting register address
- vals: [list[int]] Array of values to write
- Returns: none, or throws connection error
Example
mb:write_multiple_registers(0x0090, {0x0088, 0x0088})
Read Multiple Holding Registers
- Function:
read_holding_registersmethod under [Modbus] - Parameters:
- addr: [int] Starting register address
- num: [int] Number of reads
- Returns: [list[int]] u16 array, or throws connection error
Example
ret = mb:read_holding_registers(0x0090, 2)
print(ret)
Read Multiple Input Registers
- Function:
read_input_registersmethod under [Modbus] - Parameters:
- addr: [int] Starting register address
- num: [int] Number of reads
- Returns: [list[int]] u16 array, or throws connection error
Example
ret = mb:read_input_registers(0x0080, 2)
print(ret)
Example
-- Using Modbus/RTU
local com1 = serial.open("/dev/ttyS1")
com1:set_timeout(200)
com1:set_baud_rate(9600)
local mb = modbus.new_rtu(com1)
-- Using Modbus/TCP
local mb = modbus.new_tcp("192.168.1.2", 10001)
-- Using end-effector flange Modbus
local mb = modbus.new_flange()
-- Configure Modbus
mb:set_timeout(500)
mb:set_slave(0x01)
-- Set 1 DO, use pcall to catch errors
success, result = pcall(function() mb:write_single_coil(0x0000, true) end)
if not success then
print("Error: ", result)
end
-- Set and read 5 DO, use pcall to catch errors
success, result = pcall(function()
-- Set 5 DO
mb:write_multiple_coils(0x0000, {true, true, true, true, true})
-- Read 5 DO
mb:read_coils(0x0000, 5)
end)
if not success then
print("Error: ", result)
end
-- Read 5 DI
mb:read_discrete_inputs(0x0000, 5)
-- Set 1 AO
mb:write_single_register(0x0090, 0x0088)
-- Set 2 AO
mb:write_multiple_registers(0x0090, {0x0088, 0x0088})
-- Read 2 AO
mb:read_holding_registers(0x0090, 2)
-- Read 2 AI
mb:read_input_registers(0x0080, 2)
