From c4043451b5a3a405f2d3a7d4678dbc402557e010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Reiss?= Date: Sat, 11 Jan 2025 21:24:46 -0500 Subject: [PATCH 1/4] Implement support for broadcast --- umodbus/const.py | 3 +++ umodbus/serial.py | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/umodbus/const.py b/umodbus/const.py index eb940be..f7bb5cc 100644 --- a/umodbus/const.py +++ b/umodbus/const.py @@ -137,6 +137,9 @@ 0x4100, 0x81C1, 0x8081, 0x4040 ) +#: Broadcast address +BROADCAST_ADDR = const(0x00) + # Code to generate the CRC-16 lookup table: # def generate_crc16_table(): diff --git a/umodbus/serial.py b/umodbus/serial.py index 11b8bee..70e9455 100644 --- a/umodbus/serial.py +++ b/umodbus/serial.py @@ -64,7 +64,7 @@ def __init__(self, parity=parity, pins=pins, ctrl_pin=ctrl_pin), - [addr] + [Const.BROADCAST_ADDR, addr] ) @@ -292,7 +292,7 @@ def _send(self, modbus_pdu: bytes, slave_addr: int) -> None: def _send_receive(self, modbus_pdu: bytes, slave_addr: int, - count: bool) -> bytes: + count: bool) -> bytes|None: """ Send a modbus message and receive the reponse. @@ -311,6 +311,10 @@ def _send_receive(self, self._send(modbus_pdu=modbus_pdu, slave_addr=slave_addr) + if slave_addr == Const.BROADCAST_ADDR: + # Do not wait for response after a broadcast + return None + return self._validate_resp_hdr(response=self._uart_read(), slave_addr=slave_addr, function_code=modbus_pdu[0], @@ -386,6 +390,11 @@ def send_response(self, :param signed: Indicates if signed :type signed: bool """ + + if slave_addr == Const.BROADCAST_ADDR: + # Do not reply to broadcast messages + return + modbus_pdu = functions.response( function_code=function_code, request_register_addr=request_register_addr, From 1c9f6d8ac97d133a8eb14e70c166d0a4c9e76f1a Mon Sep 17 00:00:00 2001 From: T0RAT0RA Date: Sun, 12 Jan 2025 08:26:21 -0500 Subject: [PATCH 2/4] Apply oldschool typing to be backward compatible with older micropython versions Co-authored-by: Jones --- umodbus/serial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/umodbus/serial.py b/umodbus/serial.py index 70e9455..67222c0 100644 --- a/umodbus/serial.py +++ b/umodbus/serial.py @@ -292,7 +292,7 @@ def _send(self, modbus_pdu: bytes, slave_addr: int) -> None: def _send_receive(self, modbus_pdu: bytes, slave_addr: int, - count: bool) -> bytes|None: + count: bool) -> Union[bytes, None]: """ Send a modbus message and receive the reponse. From ee6a041d9b962257bce1e8d0d92e3309079dce9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Reiss?= Date: Sun, 12 Jan 2025 08:27:25 -0500 Subject: [PATCH 3/4] Keep CRC at the end of the file --- umodbus/const.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/umodbus/const.py b/umodbus/const.py index f7bb5cc..7ba71de 100644 --- a/umodbus/const.py +++ b/umodbus/const.py @@ -104,6 +104,9 @@ #: Modbus Application Protocol High Data Response length MBAP_HDR_LENGTH = const(0x07) +#: Broadcast address +BROADCAST_ADDR = const(0x00) + #: CRC16 lookup table CRC16_TABLE = ( 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, @@ -137,9 +140,6 @@ 0x4100, 0x81C1, 0x8081, 0x4040 ) -#: Broadcast address -BROADCAST_ADDR = const(0x00) - # Code to generate the CRC-16 lookup table: # def generate_crc16_table(): From 744784f0fcf68e7901d92ae77c3cdc9e3292ea27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Reiss?= Date: Sun, 12 Jan 2025 17:02:52 -0500 Subject: [PATCH 4/4] Upgrade Twine to 6.* to fix CI pipeline --- requirements-deploy.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-deploy.txt b/requirements-deploy.txt index 96b0350..2d1bf95 100644 --- a/requirements-deploy.txt +++ b/requirements-deploy.txt @@ -1,5 +1,5 @@ # List external packages here # Avoid fixed versions # # to upload package to PyPi or other package hosts -twine>=4.0.1,<5 +twine==6.* changelog2version>=0.5.0,<1