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

Serial problem on RPi Pico W #43

Closed
animalmutch opened this issue Dec 29, 2022 · 7 comments
Closed

Serial problem on RPi Pico W #43

animalmutch opened this issue Dec 29, 2022 · 7 comments
Labels
client Client implementation specific issue documentation Improvements or additions to documentation host Host implementation specific issue question Further information is requested
Milestone

Comments

@animalmutch
Copy link

animalmutch commented Dec 29, 2022

Issue:

When creating a modbus master on RPi Pico W, you get the following error:

>>> m = ModbusRTU(addr=1, pins=(0,1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "umodbus/serial.py", line 58, in __init__
  File "umodbus/serial.py", line 104, in __init__
ValueError: expecting a Pin```

If you create Pin objects and use these instead, you get the following error:

>>> tx_pin = Pin(0)
>>> rx_pin = Pin(1)
>>> m = ModbusRTU(addr=1, pins=(tx_pin, rx_pin))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "umodbus/serial.py", line 58, in __init__
  File "umodbus/serial.py", line 104, in __init__
ValueError: bad TX pin
@beyonlo
Copy link

beyonlo commented Dec 29, 2022

Hello @animalmutch

  1. What version of this lib are you using?
  2. Could you please paste the complete code, how are you importing the modules?
  3. MicroPython version that you are using is as well a good information.

In the new versions works fine to me:

>>> from umodbus.serial import Serial as ModbusRTUMaster
>>> rtu_pins = (17, 18)
>>> host = ModbusRTUMaster(baudrate=115200, data_bits=8, stop_bits=1, parity=None, pins=rtu_pins, ctrl_pin=15)
>>>

@animalmutch
Copy link
Author

Thanks for your swift reply @beyonlo .

I am using version 2.1.1 of the lib (seems to be the latest release). I am not installing using upip, because upip doesn't seem to come bundled with micropython any more (they use mip instead now).

I am using the latest UF2 from raspberrypi.com (https://micropython.org/download/rp2-pico-w/rp2-pico-w-latest.uf2):

MicroPython v1.19.1-782-g699477d12 on 2022-12-20; Raspberry Pi Pico W with RP2040

Using the exact same code as you above, I get the following:

image

Seems most likely there's an issue with using the lib without installation. Can you advise any further on installation on RPi pico?

@brainelectronics brainelectronics added documentation Improvements or additions to documentation question Further information is requested client Client implementation specific issue host Host implementation specific issue labels Dec 29, 2022
@brainelectronics
Copy link
Owner

Relates to #17

@brainelectronics
Copy link
Owner

@animalmutch according to the MicroPython v1.19.1 RP2 UART docs the RX and TX pins shall be specified as Pin objects, which is different than in the e.g. ESP32 UART setup where A) any pin can be used, and B) a pin as integer shall be used.

The following example should work for you on a RP2

>>> from umodbus.serial import Serial as ModbusRTUMaster
>>> rtu_pins = (Pin(4), Pin(5))   # TX, RX
>>> host = ModbusRTUMaster(baudrate=115200, data_bits=8, stop_bits=1, parity=None, pins=rtu_pins, ctrl_pin=15)
>>>

Additionally the UART pins on RP2 can only be GPIO 4/5 or 8/9 for UART1 according to the docs

There are two UARTs, UART0 and UART1. UART0 can be mapped to GPIO 0/1, 12/13 and 16/17, and UART1 to GPIO 4/5 and 8/9.

which explains your

>>> tx_pin = Pin(0)
>>> rx_pin = Pin(1)
>>> m = ModbusRTU(addr=1, pins=(tx_pin, rx_pin))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "umodbus/serial.py", line 58, in __init__
  File "umodbus/serial.py", line 104, in __init__
ValueError: bad TX pin

But there is also a not yet implemented feature in this lib (never call it bug 🤦 ), see

Serial(uart_id=1,
where the uart_id is always set to 1 without the possibility to change it on ModbusRTU class level. So I assume the UART0 can not be used with pins GPIO 0/1, 12/13 and 16/17

@animalmutch
Copy link
Author

Thanks so much @brainelectronics ! I get it now, and it's working. It was just a mismatch between the pins I was using and the UART ID.

For reference, the following works perfectly:

modbus_host = ModbusRTUMaster(
            uart_id=0,
            baudrate=115200,
            pins=(Pin(0), Pin(1))
        )

For some reason Pin(4) and Pin(5) with uart_id=1 doesn't work (well, it doesn't cause a problem with your lib, but it physically doesn't work still, so perhaps a hardware issue).

Anyway, thanks again!

@brainelectronics
Copy link
Owner

Resolved by #45

@papercodeIN
Copy link

Thanks so much @brainelectronics ! I get it now, and it's working. It was just a mismatch between the pins I was using and the UART ID.

For reference, the following works perfectly:

modbus_host = ModbusRTUMaster(
            uart_id=0,
            baudrate=115200,
            pins=(Pin(0), Pin(1))
        )

For some reason Pin(4) and Pin(5) with uart_id=1 doesn't work (well, it doesn't cause a problem with your lib, but it physically doesn't work still, so perhaps a hardware issue).

Anyway, thanks again!

Hi Friend, Could you please share connection diagram ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Client implementation specific issue documentation Improvements or additions to documentation host Host implementation specific issue question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants