Skip to content

Commit c404345

Browse files
committed
Implement support for broadcast
1 parent e9ab05a commit c404345

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

umodbus/const.py

+3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@
137137
0x4100, 0x81C1, 0x8081, 0x4040
138138
)
139139

140+
#: Broadcast address
141+
BROADCAST_ADDR = const(0x00)
142+
140143

141144
# Code to generate the CRC-16 lookup table:
142145
# def generate_crc16_table():

umodbus/serial.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(self,
6464
parity=parity,
6565
pins=pins,
6666
ctrl_pin=ctrl_pin),
67-
[addr]
67+
[Const.BROADCAST_ADDR, addr]
6868
)
6969

7070

@@ -292,7 +292,7 @@ def _send(self, modbus_pdu: bytes, slave_addr: int) -> None:
292292
def _send_receive(self,
293293
modbus_pdu: bytes,
294294
slave_addr: int,
295-
count: bool) -> bytes:
295+
count: bool) -> bytes|None:
296296
"""
297297
Send a modbus message and receive the reponse.
298298
@@ -311,6 +311,10 @@ def _send_receive(self,
311311

312312
self._send(modbus_pdu=modbus_pdu, slave_addr=slave_addr)
313313

314+
if slave_addr == Const.BROADCAST_ADDR:
315+
# Do not wait for response after a broadcast
316+
return None
317+
314318
return self._validate_resp_hdr(response=self._uart_read(),
315319
slave_addr=slave_addr,
316320
function_code=modbus_pdu[0],
@@ -386,6 +390,11 @@ def send_response(self,
386390
:param signed: Indicates if signed
387391
:type signed: bool
388392
"""
393+
394+
if slave_addr == Const.BROADCAST_ADDR:
395+
# Do not reply to broadcast messages
396+
return
397+
389398
modbus_pdu = functions.response(
390399
function_code=function_code,
391400
request_register_addr=request_register_addr,

0 commit comments

Comments
 (0)