Skip to content

Server not returning data expected in data block #2185

@jcameron-sso

Description

@jcameron-sso

Versions

  • Python: 3.8.10
  • OS: Ubuntu 20.04
  • Pymodbus: 3.6.8
  • Modbus Hardware (if used): none

Pymodbus Specific

  • Server: tcp sync
  • Client: tcp sync

Description

Trying to read registers from a server, data was returned, but data is zero, expected data to be as supplied. tcpdump showed server as cause.

Code and Logs

#!/usr/bin/python3

import pymodbus
pymodbus.pymodbus_apply_logging_config("DEBUG")
from pymodbus.server import StartTcpServer
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext

data_registers = [173, 8, 380, 168, 133, 192]

# Create a Modbus data block with your data
block = ModbusSequentialDataBlock(0, data_registers)

# Create a slave context and add your data block
context = ModbusSlaveContext(slaves={1: block})

# Create a server context
server_context = ModbusServerContext(slaves=context, single=True)

# Start the Modbus TCP server
StartTcpServer(context=server_context, address=("0.0.0.0", 32502))

"""
logs

2024-05-03 10:31:08,435 DEBUG logging:103 Awaiting connections server_listener
2024-05-03 10:31:08,435 INFO  logging:97 Server listening.
2024-05-03 10:31:10,892 DEBUG logging:103 Connected to server
2024-05-03 10:31:10,892 DEBUG logging:103 recv: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x0 0x0 0x0 0x6 old_data:  addr=None
2024-05-03 10:31:10,892 DEBUG logging:103 Handling data: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x0 0x0 0x0 0x6
2024-05-03 10:31:10,893 DEBUG logging:103 Processing: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x0 0x0 0x0 0x6
2024-05-03 10:31:10,893 DEBUG logging:103 Factory Request[ReadHoldingRegistersRequest': 3]
2024-05-03 10:31:10,893 DEBUG logging:103 validate: fc-[3] address-1: count-6
2024-05-03 10:31:10,893 DEBUG logging:103 getValues: fc-[3] address-1: count-6
2024-05-03 10:31:10,893 DEBUG logging:103 send: 0x0 0x1 0x0 0x0 0x0 0xf 0x0 0x3 0xc 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
2024-05-03 10:31:10,893 DEBUG logging:103 Frame check, no more data!
2024-05-03 10:31:10,894 DEBUG logging:103 -> transport: received eof
2024-05-03 10:31:10,894 DEBUG logging:103 Connection lost server due to None
2024-05-03 10:31:10,894 DEBUG logging:103 Handler for stream [server] has been canceled

tcpdump wireshark follow tcp stream

rx 00 01 00 00 00 06 00 03 00 00 00 06
tx 00 01 00 00 00 0f 00 03 0c 00 00 00 00 00 00 00 00 00 00 00 00

test client

#!/usr/bin/python3

from pymodbus.client import ModbusTcpClient

server_ip = "127.0.0.1"
server_port = 32502

client = ModbusTcpClient(server_ip, port=server_port)

try:
    client.connect()

    start_address = 0
    num_registers = 6  # Read 10 registers starting from address 0
    response = client.read_holding_registers(0, 6, unit=1)

    if response.isError():
        print(f"Error reading data: {response}")
    else:
        # Extract the values from the response
        data_values = response.registers
        print(f"Read data: {data_values}")
        print(f"Raw response: {response}")
        print(f"Registers:C{response.registers}")
finally:
    # Close the client connection
    client.close()

test client output

Read data: [0, 0, 0, 0, 0, 0]
Raw response: ReadHoldingRegistersResponse (6)
Registers:C[0, 0, 0, 0, 0, 0]

"""

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions