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 libraryopen in new window 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_tcp from the [modbus] module under lua module
  • Parameters:
    1. ip: [str] Slave IP address
    2. 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_rtu from the [modbus] module under lua module
  • Parameters:
    1. 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_flange from 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_rate from the lua module
  • Parameters:
    1. 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_slave method under [Modbus]
  • Parameters:
    1. id: [int] Slave ID
  • Returns: none

Example

mb:set_slave(0x01)

Disconnect

Disconnects from the slave.

  • Function: disconnect method under [Modbus]
  • Parameters: none
  • Returns: none

Example

mb:disconnect()

Set Timeout

Sets communication timeout.

  • Function: set_timeout method under [Modbus]
  • Parameters:
    1. 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_retry method under [Modbus]
  • Parameters:
    1. retry: [int] Timeout retry count. Default 0
  • Returns: none

Example

mb:set_retry(2)

Write Single Coil

  • Function: write_single_coil method under [Modbus]
  • Parameters:
    1. addr: [int] Register address
    2. val: [bool] Value to write
  • Returns: none, or throws connection error

Example

mb:write_single_coil(0x0000, true)

Write Multiple Coils

  • Function: write_multiple_coils method under [Modbus]
  • Parameters:
    1. addr: [int] Starting register address
    2. 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_coils method under [Modbus]
  • Parameters:
    1. addr: [int] Starting register address
    2. 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_inputs method under [Modbus]
  • Parameters:
    1. addr: [int] Starting register address
    2. 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_register method under [Modbus]
  • Parameters:
    1. addr: [int] Register address
    2. 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_registers method under [Modbus]
  • Parameters:
    1. addr: [int] Starting register address
    2. 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_registers method under [Modbus]
  • Parameters:
    1. addr: [int] Starting register address
    2. 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_registers method under [Modbus]
  • Parameters:
    1. addr: [int] Starting register address
    2. 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)