Modbus library only receiving data when run through Thonny #12039
Replies: 1 comment
-
When you run your code through Thonny, it might be initializing the UART port before executing your script, ensuring that the UART is ready for communication. However, when you save the code as main.py and run it directly, it might be executed too early, and the UART might not be properly initialized yet. There could be timing differences between running the code via Thonny and running it as main.py. When you run it via Thonny, it might introduce slight delays during the initialization phase, which might not happen when the code runs as main.py. To mitigate these issues, you can try adding some delays at the beginning of your script to give the UART port time to initialize before you attempt to communicate with the Modbus sensor. You can experiment with different delay values to find the optimal one for your specific setup. Here's an example of how you can add a delay using time.sleep(): |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there, I have run into an interesting issue.
I am using the Raspberry Pi Pico with Micropython "rp2-pico-20230426-v1.20.0"
I read data from a sensor that communicates with Modus. It does so without the need for an RS-485 converter since the sensor uses the Modbus protocol on a TTL level. (Modbus TTL). That means I simply connect the TX and RX of the sensor to the RX and TX UART pins of the Pico. (I am using pins 4 and 5). I also make sure they share a common ground. It's a pretty simple setup really.
I am using this Modbus library:
https://micropython-modbus.readthedocs.io/en/latest/readme_link.html
or on github:
https://github.com/brainelectronics/micropython-modbus
(all I need from the library is the "umodbus" folder which is copied to my Pico)
It's been working wonderfully and I usually run my code through Thonny.
However I want to run my code without my laptop connected, so I save it on the Pico as main.py. When I do that and I don't run it through Thonny, the code works and all but the Modbus library always returns ('no data received from slave').
Run it through Thonny and it always works, save it as main.py and I always get this exception.
(how do I know its working or not working without my laptop connected to it? I always also have a tft display connected to the Pico, showing the data)
I know it has nothing to do with how the pico is connected to power, because I have observed the following:
One time I connected the pico to the external power supply (on VSYS) and I also connected it through USB to thonny. When I hit Run it works. Then I disconnected it from my laptop and it continues to work. No connection to Thonny.
So it is something about starting my code through Thonny that is different than when I save it as main.py and then reboot the Pico.
Does that ring a bell to anyone?
I can comment the full code tomorrow, I was just thinking maybe this issue here is obvious to someone. Thank you so much in advance
(for the sake of completeness, I made one little change to the library so it won't return the registers in a tuple, but rather in a list. I wonder why it was implemented as a tuple since Modbus holding registers can change all the time)
The key part of my code is this:
Beta Was this translation helpful? Give feedback.
All reactions