Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModbusClient auto_open #90

Open
gtortone opened this issue Nov 29, 2024 · 1 comment
Open

ModbusClient auto_open #90

gtortone opened this issue Nov 29, 2024 · 1 comment

Comments

@gtortone
Copy link

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)
@sourceperl
Copy link
Owner

Hi, I'm not sure I understand your use case.

However, here are some notes/tips that I hope will help you:

  • avoid using a large timeout unless you are dealing with a very slow network or server (120s seems really huge to me).
  • socket I/O is only triggered when a request like "read_holding_registers" is issued. So if you try to read from a recently closed TCP socket (from the server's point of view, from the client's point of view, or both), that read will fail (return None). Only the next request will try to reopen the TCP link (when auto_open is set to True, otherwise you have to call open()).
  • if you want to manage TCP link yourself (with is_open, open() or close() method) just set auto_open and auto_close to False.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants