Skip to content

Commit 8d89654

Browse files
committed
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 fa6430c commit 8d89654

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

umodbus/serial.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828

2929
class ModbusRTU(Modbus):
30+
INV_RX = UART.INV_RX # Include in class so they can be used when creating the object
31+
INV_TX = UART.INV_TX
32+
3033
"""
3134
Modbus RTU client class
3235
@@ -46,7 +49,10 @@ class ModbusRTU(Modbus):
4649
:type ctrl_pin: int
4750
:param uart_id: The ID of the used UART
4851
:type uart_id: int
52+
:param invert: Invert TX and/or RX pins
53+
:type invert: int
4954
"""
55+
5056
def __init__(self,
5157
addr: int,
5258
baudrate: int = 9600,
@@ -55,7 +61,8 @@ def __init__(self,
5561
parity: Optional[int] = None,
5662
pins: List[Union[int, Pin], Union[int, Pin]] = None,
5763
ctrl_pin: int = None,
58-
uart_id: int = 1):
64+
uart_id: int = 1,
65+
invert: int = None):
5966
super().__init__(
6067
# set itf to Serial object, addr_list to [addr]
6168
Serial(uart_id=uart_id,
@@ -64,20 +71,25 @@ def __init__(self,
6471
stop_bits=stop_bits,
6572
parity=parity,
6673
pins=pins,
67-
ctrl_pin=ctrl_pin),
74+
ctrl_pin=ctrl_pin,
75+
invert=invert),
6876
[addr]
6977
)
7078

7179

7280
class Serial(CommonModbusFunctions):
81+
INV_RX = UART.INV_RX # Include in class so they can be used when creating the object
82+
INV_TX = UART.INV_TX
83+
7384
def __init__(self,
7485
uart_id: int = 1,
7586
baudrate: int = 9600,
7687
data_bits: int = 8,
7788
stop_bits: int = 1,
7889
parity=None,
7990
pins: List[Union[int, Pin], Union[int, Pin]] = None,
80-
ctrl_pin: int = None):
91+
ctrl_pin: int = None,
92+
invert: int = None):
8193
"""
8294
Setup Serial/RTU Modbus
8395
@@ -104,7 +116,8 @@ def __init__(self,
104116
# timeout_chars=2, # WiPy only
105117
# pins=pins # WiPy only
106118
tx=pins[0],
107-
rx=pins[1]
119+
rx=pins[1],
120+
invert=invert
108121
)
109122

110123
if ctrl_pin is not None:

0 commit comments

Comments
 (0)