Writing to UART without memory allocation?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Writing to UART without memory allocation?

Post by cefn » Mon Dec 04, 2017 9:29 am

I am keen to be able to write bytes from e.g. a Websocket or a calculation or procedure to UART without having to wrap the bytes in a new sequence and hence do slow memory allocation/garbage collection in the tightest central loop of my program.

The documentation for UART#write() at https://docs.micropython.org/en/latest/ ... UART.write is a bit ambiguous as to the types it will accept. I am not sure how I can investigate this further without just throwing things at UART.write and seeing what breaks, which seems a shame. I imagine it is strictly defined somewhere but I don't know where. Would be really useful to document a bit more precisely.

The write() documentation says it accepts a 'buffer of bytes'. Since 'buffer' isn't a python type, this might mean a sequence of the 'bytes' type, or it could allow for other things which generate at least one byte (byte isn't a python type either, so potentially anything generating numbers less than 256). A buffer of bytes could be even interpreted as [bytes(), bytes(), bytes()] !

However, while Uart#write() definitely accepts a single instance of what the python documentation calls 'bytes objects' I can't find anything else it accepts.

Unfortunately there is no UART#write() which accepts a single byte, so this signature seems to require that I marshall any data to be sent over UART by creating a bytes which is then garbage collected (since they do not support item assignment). Seems surprising given this is often right at the heart of a program and executed repeatedly. Did I miss something?

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Writing to UART without memory allocation?

Post by pfalcon » Mon Dec 04, 2017 1:27 pm

cefn wrote:
Mon Dec 04, 2017 9:29 am
The write() documentation says it accepts a 'buffer of bytes'. Since 'buffer' isn't a python type, this
Never too late to learn the language you use: https://docs.python.org/3/c-api/buffer. ... ght=buffer
Unfortunately there is no UART#write() which accepts a single byte, so this signature seems to require that I marshall any data to be sent over UART by creating a bytes which is then garbage collected (since they do not support item assignment). Seems surprising given this is often right at the heart of a program and executed repeatedly. Did I miss something?
https://github.com/micropython/micropython/pull/3382
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: Writing to UART without memory allocation?

Post by cefn » Mon Dec 04, 2017 4:55 pm

Nice, thankyou for all the useful pointers and sorry for wasting your time educating me for something I should have already found - the Buffer Protocol! It's there in an error message which I should have investigated from...

uart.write([23,45,65])

Too obsessed with exhaustively looking through lists of types so I omitted to search properly. I mistakenly thought I had eliminated bytearray() already by experimentation - but my failed experimentation must have been with lists of integers.

Adopting something with buffer protocol (such as bytearray) is the natural way to move forward without memory allocation.

Looking forward to your efforts on readbin() and writebin() bearing fruit within future stable releases. For now I'm targeting 1.9.3 to be sure I don't open any additional cans of worms from less tested images :)

Post Reply