pyserial module/interface for embedded boards

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
BrendanSimon
Posts: 33
Joined: Wed Sep 07, 2016 10:46 am

pyserial module/interface for embedded boards

Post by BrendanSimon » Sat Sep 17, 2016 12:47 pm

I would like to use a number of modules that require a pyserial API. e.g. xbee in API mode, or pymodbus, etc. Unfortunately the pyb.UART module doesn't appear to be pyserial compatible.

I did find a micropython pyserial module (unix port). I had a look at the code it certainly seems very unix centric, so I presume this wont run on an actual embedded board. Is that correct? If it doesn't, then what is the point of that micropython pyserial port ?

Anyway, to be able to use other modules such as xbee or pymodbus, is it best to wrap the UART class in a pyserial compatible class, or is it possible to make the UART class actually provide a pyserial API too ?

Thanks for any tips/pointers regarding pyserial targeting an embedded board (mine is OLIMEX E407).

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

Re: pyserial module/interface for embedded boards

Post by dhylands » Sat Sep 17, 2016 5:12 pm

I went the other way. pyserial is for linux, and as such it is very rich and has lots and lots of features, most of which are never used.

For talking with dynamixels (AX-12 servos which use a serial bus), I needed to send packets between devices. I eventually settled on a fairly simple API. It requires a class which implements read_byte and write_packet (it looks like I may have changed my mind on is_byte_available and any and not all may be up to date).

I have an implementation of it that uses pyserial:
https://github.com/dhylands/bioloid3/bl ... al_port.py

one that uses STM hardware:
https://github.com/dhylands/bioloid3/bl ... rt_port.py
That one uses inline assembler because the AX-12's actually use a half-duplex protocol where you send and receive on the same wire, and I need to switch between sending and receiving very quickly.

one that uses the USB_VCP interface:
https://github.com/dhylands/bioloid3/bl ... sb_port.py

and one that uses a socket interface:
https://github.com/dhylands/bioloid3/bl ... et_port.py

I did all of my original testing/debugging on a host using the socket connections, and then moved everything down to the real hardware.

BrendanSimon
Posts: 33
Joined: Wed Sep 07, 2016 10:46 am

Re: pyserial module/interface for embedded boards

Post by BrendanSimon » Mon Sep 19, 2016 11:10 am

Interesting. I don't view pyserial as being for linux. I view it as a cross-platform module to access serial/com ports. Most, if not all, of the standard API seems OS agnostic to me.

https://pythonhosted.org/pyserial/pyserial_api.html

There is a platform specific section, which has 4 methods -- nonblocking(), fileno(), set_input_flow_control(enable), set_output_flow_control(enable).

I can understand why nonblocking() and fileno() would are Posix only, but the flow control methods seem (to me) to be generic serial functionality, that could/should be portable.

There are other parts of the module which may be overkill for most micropython applications. rfc2217.Serial, threaded.Protocol, but I imagine the aio module could be useful as I believe micropython supports asyncio and recommends it instead of threads.

I understand you build your own serial module, which has just what you need instead of the full pyserial functionality.

My goals are pyserial API compatibility, at least enough to work with other modules that use pyserial. I'm wondering whether it would be better to use the pyserial code base and port it somehow (is it too large to fit in a micropython embedded system ???), or whether wrapping the UART module in a pyserial compatible class.

I'm thinking the latter is the less painful route, at least for just providing the basic API.

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

Re: pyserial module/interface for embedded boards

Post by dhylands » Mon Sep 19, 2016 4:33 pm

The entire pyserial module is fairly large (almost 8000 lines of python).

So feel free to try porting it, but you may find that you don't have much space (if any) left over for your app.

If you port a subset, then you'll need to decide which subset to port, see how big it is and decide if that's acceptable to you or not.

It also really depends on which board your code is going to run on. One of my apps barely fits into an Espruino Pico, so it definitely wouldn't fit it I was using a pyserial emulation layer.

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

Re: pyserial module/interface for embedded boards

Post by pythoncoder » Mon Sep 19, 2016 6:31 pm

In the context of boards like your Olimex 8000 lines of Python is a very large program, quite possibly bigger than anything yet written for such boards. I'd figure out what subset of pyserial is used by, say, the Xbee driver and write a Python layer to map those calls onto the UART methods. An Xbee driver would be a great addition to user contributed code.

I rather think modbus has been mentioned before - a forum search might yield dividends. It's beyond the scope of most amateurs but could prompt more professional professional use of the Pyboard.
Peter Hinch
Index to my micropython libraries.

Post Reply