Skip to content

Allow inversion of serial signals #74

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 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- ## [Unreleased] -->

## Released
## [2.4.0] - 2023-07-20
### Added
- The following fixes were provided by @sandyscott
- UART signals can be inverted with the `invert` argument of the `Serial` and `ModbusRTU` class constructors

## [2.3.7] - 2023-07-19
### Fixed
- Add a single character wait time after flush to avoid timing issues with RTU control pin, see #68 and #72
Expand Down Expand Up @@ -307,8 +312,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.3.7...develop
[Unreleased]: https://github.com/brainelectronics/micropython-modbus/compare/2.4.0...develop

[2.4.0]: https://github.com/brainelectronics/micropython-modbus/tree/2.4.0
[2.3.7]: https://github.com/brainelectronics/micropython-modbus/tree/2.3.7
[2.3.6]: https://github.com/brainelectronics/micropython-modbus/tree/2.3.6
[2.3.5]: https://github.com/brainelectronics/micropython-modbus/tree/2.3.5
Expand Down
8 changes: 7 additions & 1 deletion fakes/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class UART(object):

See https://docs.micropython.org/en/latest/library/machine.UART.html
"""
# ESP32: TX=32, RX=4
# RP2: TX=1, RX=2
INV_RX = 4
INV_TX = 32

def __init__(self,
uart_id: int,
baudrate: int = 9600,
Expand All @@ -35,7 +40,8 @@ def __init__(self,
stop: int = 1,
tx: int = 1,
rx: int = 2,
timeout: int = 0) -> None:
timeout: int = 0,
invert: int = 0) -> None:
self._uart_id = uart_id
if timeout == 0:
timeout = 5.0
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@
]
],
"deps": [],
"version": "2.3.7"
"version": "2.4.0"
}
25 changes: 21 additions & 4 deletions umodbus/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@


class ModbusRTU(Modbus):
# Include in class so they can be used when creating the object
INV_RX = UART.INV_RX
INV_TX = UART.INV_TX

"""
Modbus RTU client class

Expand All @@ -45,7 +49,10 @@ class ModbusRTU(Modbus):
:type ctrl_pin: int
:param uart_id: The ID of the used UART
:type uart_id: int
:param invert: Invert TX and/or RX pins
:type invert: int
"""

def __init__(self,
addr: int,
baudrate: int = 9600,
Expand All @@ -54,7 +61,8 @@ def __init__(self,
parity: Optional[int] = None,
pins: List[Union[int, Pin], Union[int, Pin]] = None,
ctrl_pin: int = None,
uart_id: int = 1):
uart_id: int = 1,
invert: int = 0):
super().__init__(
# set itf to Serial object, addr_list to [addr]
Serial(uart_id=uart_id,
Expand All @@ -63,20 +71,26 @@ def __init__(self,
stop_bits=stop_bits,
parity=parity,
pins=pins,
ctrl_pin=ctrl_pin),
ctrl_pin=ctrl_pin,
invert=invert),
[addr]
)


class Serial(CommonModbusFunctions):
# Include in class so they can be used when creating the object
INV_RX = UART.INV_RX
INV_TX = UART.INV_TX

def __init__(self,
uart_id: int = 1,
baudrate: int = 9600,
data_bits: int = 8,
stop_bits: int = 1,
parity=None,
pins: List[Union[int, Pin], Union[int, Pin]] = None,
ctrl_pin: int = None):
ctrl_pin: int = None,
invert: int = 0):
"""
Setup Serial/RTU Modbus

Expand All @@ -94,6 +108,8 @@ def __init__(self,
:type pins: List[Union[int, Pin], Union[int, Pin]]
:param ctrl_pin: The control pin
:type ctrl_pin: int
:param invert: Invert TX and/or RX pins
:type invert: int
"""
# UART flush function is introduced in Micropython v1.20.0
self._has_uart_flush = callable(getattr(UART, "flush", None))
Expand All @@ -105,7 +121,8 @@ def __init__(self,
# timeout_chars=2, # WiPy only
# pins=pins # WiPy only
tx=pins[0],
rx=pins[1]
rx=pins[1],
invert=invert
)

if ctrl_pin is not None:
Expand Down
2 changes: 1 addition & 1 deletion umodbus/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

__version_info__ = ("2", "3", "7")
__version_info__ = ("2", "4", "0")
__version__ = '.'.join(__version_info__)