Skip to content

ModbusTcpClient - Second Request always fails #612

@vpetrovych

Description

@vpetrovych

Versions

Pymodbus Specific

  • Client: tcp - sync

Description

What were you trying, what has happened, what went wrong, and what did you expect?
When trying to send 2nd request, an error is returned
[Connection] ModbusTcpClient(192.168.0.15:502): Connection unexpectedly closed 0.000886 seconds into read of 8 bytes without response from unit before it closed connection

It doesn't matter if I read first input and second holding, or vice versa. It always fails the second request.
Removing the ClearCountersRequest also doesn't help
This worked correctly in Node.JS using jsmodbus package, so I have a feeling there could be a bug here.

Code and Logs

try:
            client = ModbusClient(self.args['modbus_host'], port=self.args['modbus_port'], timeout= 20)
            client.connect()
            self.log('connected')


            # client.connect()
            # self.log('connected 2')

            self.log('read input')
            rq = client.read_input_registers(address=0, count=5)
            self.log(rq)
            self.log(rq.isError())
            # self.log(input_registers.registers)
            # client.close()

            self.log('reset')
            rq = ClearCountersRequest()
            rr = client.execute(rq)
            self.log(rr)
            
            self.log('read holding')
            # client = ModbusClient(self.args['modbus_host'], port=self.args['modbus_port'])
            # client.connect()
            holding_registers = client.read_holding_registers(address=0, count=3)
            # self.log(holding_registers.registers)

            client.close()
            # self.mqtt_publish("homeassistant/bedroom/light", "ON")

        except Exception as inst:
            self.error('Modbus error {}'.format(inst))

            rq = ReadExceptionStatusRequest(unit=255)
            rr = client.execute(rq)
            self.log(rr)
            self.log(rr.isError())

logs

DEBUG:pymodbus.client.sync:Connection to Modbus server established. Socket ('172.30.33.3', 33633)
2021-03-05 12:46:18.023444 INFO vent: connected
2021-03-05 12:46:18.026330 INFO vent: read input
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x4 0x0 0x0 0x0 0x5
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x0 0x1 0x0 0x0 0x0 0xd 0x0 0x4 0xa 0x0 0xe8 0x0 0xc1 0x5 0xdb 0x0 0x0 0x0 0x0
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x1 0x0 0x0 0x0 0xd 0x0 0x4 0xa 0x0 0xe8 0x0 0xc1 0x5 0xdb 0x0 0x0 0x0 0x0
DEBUG:pymodbus.factory:Factory Response[ReadInputRegistersResponse: 4]
DEBUG:pymodbus.transaction:Adding transaction 1
DEBUG:pymodbus.transaction:Getting transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
2021-03-05 12:46:18.102485 INFO vent: ReadInputRegistersResponse (5)
2021-03-05 12:46:18.104008 INFO vent: False
2021-03-05 12:46:18.106786 INFO vent: reset
DEBUG:pymodbus.client.sync:Connection to Modbus server established. Socket ('172.30.33.3', 36917)
DEBUG:pymodbus.transaction:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.transaction:Running transaction 2
DEBUG:pymodbus.transaction:SEND: 0x0 0x2 0x0 0x0 0x0 0x6 0x0 0x8 0x0 0xa 0x0 0x0
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2021-03-05 12:46:18.318106 INFO vent: Modbus error Modbus Error: [Connection] ModbusTcpClient(192.168.0.15:502): Connection unexpectedly closed 0.000886 seconds into read of 8 bytes without response from unit before it closed connection
DEBUG:pymodbus.client.sync:Connection to Modbus server established. Socket ('172.30.33.3', 45947)
DEBUG:pymodbus.transaction:Current transaction state - WAITING_FOR_REPLY
DEBUG:pymodbus.transaction:Running transaction 3
DEBUG:pymodbus.transaction:SEND: 0x0 0x3 0x0 0x0 0x0 0x2 0xff 0x7
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x0 0x3 0x0 0x0 0x0 0x3 0xff 0x87 0x1
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x3 0x0 0x0 0x0 0x3 0xff 0x87 0x1
DEBUG:pymodbus.factory:Factory Response[135]
DEBUG:pymodbus.transaction:Adding transaction 3
DEBUG:pymodbus.transaction:Getting transaction 3
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
2021-03-05 12:46:18.390142 INFO vent: Exception Response(135, 7, IllegalFunction)
2021-03-05 12:46:18.391457 INFO vent: True

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions