Skip to content

Commit

Permalink
Merge pull request AdvancedClimateSystems#42 from brainelectronics/bu…
Browse files Browse the repository at this point in the history
…gfix/use-most-generic-uart-functions

Use most generic UART functions
  • Loading branch information
brainelectronics committed Dec 29, 2022
2 parents 4699684 + b43e5db commit ebb8258
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 10 additions & 1 deletion changelog.md
Expand Up @@ -15,6 +15,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- ## [Unreleased] -->

## Released
## [2.1.2] - 2022-12-28
### Changed
- Baudrate specific inter frame time is used at Modbus RTU internal function `_uart_read` of [serial.py](umodbus/serial.py) instead of constant value of 5ms

### Fixed
- ESP32 port specific `wait_tx_done` function replaced by generic wait time calculation in `_send` function of [serial.py](umodbus/serial.py), see #34
- A 1ms delay has been added between turning the RS485 control pin on and sending the Modbus PDU in `_send` function of [serial.py](umodbus/serial.py)

## [2.1.1] - 2022-12-27
### Fixed
- Removed unnecessary dependency to `micropython-urequests` from Docker files, setup guide and package setup file
Expand Down Expand Up @@ -182,8 +190,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- PEP8 style issues on all files of [`lib/uModbus`](lib/uModbus)

<!-- Links -->
[Unreleased]: https://github.com/brainelectronics/micropython-modbus/compare/2.1.1...develop
[Unreleased]: https://github.com/brainelectronics/micropython-modbus/compare/2.1.2...develop

[2.1.2]: https://github.com/brainelectronics/micropython-modbus/tree/2.1.2
[2.1.1]: https://github.com/brainelectronics/micropython-modbus/tree/2.1.1
[2.1.0]: https://github.com/brainelectronics/micropython-modbus/tree/2.1.0
[2.0.0]: https://github.com/brainelectronics/micropython-modbus/tree/2.0.0
Expand Down
11 changes: 8 additions & 3 deletions umodbus/serial.py
Expand Up @@ -109,6 +109,7 @@ def __init__(self,
else:
self._ctrlPin = None

self._t1char = (1000000 * (data_bits + stop_bits + 2)) // baudrate
if baudrate <= 19200:
# 4010us (approx. 4ms) @ 9600 baud
self._t35chars = (3500000 * (data_bits + stop_bits + 2)) // baudrate
Expand Down Expand Up @@ -172,7 +173,9 @@ def _uart_read(self) -> bytearray:
# variable length function codes may require multiple reads
if self._exit_read(response):
break
time.sleep(0.05)

# wait for the maximum time between two frames
time.sleep_us(self._t35chars)

return response

Expand Down Expand Up @@ -245,13 +248,15 @@ def _send(self, modbus_pdu: bytes, slave_addr: int) -> None:

if self._ctrlPin:
self._ctrlPin(1)
time.sleep_us(1000) # wait until the control pin really changed
send_start_time = time.ticks_us()

self._uart.write(serial_pdu)

if self._ctrlPin:
while not self._uart.wait_tx_done(2):
total_frame_time_us = self._t1char * len(serial_pdu)
while time.ticks_us() <= send_start_time + total_frame_time_us:
machine.idle()
time.sleep_us(self._t35chars)
self._ctrlPin(0)

def _send_receive(self,
Expand Down

0 comments on commit ebb8258

Please sign in to comment.