packing a uarray

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

packing a uarray

Post by ttmetro » Tue Mar 23, 2021 12:05 am

I'd like to efficiently (without memory allocation) send binary data over a uart.

This is what I came up with:

Code: Select all

from array import array
from struct import pack

a = array('i', range(4))

# this works - is there a way to get rid of the loop?
# i.e. have C do the looping ...
for i in a:
    print(pack('i', i))

# TypeError: can't convert array to int
print(pack('4i', a))
The for loop works fine, but the "more efficient (?)" 2nd option does not (neither in CPython). Is there a "clever" solution?
Bernhard Boser

ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

Re: packing a uarray

Post by ttmetro » Tue Mar 23, 2021 1:44 am

Answered the question myself:

Directly write the array to the UART (no pack). If both ends of the connection use the same binary encoding (endian), this works.
Bernhard Boser

Post Reply