Skip to content

Commit

Permalink
Merge pull request #461 from canton7/bugfix/serial-select
Browse files Browse the repository at this point in the history
Use PosixPollSerial from pyserial for serial connections where possible
  • Loading branch information
canton7 authored Nov 4, 2023
2 parents 592624c + bef5065 commit 1b88ce9
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions custom_components/foxess_modbus/modbus_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The client used to talk Modbus"""
import asyncio
import logging
import os
import select
import socket
import time
Expand Down Expand Up @@ -178,6 +179,13 @@ def __init__(self, hass: HomeAssistant, protocol: str, adapter: InverterAdapter,
"delay_on_connect": 1 if adapter.connection_type == LAN else None,
}

# If PosixPollSerial is supported, use that. This uses poll rather than select, which means we don't break when
# there are more than 1024 fds. See #457.
# Only supported on posix, see https://github.com/pyserial/pyserial/blob/7aeea35429d15f3eefed10bbb659674638903e3a/serial/__init__.py#L31
if protocol == SERIAL and os.name == "posix":
# https://pyserial.readthedocs.io/en/latest/url_handlers.html#alt
config["port"] = f"alt://{config['port']}?class=PosixPollSerial"

# Some serial devices need a short delay after polling. Also do this for the inverter, just
# in case it helps.
self._poll_delay = 30 / 1000 if protocol == SERIAL or adapter.connection_type == LAN else 0
Expand Down

0 comments on commit 1b88ce9

Please sign in to comment.