RS485, Modbus & NVT
This category contains functions specific to RS-485 or MODBUS devices.
- Overview
- Arguments
- Example
Sends message to RS-485 bus.
Ensure RS-485 is turned on using api.rs485State(1)
first.
- msg (string) - Data to be sent to RS-485 bus
--sends 'test' string to RS-485
api.rs485Send('test')
- Overview
- Arguments
- Return
- Examples
Configures the RS-485 communication interface.
api.rs485Setup("RX_BUFFER", value)
- "RX_BUFFER" (string) - set/get size of receive buffer
- value (integer)(optional) - set size of the receive buffer
api.rs485Setup(baudrate, parity, stopBits, dataBits)
- baudrate (integer) - Baudrate to use for communication (up to 921600 baud)
- parity (integer) - Parity, 0 for none, 1 for odd and 2 for even parity
- stopBits (integer) - Number of stop bits, 1 or 2 allowed
- dataBits (integer) - Number of data bits, 7 or 8 allowed
api.rs485Setup("RX_BUFFER", value)
- value (integer) - Size of the receive buffer
Example how to get the size of the receive buffer:
size = api.rs485Setup("RX_BUFFER")
Example how to setup RS-485 interface to 9600 Baud, 8E1:
api.rs485Setup(9600, 2, 1, 8)
- Overview
- Arguments
- Example
Controls the RS-485 circuitry.
This function must be called before api.rs485Send()
or api.rs485Receive()
.
- state (integer) - New state of RS-485 circuitry:
0
for off,1
for on (fast power-up)
api.rs485State(1) --turn on RS-485
- Overview
- Arguments
- Return
- Example
Waits timeout milliseconds for data reception from RS-485 bus.
Ensure RS-485 is turned on using api.rs485State(1)
first.
After the first character is received, the inter-character (i.e., inter-byte) timeout delay is set to 10 ms by default. This can be modified by providing a second optional argument to the function.
api.rs485Receive(timeout, interByteTimeout)
- timeout (integer) - The maximum time in milliseconds to wait for RS-485 device answer
- interByteTimeout (integer, optional) - Inter-byte timeout in milliseconds
api.rs485Receive(timeout, terminator)
- timeout (integer) - The maximum time in milliseconds to wait for RS-485 device answer
- terminator (string, optional) - Terminator (can be more than one character)
api.rs485Receive(timeout, interByteTimeout, terminator)
- timeout (integer) - The maximum time in milliseconds to wait for RS-485 device answer
- interByteTimeout (integer, optional) - Inter-byte timeout in milliseconds
- terminator (string, optional) - Terminator (can be more than one character)
- answer (string) - Data received from RS-485 bus in given time
- len (integer) - Number of bytes received
--waits 1s for answer from RS-485 bus
ans,len = api.rs485Receive(1000)
--waits 1s for answer from RS-485 bus and gets everything until it finds "!" character
--stop garbage collector so it doesn't limit the memory (higher speed, prevent's loosing characters at 9600bd)
collectgarbage("stop")
ans,len = api.rs485Receive(1000,"!")
--restart the garbage collector (the memory will overflow, without it)
collectgarbage("restart")
- Overview
- Arguments
- Return
- Example
Calculates MODBUS request checksum.
- msg (string) - MODBUS request
- crc (string) - MODBUS crc for request
--calculate checksum for MODBUS request 110100010002
req = pack.pack('<b6', 0x11,0x01,0x00,0x01,0x00,0x02)
crc = api.modbusCrc(req) --crc = "EE9B"
api.modbusRTU(address, registerAddr, functionCode, typeSpec, registerCount, timeout, retry, interByteTimeout)
- Overview
- Arguments
- Return
- Example
Communicates with a MODBUS device over a serial connection using the RTU (Remote Terminal Unit) protocol.
It can be used to read or write one or more registers of various data types. The function takes several parameters, including the MODBUS device address, the starting register address, the function code, the type specification of the register(s), the number of registers to read or write, and several timeout and retry parameters.
The typeSpec
parameter is a Lua string that specifies the data type of the register(s) being read or written. It uses the Lua Packing and Unpacking syntax to specify the type and formatting of the data. For example, to read a single 16-bit unsigned integer, the typeSpec
parameter would be "H"
.
The function returns the value(s) read from or written to the MODBUS register(s), according to the specified typeSpec
. For example, if typeSpec
is "H"
and registerCount
is 2
, the function would return two 16-bit unsigned integers.
If the function fails to communicate with the MODBUS device, it will retry the operation according to the retry
parameter. If the operation still fails after the specified number of retries, the function will return nil
.
- address(integer) - The address of the MODBUS device to communicate with
- registerAddr(integer) - The starting address of the MODBUS register to read or write
- functionCode(integer) - The MODBUS function code to use for the operation (e.g. read coils, read input registers, write multiple coils, etc.)
- typeSpec(string) - A Lua string that specifies the data type of the register(s) being read or written. More details here
- if the typeSpec is
""
, it returns data as received
- if the typeSpec is
- registerCount(integer) - The number of registers to read or write
- timeout(integer) - The timeout (in milliseconds) for the MODBUS communication operation
- retry(integer) - The number of times to retry the operation if it fails
- interByteTimeout(integer) - The timeout (in milliseconds) between receiving bytes during the MODBUS communication operation
- ignoreCRC(bool, optional) - in case this is true, function is not checking MODBUS CRC
The function returns the value(s) read from the MODBUS register(s), according to the specified typeSpec.
--setup the RS-485 and turn on the RS-485 module
api.rs485Setup(9600,2,1,8) api.rs485State(1)
--read 0x4003 register (MODBUS ID in case of INEPRO PRO1)
x=api.modbusRTU(10,0x4003,3,">h",1,2000,3,10) print(x)
- Overview
- Arguments
- Return
- Example
Processes NVT message and either sets baudrate, datasize, parity or stop size for MBUS or MODBUS.
- buf (string) - Message to be processed
- msg (string) - Message without NVT sequence
- answer (string) - NVT answer
-- If massage received (buf) send it to RS-485
ret,port,buf = api.loraSend(0,1000,data)
if buf ~= nil then
buf, nvtans = api.nvtProcess(buf)
api.rs485Send(buf)
end