modbus
Implementation of modbus RTU, ASCII master.
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.
modbus| modbus.new(port, cfg, mode) | Creates a new #modbusdev. |
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. |
modbusmodbus.new(port, cfg, mode)
"/dev/ttyS0", 115200, "none", "RTU", 10
port
:
the serial port on which modbus operates. As for serial.open,
it can be given as a port number, or as a file path (POSIX operating
systems only). Defaults to port 0, i.e. "/dev/ttyS0" on POSIX.
cfg
:
a optional serial device config table, see serial.config for
details on accepted fields.
mode
:
serial mode as a string: "ASCII" or "RTU". "RTU".
nil + error message
modbusdevmodbusdev:close()
nothing.
modbusdev:customRequest(req, sid, payload)
req
:
number identifying the custom function
sid
:
slave id
payload
:
optional data bytes to send, as a string
nil + error message.
modbusdev:readCoils(sid, address, length)
sid
:
number defining the slave id to send the request to
address
:
starting address
length
:
number of coils
nil + error message
modbusdev:readDiscreteInputs(sid, address, length)
sid
:
number defining the slave id to send the request to
address
:
starting address
length
:
number of inputs
nil + error message
modbusdev:readHoldingRegisters(sid, address, length)
sid
:
number defining the slave id to send the request to
address
:
starting address
length
:
number of registers
nil + error message
modbusdev:readInputRegisters(sid, address, length)
sid
:
number defining the slave id to send the request to
address
:
starting address
length
:
number of inputs
nil + error message
modbusdev:writeMultipleCoils(sid, address, values)
sid
:
number defining the slave id to send the request to
address
:
starting address
values
:
values to write, as a bytes buffer (8 coil per byte)
"ok"
nil + error message
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)
sid
:
number defining the slave id to send the request to
address
:
starting address
values
:
"ok"
nil + error message
modbusdev:writeSingleCoil(sid, address, value)
sid
:
number defining the slave id to send the request to
address
:
address of the coil
value
:
Boolean to write in the coil
"ok"
nil + error message
modbusdev:writeSingleRegister(sid, address, value)
sid
:
number defining the slave id to send the request to
address
:
address of the coil
value
:
Short integer ([0..0xFFFF]) to write in the register
"ok"
nil + error message