AnotherSam
Posts: 14
Joined: Thu Aug 12, 2021 2:37 pm

Using Python and pyserial to run python scripts on a pico (RP2040)

Tue Sep 21, 2021 5:57 pm

Hello All,
My ultimate goal is to control a number of peripheral chips on a PCB from a GUI interface on a PC. To do so, my plan was to incorporate a RP2040 (and memory) on the PCB in order to hold all the python scripts and to program/monitor all the peripheral chips. Then, using a PC to interface with the RP2040, send commands over the serial port to execute specific python files on the pico. I realize that is a bit confusing, so the attached block diagram should help.
BlockDiabram.png
BlockDiabram.png (30.2 KiB) Viewed 433 times
Starting on the left of the block diagram, I have a PC running a tkinter GUI. I am currently running the tkinter gui in Thonny. (eventually i would like it to be an executable, but that is beyond the scope of this post) The gui has a number of buttons to choose which python scripts to run. The PC is connected to the PCB through the USB cable. The USB data lines are routed to the RP2040's USB inputs (pins 47,48). The memory on the PCB holds a number of python scrips that correspond to the buttons in the GUI. Ideally, pressing a button on the PC would execute the corresponding py file on the pcb.
What I've got working so far: My real expertise lies in peripheral chips and PCB design, in this case the front end for a 2-18GHz transceiver, so bare with me if some of my python questions seem basic or misinformed. I've written and tested all the .py files on the pico's memory. To test those scripts I used Thonny to connect to my pico and simply ran (f5) the scripts with the peripherals connected to the right GPIOs. I was also able to get tkinter working and create functioning buttons that can execute commands. Using the pyserial module, I am also able to connected to the pico through the USB and write... strings. Not very useful, but a start.

Code: Select all

import serial
ser = serial.Serial('COM3', 38400, timeout=1, parity=serial.PARITY_EVEN, rtscts=1)
s = ser.read(100)       # read up to one hundred bytes or as much is in the buffer

print(ser.name)                # check which port was really used
ser.write(b'ToggleLED.py')     # write a string
ser.close()                    # close port
Remaining task: The final task I have been failing miserably at the past 2 days has been actually trying to execute the .py files located on the pico's memory through the serial port. My unexperienced/naïve notion was to simply send a string with the files name, obviously not correct. Any thoughts on how to execute those py files using that pyserial module?
Also, if there is a better solution, please feel free to share! Perhaps the files should be located on the PC and i send 1 command at time?

dbrion06
Posts: 690
Joined: Tue May 28, 2019 11:57 am

Re: Using Python and pyserial to run python scripts on a pico (RP2040)

Mon Sep 27, 2021 7:04 pm

You should go (if it not already done) on the pico dedicated forum.
If your PC is in gnuLinux, and if you can use rshell, I bet your tkinter python script can execute a system command.
When I want to start a script from my rpi to a nanopi, a quick and dirty way (IIRC: I will get a RPi this week end) is (IIRC)

man rshell
rshell repl "~ import Script2BExecutedAndResidentOnPicoPiFlash.py"

hippy
Posts: 10880
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Using Python and pyserial to run python scripts on a pico (RP2040)

Tue Sep 28, 2021 3:16 pm

AnotherSam wrote:
Tue Sep 21, 2021 5:57 pm
Also, if there is a better solution, please feel free to share! Perhaps the files should be located on the PC and i send 1 command at time?
The best solution would be to have a 'command interpreter' permanently running on the Pico, then use PySerial to send commands to it.

It all gets quite complicated running MicroPython programs from the host and interacting with them.

MicroPython 'as is' is far from well suited to program development of code which needs a serial link to the host for command or control. With only one serial interface it is a nightmare to debug code, find out what is wrong, see what is happening, an endless chore of switching between apps and resetting the device.

A better approach is to create your own MicroPython build which has two serial interfaces. One can be used to access the REPL so you can develop and debug your program using Thonny and similar, while the other can be used to receive commands and report data - viewtopic.php?t=305834

BTW serial over USB communications will require 'no parity'.

Return to “MicroPython”