Module modbus Implementation of modbus RTU, ASCII master.
This module relies on the serial module to handle port configuration and actual data exchanges.

Data read and written by this module are exchanged as buffers, i.e. either strings or lists of strings. String(s) contain data as 8-bits bytes; endianness depends on what is expected/returned by the slave device. To encode such strings, it is suggested to rely on pack, the binary string packing library.

Type modbus

modbus.new(port, cfg, mode) Creates a new #modbusdev.

Type modbusdev

modbusdev:close() Closes a modbus instance.
modbusdev:customRequest(req, sid, payload) Send a custom MODBUS request, not directly supported by the API.
modbusdev:readCoils(sid, address, length) Read a sequence of consecutive coils' content.
modbusdev:readDiscreteInputs(sid, address, length) Read a sequence of consecutive discrete inputs' content.
modbusdev:readHoldingRegisters(sid, address, length) Read a sequence of consecutive holding registers' content.
modbusdev:readInputRegisters(sid, address, length) Read a sequence of consecutive input registers' content.
modbusdev:writeMultipleCoils(sid, address, values) Write a sequence of consecutive coils.
modbusdev:writeMultipleRegisters(sid, address, values) Write a sequence of consecutive registers.
modbusdev:writeSingleCoil(sid, address, value) Write a value in a coil.
modbusdev:writeSingleRegister(sid, address, value) Write a value in a register.

Type modbus

Field(s)

modbus.new(port, cfg, mode)

Creates a new #modbusdev.
Default initialization is "/dev/ttyS0", 115200, "none", "RTU", 10

Parameters

Return values

  1. #modbusdev: the new #modbusdev on success
  2. nil + error message

Type modbusdev

Field(s)

modbusdev:close()

Closes a modbus instance.

Return value

nothing.

modbusdev:customRequest(req, sid, payload)

Send a custom MODBUS request, not directly supported by the API.

Parameters

Return values

  1. the response as a buffer. Beware, it contains some error-checking bytes as a suffix.
  2. nil + error message.

modbusdev:readCoils(sid, address, length)

Read a sequence of consecutive coils' content.

Parameters

Return values

  1. read coil values in a buffer
  2. nil + error message

modbusdev:readDiscreteInputs(sid, address, length)

Read a sequence of consecutive discrete inputs' content.

Parameters

Return values

  1. read discrete inputs contant in a buffer
  2. nil + error message

modbusdev:readHoldingRegisters(sid, address, length)

Read a sequence of consecutive holding registers' content.

Parameters

Return values

  1. read holding registers content in a buffer
  2. nil + error message

modbusdev:readInputRegisters(sid, address, length)

Read a sequence of consecutive input registers' content.

Parameters

Return values

  1. read input registers content in a buffer
  2. nil + error message

modbusdev:writeMultipleCoils(sid, address, values)

Write a sequence of consecutive coils.

Parameters

Return values

  1. "ok"
  2. nil + error message

Usage:


  m = modbus.new('/dev/ttyS0')
  res,err=m:writeMultipleCoils(1, 0, 8, string.pack('x8', 
  true,false,false,true,false,true,false,true)

modbusdev:writeMultipleRegisters(sid, address, values)

Write a sequence of consecutive registers.

Parameters

Return values

  1. "ok"
  2. nil + error message

modbusdev:writeSingleCoil(sid, address, value)

Write a value in a coil.

Parameters

Return values

  1. "ok"
  2. nil + error message

modbusdev:writeSingleRegister(sid, address, value)

Write a value in a register.

Parameters

Return values

  1. "ok"
  2. nil + error message