Parse TCP input stream from byte format to TableDAT

Hello
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:

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,
Phil

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()

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…

docs.python.org/3/howto/unicode.html

The ‘struct’ Python module is also really helpful for parsing byte streams of data

1 Like