Login | Register 

Parse TCP input stream from byte format to TableDAT

General discussion about anything TouchDesigner

Parse TCP input stream from byte format to TableDAT

Postby pdubost » Mon Dec 11, 2017 7:28 pm

I'm having issues to parse an incoming stream of keys and values in TCP format

this is the style of message I get
python >>>
b'\x02+\x00e\x12\x00Clock.DayTime.Time\x13\x0012/12/2017 00:10:38\x03'
b'\x02 \x00e\x12\x00Clock.DayTime.Long\x08\x0000:10:38\x03\x02\x1c\x00e\x14\x00Clock.DayTime.Second\x02\x0038\x03\x020\x00e\x17\x00Clock.DayTime.Universal\x13\x0012/12/2017 00:10:38\x03'

For instance, here I want to get:
Clock.DayTime.Long : 00:10:38
Clock.DayTime.Second : 38
Clock.DayTime.Universal : 12/12/2017 00:10:38

Tried some dumb string splitting at the beginning which quickly breaks with some messages..
So I try to parse it following the specifications but there s some irregularities from one message to another. I have the feeling it's due to some decoding issue with UTF-8 maybe.

I'm using a cursor to navigate through the string with the different length informations.
but sometimes the length encoded in the bytes gets very high and exceeds the parameter key and value.
Here are the specs:
SpecsTCP.png (38.92 KiB) Viewed 976 times

I was wondering if there was any recommendation for this kind of Parsing in Touch.
Are there some native functions that could help me?

thanks a lot,
Posts: 64
Joined: Tue Dec 27, 2011 6:30 pm

Re: Parse TCP input stream from byte format to TableDAT

Postby prochoy » Mon Jan 15, 2018 12:19 pm

Are you decoding the message first? The message style you posted is an encoded byte string so the first thing you would want to do is run it through a decode()

Code: Select all
encoded_string = b'\x02+\x00e\x12\x00Clock.DayTime.Time\x13\x0012/12/2017 00:10:38\x03'
encoded_string.decode('utf-8', 'ignore')

Also, the 'errors' argument on the decode method may be useful to you to handle any conversion issues...

Posts: 66
Joined: Wed Dec 31, 2014 2:20 pm

Re: Parse TCP input stream from byte format to TableDAT

Postby malcolm » Mon Jan 15, 2018 7:21 pm

The 'struct' Python module is also really helpful for parsing byte streams of data
User avatar
Posts: 4256
Joined: Tue Nov 13, 2007 1:11 am

Return to General TouchDesigner Discussion

Who is online

Users browsing this forum: No registered users and 6 guests