Modbus 3.0.8

After L Master version 3.0, the robot supports acting as a Modbus master (client) to connect with PLCs (Programmable Logic Controllers) for programming. It supports network-based TCP/Socket underlying protocol and RTU serial protocol.

WARNING

Errors may occur due to unstable communication and other reasons. In most cases, you need to use the error handling mechanism to catch errors and prevent abnormal exits.

Compatibility Note

In the older L Master 2.x versions, an open-source libraryopen in new window was pre-installed 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 has been deprecated in version 3.0.

Connect to Modbus/TCP IP Slave

modbus.new_tcp(ip, port)

Connect to a slave via Modbus/TCP IP.

Supports IPv4 and IPv6.

Returns a slave instance or throws an error.

Connect to Modbus/RTU Slave

modbus.new_rtu(com)

Connect to a slave via Modbus/RTU.

  • Parameters

Returns a slave instance or throws an error.

Connect to Flange Slave

modbus.new_flange()

Connect to a slave via the flange's Modbus/RTU.

Currently only supports a baud rate of 115200 bps.

Currently only supports using 8 data bits, 1 stop bit, and no parity bit.

Returns a slave instance or throws an error.

Set Slave ID

mb:set_slave(id)

Returns nothing.

Set Timeout

mb:set_timeout(timeout)
  • Parameters
    • timeout: Timeout in milliseconds (ms). Default is 800ms.

Write Single Coil

mb:write_single_coil(addr, val)

Returns nothing or throws a connection error.

Write Multiple Coils

mb:write_multiple_coils(addr, vals)

Returns nothing or throws a connection error.

Read Multiple Coils

mb:read_coils(addr, num)

Returns a boolean array or throws a connection error.

Read Multiple Discrete Inputs

mb:read_discrete_inputs(addr, num)

Returns a boolean array or throws a connection error.

Write Single Holding Register

mb:write_single_register(addr, val)

Returns nothing or throws a connection error.

Write Multiple Holding Registers

mb:write_multiple_registers(addr, vals)

Returns nothing or throws a connection error.

Read Multiple Holding Registers

mb:read_holding_registers(addr, num)

Returns a u16 array or throws a connection error.

Read Multiple Input Registers

mb:read_input_registers(addr, num)

Returns a u16 array or throws a connection error.

Read and Write Multiple Registers

mb:read_write_multiple_registers(read_addr, read_num, write_addr, write_val)

Returns a u16 array or throws a connection error.

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 flange Modbus
local mb = modbus.new_flange()

-- Configure Modbus
mb:set_timeout(500)
mb:set_slave(0x01)

-- Set 1 DO, using 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 DOs, using pcall to catch errors
success, result = pcall(function()
  -- Set 5 DOs
  mb:write_multiple_coils(0x0000, {true,true,true,true,true})
  -- Read 5 DOs
  mb:read_coils(0x0000, 5)
end)
if not success then
  print("Error: ", result)
end
-- Read 5 DIs
mb:read_discrete_inputs(0x0000, 5)

-- Set 1 AO
mb:write_single_register(0x0090, 0x0088)
-- Set 2 AOs
mb:write_multiple_registers(0x0090, {0x0088, 0x0088})
-- Read 2 AOs
mb:read_holding_registers(0x0090, 2)
-- Read 2 AIs
mb:read_input_registers(0x0080, 2)