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
uctypes how to struc
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: uctypes how to struc
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.
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.
Index to my micropython libraries.
Re: uctypes how to struc
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)
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)