Skip to content

Implement support for broadcast #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please move this new part up to around line 105, just to not mix up any non existing order ;) I still want to keep the CRC part at the end of the file



# 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
Loading