|
43 | 43 | # check MicroPython UART documentation |
44 | 44 | # https://docs.micropython.org/en/latest/library/machine.UART.html |
45 | 45 | # for Device/Port specific setup |
| 46 | +# |
46 | 47 | # RP2 needs "rtu_pins = (Pin(4), Pin(5))" whereas ESP32 can use any pin |
47 | 48 | # 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 |
48 | 51 | rtu_pins = (25, 26) # (TX, RX) |
49 | 52 | 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)) |
50 | 89 |
|
51 | 90 | host = ModbusRTUMaster( |
52 | 91 | pins=rtu_pins, # given as tuple (TX, RX) |
|
55 | 94 | # stop_bits=1, # optional, default 1 |
56 | 95 | # parity=None, # optional, default None |
57 | 96 | # 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 |
59 | 98 | ) |
60 | 99 |
|
61 | 100 | if IS_DOCKER_MICROPYTHON: |
|
182 | 221 | print('Status of IST {}: {}'.format(ist_address, input_status)) |
183 | 222 | time.sleep(1) |
184 | 223 |
|
| 224 | +print() |
| 225 | + |
185 | 226 | # READ IREGS |
186 | 227 | ireg_address = register_definitions['IREGS']['EXAMPLE_IREG']['register'] |
187 | 228 | register_qty = register_definitions['IREGS']['EXAMPLE_IREG']['len'] |
|
0 commit comments