I work with three serial ports and I would like to handle these within the program. So I use USB-VCP exclusively for debugging.
I have an uart 3 that communicates to a host, I have uart 1, uart 2 and uart 6 ready to go. So data may be coming from everywhere.
How does one use the select.poll() in such occasion to see which port to service?
Greetings,
R.
use of select.poll() with multiple uarts
Re: use of select.poll() with multiple uarts
The documentation is here: http://docs.micropython.org/en/latest/p ... elect.html
I was able to put together a simple example: which gives the following output:
I was able to put together a simple example:
Code: Select all
import pyb
import select
# assume UART4 Tx (X1) connected to UART6 Rx (Y2)
u4 = pyb.UART(4, 9600)
u6 = pyb.UART(6, 9600)
u4.write('123')
poll = select.poll()
poll.register(u6, select.POLLIN)
while True:
events = poll.poll()
print('events =', events)
for file in events:
# file is a tuple
if file[0] == u6:
ch = u6.read(1)
print('Got ', ch)
Code: Select all
>>> import poll_uart
events = [(UART(6, baudrate=9600, bits=8, parity=None, stop=1, timeout=1000, timeout_char=2, read_buf_len=64), 1)]
Got b'1'
events = [(UART(6, baudrate=9600, bits=8, parity=None, stop=1, timeout=1000, timeout_char=2, read_buf_len=64), 1)]
Got b'2'
events = [(UART(6, baudrate=9600, bits=8, parity=None, stop=1, timeout=1000, timeout_char=2, read_buf_len=64), 1)]
Got b'3'
... and it stops here waiting for more data on the uart...
Re: use of select.poll() with multiple uarts
@dhylands This works like a charm. I added some more UARTS and have data coming from all sides!
Re: use of select.poll() with multiple uarts
Would select.poll() be the most efficient way to deal with logging asci data that is on multiple uarts?
I have 3 UARTS, two of the UARTS are sending data at a 1 second interval (gps, and another sensor), the third UART has a data rate that varies between 0.75 and 1.25 seconds.
If I modified the simple example, and wrote a while loop for each of the UARTS, would it be fast enough to open the file, append-write the data, close the file and flush the buffer on each of the UARTS?
Is there a way to prioritize the UARTS, so the most important data does not get missed?
Could the UART data be concatenated into a single append-write?
Thank you!
I have 3 UARTS, two of the UARTS are sending data at a 1 second interval (gps, and another sensor), the third UART has a data rate that varies between 0.75 and 1.25 seconds.
If I modified the simple example, and wrote a while loop for each of the UARTS, would it be fast enough to open the file, append-write the data, close the file and flush the buffer on each of the UARTS?
Is there a way to prioritize the UARTS, so the most important data does not get missed?
Could the UART data be concatenated into a single append-write?
Thank you!