Skip to content

Commit

Permalink
Allow inversion of serial signals
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sandyscott authored and brainelectronics committed Jul 2, 2023
1 parent b9a3901 commit 443974d
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions umodbus/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@


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

"""
Modbus RTU client class
Expand All @@ -45,7 +48,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 +60,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 = None):
super().__init__(
# set itf to Serial object, addr_list to [addr]
Serial(uart_id=uart_id,
Expand All @@ -63,20 +70,25 @@ 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):
INV_RX = UART.INV_RX # Include in class so they can be used when creating the object
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 = None):
"""
Setup Serial/RTU Modbus
Expand Down Expand Up @@ -105,7 +117,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

0 comments on commit 443974d

Please sign in to comment.