Skip to content

Commit 13463a0

Browse files
sandyscottbrainelectronics
authored andcommitted
Allow inversion of serial signals
Add "invert" argument to Serial and ModbusRTU classes, and include the INV_RX and INV_TX constants as class variables to be used to fill this argument. This is passed to the UART constructor.
1 parent e9ab05a commit 13463a0

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

umodbus/serial.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727

2828
class ModbusRTU(Modbus):
29+
INV_RX = UART.INV_RX # Include in class so they can be used when creating the object
30+
INV_TX = UART.INV_TX
31+
2932
"""
3033
Modbus RTU client class
3134
@@ -45,7 +48,10 @@ class ModbusRTU(Modbus):
4548
:type ctrl_pin: int
4649
:param uart_id: The ID of the used UART
4750
:type uart_id: int
51+
:param invert: Invert TX and/or RX pins
52+
:type invert: int
4853
"""
54+
4955
def __init__(self,
5056
addr: int,
5157
baudrate: int = 9600,
@@ -54,7 +60,8 @@ def __init__(self,
5460
parity: Optional[int] = None,
5561
pins: List[Union[int, Pin], Union[int, Pin]] = None,
5662
ctrl_pin: int = None,
57-
uart_id: int = 1):
63+
uart_id: int = 1,
64+
invert: int = None):
5865
super().__init__(
5966
# set itf to Serial object, addr_list to [addr]
6067
Serial(uart_id=uart_id,
@@ -63,20 +70,25 @@ def __init__(self,
6370
stop_bits=stop_bits,
6471
parity=parity,
6572
pins=pins,
66-
ctrl_pin=ctrl_pin),
73+
ctrl_pin=ctrl_pin,
74+
invert=invert),
6775
[addr]
6876
)
6977

7078

7179
class Serial(CommonModbusFunctions):
80+
INV_RX = UART.INV_RX # Include in class so they can be used when creating the object
81+
INV_TX = UART.INV_TX
82+
7283
def __init__(self,
7384
uart_id: int = 1,
7485
baudrate: int = 9600,
7586
data_bits: int = 8,
7687
stop_bits: int = 1,
7788
parity=None,
7889
pins: List[Union[int, Pin], Union[int, Pin]] = None,
79-
ctrl_pin: int = None):
90+
ctrl_pin: int = None,
91+
invert: int = None):
8092
"""
8193
Setup Serial/RTU Modbus
8294
@@ -105,7 +117,8 @@ def __init__(self,
105117
# timeout_chars=2, # WiPy only
106118
# pins=pins # WiPy only
107119
tx=pins[0],
108-
rx=pins[1]
120+
rx=pins[1],
121+
invert=invert
109122
)
110123

111124
if ctrl_pin is not None:

0 commit comments

Comments
 (0)