tiagocoutinho / async_modbus

Async python modbus client library
Other
9 stars 7 forks source link

Readexactly attribute error #1

Closed kellerza closed 2 years ago

kellerza commented 2 years ago

Description

Connecting to serial:///dev/ttyUSB0 I get a AttributeError: 'Serial' object has no attribute 'readexactly'

Traceback (most recent call last):
  File "/usr/src/app/./run.py", line 251, in <module>
    LOOP.run_until_complete(main(LOOP))
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/usr/src/app/./run.py", line 191, in main
    await SUNSYNK.connect(timeout=OPT.timeout)
  File "/usr/src/app/usunsynk.py", line 18, in connect
    self.client = modbus_for_url(self.port)
  File "/usr/local/lib/python3.9/dist-packages/async_modbus.py", line 398, in modbus_for_url
    return AsyncRTUClient(stream)
  File "/usr/local/lib/python3.9/dist-packages/async_modbus.py", line 260, in __init__
    self.stream = _Stream(stream)
  File "/usr/local/lib/python3.9/dist-packages/async_modbus.py", line 228, in __init__
    self.readexactly = self.reader.readexactly
AttributeError: 'Serial' object has no attribute 'readexactly'
tiagocoutinho commented 2 years ago

Thanks for reporting the problem.

I just made a quick fix and a released on pypi version 0.1.4 with that fix.

Let me know if it works

kellerza commented 2 years ago

Thank you, everything seems ok! 👍

Not sure how you would change the baudrate/other settings with modbus_for_url, but for now it seems to connect fine on 9600 8N1.

I'm testing your library as an alternative to pymodbus https://github.com/kellerza/sunsynk/issues/18 I like the automatic connect, but getting numpy in the container was not that easy (originally an Alpine build, now Debian)

kellerza commented 2 years ago

Would you be open to accept a PR to make numpy optional?

something like

try:
    import numpy  # noqa
except ImportError:
    pass
else:
    import async_modbus_use_numpy  # noqa
tiagocoutinho commented 2 years ago

To use a different serial settings:

>>> mb = async_modbus.modbus_for_url("/dev/ttyS0", {"baudrate": 19200, "parity": "E"})

>>> # confirm with:
>>> mb.stream.reader
Serial<id=0x7fae08477e50, open=False>(port='/dev/ttyS0', baudrate=19200, bytesize=8, parity='E', stopbits=1, xonxoff=False, rtscts=False, dsrdtr=False)
tiagocoutinho commented 2 years ago

Regarding making numpy optional sounds interesting.

I'm happy to accept something that makes numpy not required by default and add an extra in setup something like:

pip install async_modbus[numpy]