Module libmodbus
lua bindings to libmodbus.
Generally, this provides a very thin layer over libmodbus. Instead of passing the context around to all your modbus_xxx functions, you simply call them as member functions on the context returned by the new() functions.
Info:
- License:
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Author: Karl Palsson
2016-2020
Functions
version () | Returns the runtime linked version of libmodbus as a string. |
new_rtu (device, baud, parity, databits, stopbits) | Create a Modbus/RTU context |
new_tcp_pi (host, service) | Create a Modbus/TCP context |
set_s32 (num) | Write a 32bit (u)int to 2x16bit registers |
set_f32 (num) | Write a bit float to 2x16bit registers |
get_s16 (1) | 16bit register as number to signed 16bit |
get_s32 (1, 2) | 2x16bit registers as number to signed 32 bit |
get_s32le (1, 2) | 2x16bit registers as number to signed 32 bit (reverse order) |
get_u32 (1, 2) | 2x16bit registers as number to unsigned 32 bit |
get_u32le (1, 2) | 2x16bit registers as number to unsigned 32 bit (reversed order) |
get_f32 (1, 2) | 2x16bit registers as number to 32 bit float |
get_f32le (1, 2) | 2x16bit registers as number to 32 bit float (Reversed order) |
get_s64 (1, 2, 3, 4) | 4x16bit registers as number to signed 64 bit |
get_u64 (1, 2, 3, 4) | 4x16bit registers as number to unsigned 64 bit |
Context Methods
Constants provided for use
rtu_constants | RTU Mode Constants |
rtu_rts_constants | RTU RTS Consants |
error_recovery_constants | Error Recovery Constants |
exception_codes | Exception codes These are all MODBUS_xxxx upstream in libmodbus. |
other_constants | Other constants |
Functions
- version () line 83
-
Returns the runtime linked version of libmodbus as a string.
The compile time version is available as a constant VERSION.
Returns:
-
eg "3.0.6"
See also:
- new_rtu (device, baud, parity, databits, stopbits) line 103
-
Create a Modbus/RTU context
Parameters:
- device (required)
- baud rate, defaults to 19200
- parity defaults to EVEN
- databits defaults to 8
- stopbits defaults to 1
Returns:
-
a modbus context ready for use
- new_tcp_pi (host, service) line 168
-
Create a Modbus/TCP context
Parameters:
- host eg "192.168.1.100" or "modbus.example.org"
- service eg "502" or "mbap"
Returns:
-
a modbus context ready for use
- set_s32 (num) line 204
-
Write a 32bit (u)int to 2x16bit registers
Parameters:
- num 32bit number
Returns:
- reg1 upper 16bits
- reg2 lower 16bits
- set_f32 (num) line 221
-
Write a bit float to 2x16bit registers
Parameters:
- num floating point number
Returns:
- reg1 upper 16bits of a 32bit float
- reg2 lower 16bits of a 32bit float
- get_s16 (1) line 239
-
16bit register as number to signed 16bit
Parameters:
- 1 16bit register
Returns:
-
signed 16bit number
- get_s32 (1, 2) line 253
-
2x16bit registers as number to signed 32 bit
Parameters:
- 1 16bit register
- 2 16bit register
Returns:
-
32bit number
- get_s32le (1, 2) line 269
-
2x16bit registers as number to signed 32 bit (reverse order)
Parameters:
- 1 16bit register
- 2 16bit register
Returns:
-
32bit number
- get_u32 (1, 2) line 286
-
2x16bit registers as number to unsigned 32 bit
Parameters:
- 1 16bit register
- 2 16bit register
Returns:
-
32bit number
- get_u32le (1, 2) line 302
-
2x16bit registers as number to unsigned 32 bit (reversed order)
Parameters:
- 1 16bit register
- 2 16bit register
Returns:
-
32bit number
- get_f32 (1, 2) line 318
-
2x16bit registers as number to 32 bit float
Parameters:
- 1 16bit register
- 2 16bit register
Returns:
-
32bit float
- get_f32le (1, 2) line 338
-
2x16bit registers as number to 32 bit float (Reversed order)
Parameters:
- 1 16bit register
- 2 16bit register
Returns:
-
32bit float
- get_s64 (1, 2, 3, 4) line 360
-
4x16bit registers as number to signed 64 bit
Parameters:
- 1 16bit register
- 2 16bit register
- 3 16bit register
- 4 16bit register
Returns:
-
64bit number
- get_u64 (1, 2, 3, 4) line 383
-
4x16bit registers as number to unsigned 64 bit
Parameters:
- 1 16bit register
- 2 16bit register
- 3 16bit register
- 4 16bit register
Returns:
-
64bit number
Context Methods
- ctx:connect () line 441
- Connect, see modbus_connect()
- ctx:close () line 454
- Close, see modbus_close()
- ctx:set_debug (enable) line 468
-
Set debug
Parameters:
- enable optional bool, defaults to true
- ctx:set_error_recovery (a, b) line 488
-
Set error recovery, see modbus_set_error_recovery.
The arguments will be or'd together.
Parameters:
- a one of error_recovery_constants
- b one of error_recovery_constants
- ctx:set_byte_timeout (seconds, microseconds) line 505
-
See also modbus_set_byte_timeout
Parameters:
- seconds (required)
- microseconds (optional, defaults to 0)
- ctx:get_byte_timeout () line 523
-
Returns:
- seconds
- microseconds
- ctx:set_response_timeout (seconds, microseconds) line 544
-
Parameters:
- seconds (required)
- microseconds (optional, defaults to 0)
- ctx:get_response_timeout () line 562
-
Returns:
- seconds
- microseconds
- ctx:get_socket () line 582
-
Returns:
-
the socket number
- ctx:set_socket (sock) line 595
-
Parameters:
- sock integer socket number to set
- ctx:rtu_get_serial_mode () line 609
-
Returns:
-
rtu_constants the serial mode, either RTU_RS232 or RTU_RS485
- ctx:rtu_set_serial_mode (mode) line 628
-
Sets the mode of a serial port.
Remember, this is only required if your kernel is handling rs485 natively.
If you are using a USB adapter, you do NOT need this.
Parameters:
- mode The selected serial mode from rtu_constants, either RTU_RS232 or RTU_RS485.
- ctx:rtu_get_rts () line 645
-
Returns:
-
rtu_rts_constants the RTS handling mode, up/down/none
- ctx:rtu_set_rts (mode) line 662
-
Sets the RTS handling of a serial port.
Parameters:
- mode The selected RTS handling mode from rtu_rts_constants, up/down/none.
- ctx:rtu_get_rts_delay () line 679
-
Returns:
-
usecs
- ctx:rtu_set_rts_delay (usecs) line 696
-
Sets the RTS delay of a serial port.
Parameters:
- usecs
- ctx:get_header_length () line 714
-
Returns the header length of the transport
Returns:
-
the length in bytes
- ctx:set_slave (unitid) line 727
-
Parameters:
- unitid the unit address / slave id to use
- ctx:read_input_bits (address, count) line 781
-
Parameters:
- address
- count
Returns:
-
an array of results
- ctx:read_bits (address, count) line 792
-
Parameters:
- address
- count
Returns:
-
an array of results
- ctx:read_input_registers (address, count) line 840
-
Parameters:
- address
- count
Returns:
-
an array of results
- ctx:read_registers (address, count) line 851
-
Parameters:
- address
- count
Returns:
-
an array of results
- ctx:report_slave_id () line 860
-
Returns:
-
a luastring with the raw result (lua strings can contain nulls)
- ctx:write_bit (address, value) line 881
-
Parameters:
- address
- value either a number or a boolean
- ctx:write_register (address, value) line 905
-
Parameters:
- address
- value
- ctx:write_bits (address, value) line 922
-
Parameters:
- address
- value as a lua array table
- ctx:write_registers (address, value) line 984
-
Parameters:
- address base address to write to
- value as a lua array table, or a sequence of values.
Usage:
either ctx:write_registers(0x2000, {1,2,3}) ctx:write_registers(0x2000, 1, 2, 3)
- ctx:tcp_pi_listen (conns) line 1095
-
Parameters:
- conns max connections to listen to, defaults to 1
Returns:
-
the new socket number or normal error
- ctx:tcp_pi_accept (sock) line 1117
-
Parameters:
- sock the socket we're accepting on
Returns:
-
the new socket we've accepted or normal error
- ctx:receive () line 1141
-
Receives a request from a remote.
WARNING this might not be complete, documented that it exists
Returns:
- the length of the data
- the data received
- ctx:reply_exception (initial, exc) line 1183
-
Reply to a request with an Exception.
Parameters:
- initial the initial request to build into an exception
- exc the exception code to use from exception_codes
Returns:
-
the new socket number or normal error
Constants provided for use
- rtu_constants line 1230
-
RTU Mode Constants
Fields:
- RTU_RS232
- RTU_RS485
See also:
- rtu_rts_constants line 1238
-
RTU RTS Consants
Fields:
- RTU_RTS_NONE
- RTU_RTS_UP
- RTU_RTS_DOWN
See also:
- error_recovery_constants line 1253
-
Error Recovery Constants
Fields:
- ERROR_RECOVERY_NONE
- ERROR_RECOVERY_LINK
- ERROR_RECOVERY_PROTOCOL
See also:
- exception_codes line 1254
-
Exception codes
These are all MODBUS_xxxx upstream in libmodbus.
Fields:
- EXCEPTION_ILLEGAL_FUNCTION
- EXCEPTION_ILLEGAL_DATA_ADDRESS
- EXCEPTION_ILLEGAL_DATA_VALUE
- EXCEPTION_SLAVE_OR_SERVER_FAILURE
- EXCEPTION_ACKNOWLEDGE
- EXCEPTION_SLAVE_OR_SERVER_BUSY
- EXCEPTION_NEGATIVE_ACKNOWLEDGE
- EXCEPTION_MEMORY_PARITY
- EXCEPTION_NOT_DEFINED
- EXCEPTION_GATEWAY_PATH
- other_constants line 1285
-
Other constants
Fields: