uctypes how to struc

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
melmager
Posts: 3
Joined: Mon Sep 28, 2020 9:55 am

uctypes how to struc

Post by melmager » Wed Oct 07, 2020 11:13 am

first : i found 2 versions for define an struct
offset | uctype or uctype | offset
Which version is the right one ?

2: variable len of an array - how ?

background : i want to handel a Vertex Message - first come 4 Byte for len message then comes the data as (json/text)

vertex = { "size" : 0 | uctypes.UINT32, "json" : 4 | uctypes.ARRAY , ???? | uctypes.UINT8 )

or must the have the struc a fix length ? then i must go an other way :-)

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

Re: uctypes how to struc

Post by pythoncoder » Wed Oct 07, 2020 12:35 pm

I think you need to explain how this message is physically received. If it's a streaming device such as a UART or a socket you merely need to issue two stream reads, one to read 4 bytes and interpret the result as an integer N, then a second read of N bytes followed by a ujson decode.

Unless I'm misunderstanding I very much doubt that uctypes is the way to proceed, but without knowing the source of your data I can't be sure.
Peter Hinch
Index to my micropython libraries.

melmager
Posts: 3
Joined: Mon Sep 28, 2020 9:55 am

Re: uctypes how to struc

Post by melmager » Mon Oct 12, 2020 5:09 pm

read from a socket

ldat = connection.recv(4) # get len field
dlen = ustruct.decode("!i",ldat) #byte to int
bdat = connection.recv(dlen) #get bytes
tdat = bdat.decode('utf-8') # bytes to text
odat = json.loads(tdat) #json text to obj
back = obj.call(odat) #
tdat = ujson.dumps(back)
bdat = tdat.encode('utf-8')
dlen = len(bdat)
ldat = ustruct.pack("!i",dlen)
connection.sendall(ldat)
connection.sendall(bdat)

Post Reply