Skip to content

ModbusClient auto_open #90

@gtortone

Description

@gtortone

Hi,
I'm using pyModbusTCP to read 19 devices on Modbus with mbusd (on same host).

Everything seems to work fine but if set auto_open=True the TCP socket after mbusd timeout (60s) is
closed and it is not re-opened at first access on Modbus but it is working since second access...

A call to close() and open() don't help and the behaviour is always the same: the socket is reopened starting
from second access on the bus.

To test this I'm using following Python script:

#!/usr/bin/env python3

import time
from pyModbusTCP.client import ModbusClient

def on_tx_rx(frame: bytes, is_tx: bool):
   if is_tx:
      print(f'[tx] {frame}')
   else:
      print(f'[rx] {frame}')

c = ModbusClient(host="localhost", auto_open=True, auto_close=False, timeout=120)
c.on_tx_rx = on_tx_rx

value = 25

for ch in range(1,20):
   c.unit_id = ch
   c.write_single_register(36, value)
   print(f'W {ch} {value}')

while True:
   for ch in range(1,20):
      c.unit_id = ch
      if(c.is_open == False):
         c.close()
         c.open()
      rup = c.read_holding_registers(36, 1)
      print(f'R {ch} {rup} is_open: {c.is_open}')

   time.sleep(60)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions