Skip to content

Improve uart_read_frame#5

Merged
GimmickNG merged 1 commit intoGimmickNG:mpmodbus-compatibilityfrom
hmaerki:improve-uart_read_frame
Dec 31, 2023
Merged

Improve uart_read_frame#5
GimmickNG merged 1 commit intoGimmickNG:mpmodbus-compatibilityfrom
hmaerki:improve-uart_read_frame

Conversation

@hmaerki
Copy link

@hmaerki hmaerki commented Dec 31, 2023

This is a request to replace async def _uart_read_frame().

Problem 1: The current implementation of async def _uart_read_frame() contains two nested loops. Why? The timeout calculation does not seem to match the modus-spec.
Problem 2: Line await asyncio.sleep_ms(self._inter_frame_delay // 10) # 175 ms, arbitrary for now.
_inter_frame_delay is in micro seconds, hoever used in sleep_ms. Therefore, the timeout is way too long

My proposal uses only 1 loop: The frame is finished, when after the last character which was successfully read there is silence of 1.5 characters (or 4.5 characters).

I tested the following code on a RP2 measuring the times with the scope.

@GimmickNG GimmickNG merged commit fd694b5 into GimmickNG:mpmodbus-compatibility Dec 31, 2023
@GimmickNG
Copy link
Owner

@hmaerki Thanks for the patch! I hadn't worked with Modbus RTU directly so my earlier code was rather arbitrary based on the results of @beyonlo's testing. This should now hopefully be more robust :)

RustyWraith pushed a commit to RustyWraith/modbus_async that referenced this pull request Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants