Skip to content

Commit 276165a

Browse files
add RTU pin usage examples for ESP32, RP2 and pyboard to RTU client and host examples, print uPy and lib version, relates to #7
1 parent 3fca360 commit 276165a

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

examples/rtu_client_example.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,51 @@
3737
# check MicroPython UART documentation
3838
# https://docs.micropython.org/en/latest/library/machine.UART.html
3939
# for Device/Port specific setup
40+
#
4041
# RP2 needs "rtu_pins = (Pin(4), Pin(5))" whereas ESP32 can use any pin
41-
# the following example is for an ESP32
42+
# the following example is for an ESP32.
43+
# For further details check the latest MicroPython Modbus RTU documentation
44+
# example https://micropython-modbus.readthedocs.io/en/latest/EXAMPLES.html#rtu
4245
rtu_pins = (25, 26) # (TX, RX)
4346
slave_addr = 10 # address on bus as client
4447
baudrate = 9600
48+
uart_id = 1
49+
50+
try:
51+
from machine import Pin
52+
import os
53+
from umodbus import version
54+
55+
os_info = os.uname()
56+
print('MicroPython infos: {}'.format(os_info))
57+
print('Used micropthon-modbus version: {}'.format(version.__version__))
58+
59+
if 'pyb' in os_info:
60+
# NOT YET TESTED !
61+
# https://docs.micropython.org/en/latest/library/pyb.UART.html#pyb-uart
62+
# (TX, RX) = (X9, X10) = (PB6, PB7)
63+
uart_id = 1
64+
rtu_pins = (Pin(PB6), Pin(PB7)) # (TX, RX)
65+
elif 'esp8266' in os_info:
66+
# https://docs.micropython.org/en/latest/esp8266/quickref.html#uart-serial-bus
67+
raise Exception(
68+
'UART0 of ESP8266 is used by REPL, UART1 can only be used for TX'
69+
)
70+
elif 'esp32' in os_info:
71+
# https://docs.micropython.org/en/latest/esp32/quickref.html#uart-serial-bus
72+
uart_id = 1
73+
rtu_pins = (25, 26) # (TX, RX)
74+
elif 'rp2' in os_info:
75+
# https://docs.micropython.org/en/latest/rp2/quickref.html#uart-serial-bus
76+
uart_id = 0
77+
rtu_pins = (Pin(0), Pin(1)) # (TX, RX)
78+
except AttributeError:
79+
pass
80+
except Exception as e:
81+
raise e
82+
83+
print('Using pins {} with UART ID {}'.format(rtu_pins, uart_id))
84+
4585
client = ModbusRTU(
4686
addr=slave_addr, # address on bus
4787
pins=rtu_pins, # given as tuple (TX, RX)

examples/rtu_host_example.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,49 @@
4343
# check MicroPython UART documentation
4444
# https://docs.micropython.org/en/latest/library/machine.UART.html
4545
# for Device/Port specific setup
46+
#
4647
# RP2 needs "rtu_pins = (Pin(4), Pin(5))" whereas ESP32 can use any pin
4748
# the following example is for an ESP32
49+
# For further details check the latest MicroPython Modbus RTU documentation
50+
# example https://micropython-modbus.readthedocs.io/en/latest/EXAMPLES.html#rtu
4851
rtu_pins = (25, 26) # (TX, RX)
4952
baudrate = 9600
53+
uart_id = 1
54+
55+
try:
56+
from machine import Pin
57+
import os
58+
from umodbus import version
59+
60+
os_info = os.uname()
61+
print('MicroPython infos: {}'.format(os_info))
62+
print('Used micropthon-modbus version: {}'.format(version.__version__))
63+
64+
if 'pyb' in os_info:
65+
# NOT YET TESTED !
66+
# https://docs.micropython.org/en/latest/library/pyb.UART.html#pyb-uart
67+
# (TX, RX) = (X9, X10) = (PB6, PB7)
68+
uart_id = 1
69+
rtu_pins = (Pin(PB6), Pin(PB7)) # (TX, RX)
70+
elif 'esp8266' in os_info:
71+
# https://docs.micropython.org/en/latest/esp8266/quickref.html#uart-serial-bus
72+
raise Exception(
73+
'UART0 of ESP8266 is used by REPL, UART1 can only be used for TX'
74+
)
75+
elif 'esp32' in os_info:
76+
# https://docs.micropython.org/en/latest/esp32/quickref.html#uart-serial-bus
77+
uart_id = 1
78+
rtu_pins = (25, 26) # (TX, RX)
79+
elif 'rp2' in os_info:
80+
# https://docs.micropython.org/en/latest/rp2/quickref.html#uart-serial-bus
81+
uart_id = 0
82+
rtu_pins = (Pin(0), Pin(1)) # (TX, RX)
83+
except AttributeError:
84+
pass
85+
except Exception as e:
86+
raise e
87+
88+
print('Using pins {} with UART ID {}'.format(rtu_pins, uart_id))
5089

5190
host = ModbusRTUMaster(
5291
pins=rtu_pins, # given as tuple (TX, RX)
@@ -55,7 +94,7 @@
5594
# stop_bits=1, # optional, default 1
5695
# parity=None, # optional, default None
5796
# ctrl_pin=12, # optional, control DE/RE
58-
# uart_id=1 # optional, see port specific documentation
97+
uart_id=uart_id # optional, default 1, see port specific docs
5998
)
6099

61100
if IS_DOCKER_MICROPYTHON:
@@ -182,6 +221,8 @@
182221
print('Status of IST {}: {}'.format(ist_address, input_status))
183222
time.sleep(1)
184223

224+
print()
225+
185226
# READ IREGS
186227
ireg_address = register_definitions['IREGS']['EXAMPLE_IREG']['register']
187228
register_qty = register_definitions['IREGS']['EXAMPLE_IREG']['len']

examples/tcp_host_example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@
190190
print('Status of IST {}: {}'.format(ist_address, input_status))
191191
time.sleep(1)
192192

193+
print()
194+
193195
# READ IREGS
194196
ireg_address = register_definitions['IREGS']['EXAMPLE_IREG']['register']
195197
register_qty = register_definitions['IREGS']['EXAMPLE_IREG']['len']

0 commit comments

Comments
 (0)