multi uart commuication via asyncio

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
skylin008
Posts: 59
Joined: Wed Mar 11, 2015 6:21 am

multi uart commuication via asyncio

Post by skylin008 » Sun Mar 08, 2020 1:15 pm

Hi, everyone! I had 2 openmv boards and 1 pyboard.Now I want to pyboard read the image information from 2 openmv boards simultaneously, when it read two images finished , then donging some algorithm. Now it any issue: 1. How to know the Image transmited finish? 2.How to keep guarantee the image information is right in transmited, if any protocol to be suggest ? Thanks very much!

User avatar
pythoncoder
Posts: 4030
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: multi uart commuication via asyncio

Post by pythoncoder » Mon Mar 09, 2020 8:15 am

Concurrent reading and writing to two uarts is easy using the stream mechanism but you'll have to develop your own protocol. If images are of fixed size this should help. I think you'll need to run code on the OpenMV boards to determine when an updated image needs to be sent to the Pyboard.

Your protocol may need to account for the case where OpenMV boards want to send data more frequently than the Pyboard can process it. I think you'll need some kind of "ready for data" message from the Pyboard to the OpenMV. It's worth spending some time thinking through these issues before you start coding. Also consider whether there are cases where the three boards are not powered up simultaneously, or if an OpenMV might be powered down while the Pyboard is running. This has implications for your protocol.
Peter Hinch

skylin008
Posts: 59
Joined: Wed Mar 11, 2015 6:21 am

Re: multi uart commuication via asyncio

Post by skylin008 » Tue Mar 10, 2020 2:34 am

Hi, Pythoncoder! Thank you for your kindly reply. I have any issue: Whether I can put the image infomation to the queue and periodically reading from the queue to the uart. One Image frame is 640 bytes. Thanks!

ltmerlin
Posts: 20
Joined: Fri Jun 28, 2019 12:34 pm

Re: multi uart commuication via asyncio

Post by ltmerlin » Sat Mar 28, 2020 10:24 am

pythoncoder wrote:
Mon Mar 09, 2020 8:15 am
Concurrent reading and writing to two uarts is easy using the stream mechanism but you'll have to develop your own protocol. If images are of fixed size this should help. I think you'll need to run code on the OpenMV boards to determine when an updated image needs to be sent to the Pyboard.

Your protocol may need to account for the case where OpenMV boards want to send data more frequently than the Pyboard can process it. I think you'll need some kind of "ready for data" message from the Pyboard to the OpenMV. It's worth spending some time thinking through these issues before you start coding. Also consider whether there are cases where the three boards are not powered up simultaneously, or if an OpenMV might be powered down while the Pyboard is running. This has implications for your protocol.
Hi Pythoncoder!
Thank you for your great uasyncio tutorial and extra modules at https://github.com/peterhinch/micropython-async. I was wondering what kind of adaptations we need to do to get our code (using uasyncio v2 as in your tutorials used) compatible with the new frozen asyncio version (i.e. v3).

I have noticed that there is no StreamReader or StreamWriter in the new version so this will break... And I also use the uasyncio.queues library, do you think we can still use this in combination with the new uasyncio.

I am just wondering if I have to switch to the new version or keep developing using v2 together with your nicely documented extra's...

User avatar
tve
Posts: 104
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: multi uart commuication via asyncio

Post by tve » Sat Mar 28, 2020 4:25 pm

ltmerlin wrote:
Sat Mar 28, 2020 10:24 am
I have noticed that there is no StreamReader or StreamWriter in the new version so this will break...

I am just wondering if I have to switch to the new version or keep developing using v2 together with your nicely documented extra's...
The new Stream class returned by open_connection and start_server is both a reader and writer, so should be compatible other than the name change.

I believe you should definitely look into switching 'cause pretty soon no-one will want to maintain the old one or stuff around it (hmm, Peter still "supports" the lobo port in mqtt_as, so maybe that's not true). However, it may make sense to delay a few weeks until some of the missing pieces have been filled in.

ltmerlin
Posts: 20
Joined: Fri Jun 28, 2019 12:34 pm

Re: multi uart commuication via asyncio

Post by ltmerlin » Sat Mar 28, 2020 4:34 pm

Thank you for your answer tve!
I'm using the classes StreamReader(uart, {}) and StreamWriter(uart) as seen in Peter Hinch's tutorial code: https://github.com/peterhinch/micropyth ... r/auart.py. Any idea how this would translate into the new Asyncio version?

User avatar
pythoncoder
Posts: 4030
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: multi uart commuication via asyncio

Post by pythoncoder » Sat Mar 28, 2020 7:03 pm

My uart example works under the new uasyncio. StreamReader and StreamWriter are part of CPython's asyncio and are supported.

New uasyncio has some drawbacks, notably it doesn't support fast I/O. Also its raw scheduling performance is slower than the old version. So there are still use cases for my fast_io variant, notably if you run multiple UARTS at high baudrates.

I very much hope that the new version is improved to allow high speed I/O so I can abandon fast_io. Failing that, I will attempt to find a solution and raise a PR.

I am planning a major revamp of my async repo to accommodate the new version, but it will take a few weeks to get this done. Most of the tutorial examples should still work as they are designed to be compatible with CPython.
Peter Hinch

ltmerlin
Posts: 20
Joined: Fri Jun 28, 2019 12:34 pm

Re: multi uart commuication via asyncio

Post by ltmerlin » Sat Mar 28, 2020 7:27 pm

Thank you Peter for your reply. I will stick with your fast_io variant for now. I had hoped that the new asyncio version had solved the slow I/O.
I am planning a major revamp of my async repo to accommodate the new version, but it will take a few weeks to get this done. Most of the tutorial examples should still work as they are designed to be compatible with CPython.
Thank you again for this, besides yours, there are no great tutorials/docs for async micropython out there. This is a gem for me and other beginners that otherwise would be trapped or lured in the world of threads, where in some cases cooperative is the better solution than pre-emptive...

User avatar
pythoncoder
Posts: 4030
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: multi uart commuication via asyncio

Post by pythoncoder » Sun Mar 29, 2020 12:21 pm

To be fair to new uasyncio, the I/O is twice as fast as the old version. Say you have 10 tasks, each of which hogs the Python VM for 5ms between yields. The old version would schedule I/O every 2*10*5 = 100ms. The new version will schedule it every 10*5 = 50ms, i.e. in round-robin style with other tasks. The fast_io version, if you use an I/O queue, will schedule it every 5ms.

Of course testing I/O readiness on every pass of the scheduler carries an overhead, but if it stops buffers overflowing with data loss it's a win.
Peter Hinch

Post Reply