Abstraction layer over libmodbus, for Modbus RTU/TCP communication.
You may want to use the functions dm_to_signed()
and
dm_to_unsigned()
available in module dm_c
to convert unsigned to
signed integers and vice versa.
Use Modbus function code 0x03
to read holding registers from a Modbus
RTU connection:
integer :: i, rc, s
integer(kind=u2) :: regs(2)
type(modbus_rtu_type) :: modbus
! Create Modbus RTU context and connect to device 10.
rc = dm_modbus_create(modbus = modbus, &
path = '/dev/ttyUSB0', &
baud_rate = TTY_B19200, &
byte_size = TTY_BYTE_SIZE8, &
parity = TTY_PARITY_EVEN, &
stop_bits = TTY_STOP_BITS1)
rc = dm_modbus_connect(modbus)
rc = dm_modbus_set_slave(modbus, slave=10)
! Read and output two registers.
rc = dm_modbus_read_registers(modbus, address=50, registers=regs)
do i = 1, size(registers)
s = dm_to_signed(regs(i))
print '("regs(", i0, ") = ", i0, " (0x", z0, ")")', i, s, s
end do
! Print the two registers as real in ABCD byte order.
print '(f12.8)', dm_modbus_get_real_abcd(regs)
! Disconnect and clean-up.
call dm_modbus_close(modbus)
call dm_modbus_destroy(modbus)
In production, add additional error handling of the return codes.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public, | parameter | :: | MODBUS_REAL_ABCD | = | 0 |
ABCD byte order. |
integer, | public, | parameter | :: | MODBUS_REAL_BADC | = | 1 |
BADC byte order. |
integer, | public, | parameter | :: | MODBUS_REAL_CDAB | = | 2 |
CDBA byte order. |
integer, | public, | parameter | :: | MODBUS_REAL_DCBA | = | 3 |
DCBA byte order. |
Generic function to create Modbus RTU or TCP context.
Creates a new Modbus RTU context.
The function returns the following error codes:
E_INVALID
if a given argument is invalid.E_MODBUS
if no Modbus context could be created.Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(modbus_rtu_type), | intent(out) | :: | modbus |
Modbus RTU type. |
||
character(len=*), | intent(in) | :: | path |
Device path. |
||
integer, | intent(in) | :: | baud_rate |
Baud rate enumerator ( |
||
integer, | intent(in) | :: | byte_size |
Byte size enumerator ( |
||
integer, | intent(in) | :: | parity |
Parity enumerator ( |
||
integer, | intent(in) | :: | stop_bits |
Stop bits enumerator ( |
Creates a new Modbus TCP context.
The function returns the following error codes:
E_INVALID
if the given arguments are invalid.E_MODBUS
if no Modbus context could be created.Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(modbus_tcp_type), | intent(out) | :: | modbus |
Modbus TCP type. |
||
character(len=*), | intent(in) | :: | address |
IPv4 address. |
||
integer, | intent(in) | :: | port |
Port number. |
Opaque Modbus RTU context type.
Opaque Modbus TCP context type.
Returns byte order named parameter associated with given string.
Sets argument byte_order
to MODBUS_REAL_ACBD
if string
is
ABCD
(case insensitive). Returns E_INVALID
and sets byte_order
to MODBUS_REAL_ABCD
if the string is invalid.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | name |
Input string. |
||
integer, | intent(out) | :: | byte_order |
Byte order of real values. |
Connects to Modbus RTU/TCP device.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(modbus_type), | intent(inout) | :: | modbus |
Modbus RTU/TCP type. |
Creates a new Modbus RTU context.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(modbus_rtu_type), | intent(out) | :: | modbus |
Modbus RTU type. |
||
character(len=*), | intent(in) | :: | path |
Device path. |
||
integer, | intent(in) | :: | baud_rate |
Baud rate enumerator ( |
||
integer, | intent(in) | :: | byte_size |
Byte size enumerator ( |
||
integer, | intent(in) | :: | parity |
Parity enumerator ( |
||
integer, | intent(in) | :: | stop_bits |
Stop bits enumerator ( |
Creates a new Modbus TCP context.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(modbus_tcp_type), | intent(out) | :: | modbus |
Modbus TCP type. |
||
character(len=*), | intent(in) | :: | address |
IPv4 address. |
||
integer, | intent(in) | :: | port |
Port number. |
Returns error message from libmodbus.
Flushes non-transmitted data.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(modbus_type), | intent(inout) | :: | modbus |
Modbus RTU/TCP type. |
Returns real value from two registers of given byte order in argument
value
. The argument byte order must be one of the following:
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=u2), | intent(inout) | :: | registers(2) |
Registers to convert. |
||
integer, | intent(in) | :: | byte_order |
Byte order. |
||
integer, | intent(out), | optional | :: | error |
Error code. |
Returns real value from two registers in ABCD byte order.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=u2), | intent(inout) | :: | registers(2) |
Registers to convert. |
Returns real value from two registers in BADC byte order.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=u2), | intent(inout) | :: | registers(2) |
Registers to convert. |
Returns real value from two registers in CDAB byte order.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=u2), | intent(inout) | :: | registers(2) |
Registers to convert. |
Returns real value from two registers in DCBA byte order.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=u2), | intent(inout) | :: | registers(2) |
Registers to convert. |
Gets the current Modbus RTU serial mode (RS-232 or RS-485).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(modbus_rtu_type), | intent(inout) | :: | modbus |
Modbus RTU type. |
||
integer, | intent(out) | :: | mode |
Modbus RTU mode ( |
Gets current slave number in the Modbus context.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(modbus_type), | intent(inout) | :: | modbus |
Modbus RTU/TCP type. |
||
integer, | intent(out) | :: | slave |
Device id. |
Reads many registers from address
. The size of argument
registers
determines the number of registers to read, unless
optional argument n
is passed. The function uses the Modbus
function code 0x03
(read holding registers).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(modbus_type), | intent(inout) | :: | modbus |
Modbus RTU/TCP type. |
||
integer, | intent(in) | :: | address |
Address to read from. |
||
integer(kind=u2), | intent(inout) | :: | registers(:) |
Register values (unsigned). |
||
integer, | intent(inout), | optional | :: | n |
Number of registers to read on input, number of registers read on output. |
Sets debug flag of the Modbus context. Returns E_MODBUS
on error.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(modbus_type), | intent(inout) | :: | modbus |
Modbus RTU/TCP type. |
||
logical, | intent(in) | :: | debug |
Enable debug mode. |
Sets the Modbus RTU serial mode to RS-232 or RS-485. This API function is only supported on Linux kernels 2.6.28 onwards.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(modbus_rtu_type), | intent(inout) | :: | modbus |
Modbus RTU type. |
||
integer, | intent(in) | :: | mode |
Modbus RTU mode ( |
Sets slave number in the Modbus context.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(modbus_type), | intent(inout) | :: | modbus |
Modbus RTU/TCP type. |
||
integer, | intent(in) | :: | slave |
Device id. |
Returns libmodbus version as allocatable string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
logical, | intent(in), | optional | :: | name |
Add prefix |
Version string.
Writes register to address
. The function uses the Modbus function
code 0x06
(preset single register).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(modbus_type), | intent(inout) | :: | modbus |
Modbus RTU/TCP type. |
||
integer, | intent(in) | :: | address |
Address to write to. |
||
integer(kind=u2), | intent(in) | :: | register |
Register value (unsigned). |
Writes many registers to address
. The size of argument registers
determines the number of registers to write, unless optional
argument n
is passed. The function uses the Modbus function code
0x10
(preset multiple registers).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(modbus_type), | intent(inout) | :: | modbus |
Modbus RTU/TCP type. |
||
integer, | intent(in) | :: | address |
Address to write to. |
||
integer(kind=u2), | intent(inout) | :: | registers(:) |
Register values (unsigned). |
||
integer, | intent(inout), | optional | :: | n |
Number of registers to write on input, number of registers written on output. |
Closes the Modbus RTU/TCP connection.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(modbus_type), | intent(inout) | :: | modbus |
Modbus RTU/TCPtype. |
Destroys the Modbus RTU/TCP context.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(modbus_type), | intent(inout) | :: | modbus |
Modbus RTU/TCP type. |
Sets real value to registers of given byte order. The argument byte order must be one of the following:
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real, | intent(in) | :: | value |
Real value to set. |
||
integer(kind=u2), | intent(out) | :: | registers(2) |
Registers to write to. |
||
integer, | intent(in) | :: | byte_order |
Byte order. |
||
integer, | intent(out), | optional | :: | error |
Error code. |
Returns real value to registers in ABCD byte order.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real, | intent(in) | :: | value |
Real value to set. |
||
integer(kind=u2), | intent(out) | :: | registers(2) |
Registers to write to. |
Returns real value to registers in BADC byte order.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real, | intent(in) | :: | value |
Real value to set. |
||
integer(kind=u2), | intent(out) | :: | registers(2) |
Registers to write to. |
Sets real value to registers in CDAB byte order.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real, | intent(in) | :: | value |
Real value to set. |
||
integer(kind=u2), | intent(out) | :: | registers(2) |
Registers to write to. |
Sets real value to registers in DCBA byte order.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real, | intent(in) | :: | value |
Real value to set. |
||
integer(kind=u2), | intent(out) | :: | registers(2) |
Registers to write to. |