USB comm between RP2040 and Python #15827
Unanswered
CodreanuML
asked this question in
RP2040 / Pico
Replies: 2 comments
-
These scripts are working fine for me, so please feel free to give them a try. Python: # Python3
import serial
def main(cnt):
s.write(f"#{cnt} TESTMESSAGE\n".encode())
s.flush()
mes = s.readline()
print(mes.decode(), end='')
#device="/dev/ttyUSB0"
device="/dev/ttyACM0"
s = serial.Serial(port=device, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
print(device,s)
cnt=1
while cnt<100:
main(cnt)
cnt+=1 MicroPython: # MicroPython
import select, sys, os
# Set up the poll object
poll_obj = select.poll()
poll_obj.register(sys.stdin, select.POLLIN)
dp = os.dupterm(None) # disassociate REPL from terminal in/out
# Loop indefinitely until CRTL+C
while True:
# Wait for input on stdin
poll_results = poll_obj.poll(100) # 100 microseconds timeout
if poll_results:
# Read the data from stdin (read data coming from PC), use readline()
data = sys.stdin.readline()[:-1] # strip '\n'
if data: # Write the data to the input file
sys.stdout.write("received data: " + data + '\n')
else:
continue
os.dupterm(dp) # restore REPL There are a few differences from your scripts. I use |
Beta Was this translation helpful? Give feedback.
0 replies
-
I'm not sure why it works sometimes and not others, but one thing I notice is that you use |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
i am using this script on my rp2040 :
import select
import sys
import time
Set up the poll object
poll_obj = select.poll()
poll_obj.register(sys.stdin, select.POLLIN)
Loop indefinitely
while True:
# Wait for input on stdin
poll_results = poll_obj.poll(1) # the '1' is how long it will wait for message before looping again (in microseconds)
if poll_results:
# Read the data from stdin (read data coming from PC)
data = sys.stdin.readline().strip()
# Write the data to the input file
sys.stdout.write("received data: " + data + "\r")
else:
# do something if no message received (like feed a watchdog timer)
continue
I use it to communicate with a python script on my computer:
def main():
s = serial.Serial(port="/dev/ttyACM0", parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, timeout=1)
s.flush()
I don't know why sometimes it works...sometimes not.
Strange is that the python script on my computer don't throw an error. The port is open everything is fine .But if what i seen is that when it won't work mycropython script don't enter inside IF statement .
Can anybody help to undestand why ?
Beta Was this translation helpful? Give feedback.
All reactions