Skip to content

Commit

Permalink
Implement support for broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
T0RAT0RA committed Jan 12, 2025
1 parent e9ab05a commit c404345
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions umodbus/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
13 changes: 11 additions & 2 deletions umodbus/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(self,
parity=parity,
pins=pins,
ctrl_pin=ctrl_pin),
[addr]
[Const.BROADCAST_ADDR, addr]
)


Expand Down Expand Up @@ -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.
Expand All @@ -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],
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c404345

Please # to comment.