Page 1 of 1

uctypes how to struc

Posted: Wed Oct 07, 2020 11:13 am
by melmager
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 :-)

Re: uctypes how to struc

Posted: Wed Oct 07, 2020 12:35 pm
by pythoncoder
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.

Re: uctypes how to struc

Posted: Mon Oct 12, 2020 5:09 pm
by melmager
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)