7572 questions

9025 answers

14571 comments

11781 members

0 votes
609 views 2 comments
by
Hello,

I'm looking for an example of a bash script line to execute a Modbus TCP read request to a slave.

I've searched a lot but I didn't find anything in the documentation.
I need, at initialization, to execute some read/write requests on a Modbus slave and then configure the Modbus_tcp_master.

Thanks

1 Answer

+1 vote
by

Hi,

You can use the modbus_tcp_test command to gather slave register values through CLI.

EB.

by

These Modbus functions are supported:

  • 1 (read coils)
  • 2 (read inputs)
  • 3 (read input registers)
  • 4 (read holding registers)
  • 5 (write to a single coil)
  • 6 (write to a single holding register)
  • 15 (write to multiple coils)
  • 16 (write to multiple holding registers)

It supports these data types (currently only for reading functions: 1, 2, 3, 4)

  • 8-bit signed integer
  • 8-bit unsigned integer
  • 16-bit signed integer, big-endian (byter order 1, 2)
  • 16-bit signed integer, little-endian (byter order 2, 1)
  • 16-bit unsigned integer, big-endian (byte order 1, 2)
  • 16-bit unsigned integer, little-endian (byte order 2, 1)
  • 32-bit signed integer, big-endian (byte order 1, 2, 3, 4)
  • 32-bit signed integer, little-endian (byte order 4, 3, 2, 1)
  • 32-bit signed integer, byte order 2, 1, 4, 3
  • 32-bit signed integer, byte order 3, 4, 1, 2
  • 32-bit unsigned integer, big-endian (byte order 1, 2, 3, 4)
  • 32-bit unsigned integer, little-endian (byte order 4, 3, 2, 1)
  • 32-bit unsigned integer, byte order 2, 1, 4, 3
  • 32-bit unsigned integer, byte order 3, 4, 1, 2
  • 32-bit IEEE 754 single-precision floating-point number, big-endian (byte order 1, 2, 3, 4)
  • 32-bit IEEE 754 single-precision floating-point number, little-endian (byte order 4, 3, 2, 1)
  • 32-bit IEEE 754 single-precision floating-point number, byte order 2, 1, 4, 3
  • 32-bit IEEE 754 single-precision floating-point number, byte order 3, 4, 1, 2
  • HEX
  • ASCII

Usage

modbus_tcp_test IP PORT TIMEOUT SLAVE_ID FUNCTION REG_ADDRESS REG_COUNT/VALUE(S) DATA_TYPE

  • IP
    • An IPv4 address of the Modbus TCP slave to be tested
  • PORT
    • A port (a number in range [0;65535]) which the Modbus TCP slave to be tested is listening to
  • TIMEOUT
    • TCP connection timeout, in seconds, in range [1;30]
  • SLAVE_ID
    • The slave ID of the Modbus TCP slave to be tested, in range [1;255]
  • FUNCTION
    • The Modbus function to be performed: one of 1, 2, 3, 4, 5, 6, 15, 16
  • REG_ADDRESS
    • From/to which register address the Modbus function will be performed
  • REG_COUNT/VALUE(S)
    • If FUNCTION is 1 or 2:
      • How many coils to read, a number in range [1;2000]
    • If FUNCTION is 3 or 4:
      • How many registers to read, a number in range [1;125]
    • If FUNCTION is 5
      • What coil value to write, either 0 or 65280 (0xFF00)
    • If FUNCTION is 6
      • What register value to write, a number in range [0;65535]
    • If FUNCTION is 15
      • What coil values to write: a sequence of up to 246 8-bit unsigned integers (numbers in range [0;255]) separated by spaces, each representing 8 coils to set
        • For example, 123 54 22 sets 24 coils to values: 01111011 00110110 00010110
    • If FUNCTION is 16
      • What register values to write a sequence of up to 123 16-bit unsigned integers (numbers in range [0;65535]) separated by spaces
  • DATA_TYPE
    • Only relevant when reading data from the Modbus TCP slave (functions 1, 2, 3, 4)
    • Must be specified but is ignored when FUNCTION is 5, 6, 15, or 16
    • Specifies how the data retrieved from the Modbus TCP slave will be interpreted
    • For example, we read three registers and received four bytes: AA BB CC DD EE FF
      • When interpreted as HEX, will be interpreted as AA BB CC DD EE FF
      • When interpreted as 16-bit unsigned big-endian integers, will be interpreted as 43707 552455 61183
      • When interpreted as 32-bit unsigned big-endian integers, will be interpreted as 2864434397
        • (not enough to make another number because only two more bytes of data are available)
by
Thanks a lot ! It will be very useful.