use of select.poll() with multiple uarts

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
roland_vs
Posts: 88
Joined: Tue Dec 08, 2015 8:28 pm
Location: Netherlands
Contact:

use of select.poll() with multiple uarts

Post by roland_vs » Mon Apr 04, 2016 11:30 pm

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.

User avatar
dhylands
Posts: 3482
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: use of select.poll() with multiple uarts

Post by dhylands » Tue Apr 05, 2016 12:37 am

The documentation is here: http://docs.micropython.org/en/latest/p ... elect.html

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)
which gives the following output:

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...

User avatar
roland_vs
Posts: 88
Joined: Tue Dec 08, 2015 8:28 pm
Location: Netherlands
Contact:

Re: use of select.poll() with multiple uarts

Post by roland_vs » Wed Apr 06, 2016 7:16 am

@dhylands This works like a charm. I added some more UARTS and have data coming from all sides!

oceanq
Posts: 3
Joined: Fri Jan 18, 2019 3:21 am

Re: use of select.poll() with multiple uarts

Post by oceanq » Fri Jan 25, 2019 5:46 pm

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!

Post Reply