Skip to content
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

Remove hardcoded UART ID from Modbus RTU #45

Merged
Merged
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.1.3] - 2022-12-30
### Fixed
- `uart_id` can be specified during init of `ModbusRTU` and `Serial` class and is no longer hardcoded to `1`, but set as `1` by default to ensure backwards compability, see #7 and #43
- RTU Client example and USAGE documentation updated with new `uart_id` parameter

## [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
Expand Down Expand Up @@ -190,8 +195,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.2...develop
[Unreleased]: https://github.com/brainelectronics/micropython-modbus/compare/2.1.3...develop

[2.1.3]: https://github.com/brainelectronics/micropython-modbus/tree/2.1.3
[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
Expand Down
27 changes: 27 additions & 0 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ based on TCP togehter with the latest provided
All described functions require a successful setup of a Host communicating
to/with a Client device which is providing the data and accepting the new data.

#### TCP

```python
from umodbus.tcp import TCP as ModbusTCPMaster

Expand All @@ -254,6 +256,31 @@ host = ModbusTCPMaster(
timeout=5) # optional, default 5
```

#### RTU

```python
from umodbus.serial import Serial as ModbusRTUMaster

slave_addr = 10 # bus address of client

# check MicroPython UART documentation
# https://docs.micropython.org/en/latest/library/machine.UART.html
# for Device/Port specific setup
# RP2 needs "rtu_pins = (Pin(4), Pin(5))" whereas ESP32 can use any pin
# the following example is for an ESP32
rtu_pins = (25, 26) # (TX, RX)
host = ModbusRTUMaster(
addr=1, # bus address of this Host/Master, usually '1'
baudrate=9600, # optional, default 9600
pins=rtu_pins, # given as tuple (TX, RX)
# data_bits=8, # optional, default 8
# stop_bits=1, # optional, default 1
# parity=None, # optional, default None
# ctrl_pin=12, # optional, control DE/RE
# uart_id=1 # optional, see port specific documentation
)
```

#### Coils

Coils represent binary states, which can be get as and set to either `0` (off)
Expand Down
28 changes: 21 additions & 7 deletions examples/rtu_client_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
Create a Modbus RTU client (slave) which can be requested for data or set with
specific values by a host device.

The RTU communication pins can be choosen freely. The register definitions of
the client as well as its connection settings like bus address and UART
communication speed can be defined by the user.
The RTU communication pins can be choosen freely (check MicroPython device/
port specific limitations).
The register definitions of the client as well as its connection settings like
bus address and UART communication speed can be defined by the user.
"""

# import modbus client classes
Expand All @@ -21,20 +22,33 @@
# RTU Slave setup
# act as client, provide Modbus data via RTU to a host device
# ModbusRTU can get serial requests from a host device to provide/set data
# check MicroPython UART documentation
# https://docs.micropython.org/en/latest/library/machine.UART.html
# for Device/Port specific setup
# RP2 needs "rtu_pins = (Pin(4), Pin(5))" whereas ESP32 can use any pin
# the following example is for an ESP32
rtu_pins = (25, 26) # (TX, RX)
slave_addr = 10 # address on bus as client
baudrate = 9600
client = ModbusRTU(
addr=slave_addr, # address on bus
baudrate=baudrate, # optional, default 9600
# data_bits=8, # optional, default 8
# stop_bits=1, # optional, default 1
# parity=None, # optional, default None
pins=rtu_pins)
pins=rtu_pins, # given as tuple (TX, RX)
# data_bits=8, # optional, default 8
# stop_bits=1, # optional, default 1
# parity=None, # optional, default None
# ctrl_pin=12, # optional, control DE/RE
# uart_id=1 # optional, see port specific documentation
)

# common slave register setup, to be used with the Master example above
register_definitions = {
"COILS": {
"RESET_REGISTER_DATA_COIL": {
"register": 42,
"len": 1,
"val": 0
},
"EXAMPLE_COIL": {
"register": 123,
"len": 1,
Expand Down
15 changes: 9 additions & 6 deletions umodbus/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@ class ModbusRTU(Modbus):
:param parity: The parity, default None
:type parity: Optional[int]
:param pins: The pins as list [TX, RX]
:type pins: List[int, int]
:type pins: List[Union[int, Pin], Union[int, Pin]]
:param ctrl_pin: The control pin
:type ctrl_pin: int
:param uart_id: The ID of the used UART
:type uart_id: int
"""
def __init__(self,
addr: int,
baudrate: int = 9600,
data_bits: int = 8,
stop_bits: int = 1,
parity: Optional[int] = None,
pins: List[int, int] = None,
ctrl_pin: int = None):
pins: List[Union[int, Pin], Union[int, Pin]] = None,
ctrl_pin: int = None,
uart_id: int = 1):
super().__init__(
# set itf to Serial object, addr_list to [addr]
Serial(uart_id=1,
Serial(uart_id=uart_id,
baudrate=baudrate,
data_bits=data_bits,
stop_bits=stop_bits,
Expand All @@ -73,7 +76,7 @@ def __init__(self,
data_bits: int = 8,
stop_bits: int = 1,
parity=None,
pins: List[int, int] = None,
pins: List[Union[int, Pin], Union[int, Pin]] = None,
ctrl_pin: int = None):
"""
Setup Serial/RTU Modbus
Expand All @@ -89,7 +92,7 @@ def __init__(self,
:param parity: The parity, default None
:type parity: Optional[int]
:param pins: The pins as list [TX, RX]
:type pins: List[int, int]
:type pins: List[Union[int, Pin], Union[int, Pin]]
:param ctrl_pin: The control pin
:type ctrl_pin: int
"""
Expand Down