Converting Bytes to integers for an AR-Project

Hey Guys,

I just started working on an Augmented-Reality project. For this project I have a PTZ-Camera and I can send inquiries for Pan, Tilt, Zoom and so on. The problem I have is that the answer comes packaged in 4 bytes (I am just looking at the Tilt Values right now) and I cannot find a way to reconstruct the actual data.

The Data comes in the following format in an TcpIp-Dat:

90 50 0w 0w 0w 0w 0z 0z 0z 0z FF

(from the documentation found here, search for “CAM_PanTiltPosInq“:
https://ptzoptics.com/wp-content/uploads/2020/08/PTZOptics-VISCA-over-IP-Rev-1_2-8-20.pdf )

In this package, the wwww represents the Pan-Value and the zzzz the Tilt-Value. In Wireshark it looks like this:

90 50 0f 0e 04 0e 00 05 01 00 ff

In the TcpIp-Dat with bytes enabled it looks like this:

80 15 14 04 14 00 05 01 00 255

I have found that if I take the zzzz (Tilt-Values) from Wireshark in this example and run them through a Hexadecimal to Decimal converter, it gives me a number I can work with. In this example “0510” gives me “1296”. How I decipher this number to some kind of degrees is another question for another day, but I think it divides 360° of theoretical movement in 256²=65536 Steps and that’s logic I think I can handle.

I know that if I take the Tilt-Values (the bytes) in TouchDesigner “00 05 01 00” and converted them to normal integers I could use math (first number times 16³, second number times 16² and so on) to get that same number. But I have not found a way to convert the bytes to integers.

My question is, how I can get this conversion from bytes to integers done in Python or other Dats. Any help would be much appreciated! I will append my file, so that you could look at what I have done so far!

Thanks

ToelzerKing

PTZ_Converter1 Forum.2.toe (4.4 KB)

Look into the ‘struct’ module (it comes with default Python). It allows to to define how an array of raw bytes should be re-interpreted as different types of values (ints, floats etc)

1 Like

@malcolm Thank you for your swift answer, I tried:

TiltAngle = struct.unpack(‘=i’, bytes)

and now I get a “struct.error: unpack requires a buffer of 4 bytes”. I have looked thorugh many discussions in different Forums, but haven’t found a clue of what I have to do.

My knowledge in Python is not that great, so any help is, once again, much appreciated.

Thanks
ToelzerKing

One tip, in your onReceive callback you should use the ‘bytes’ argument directly, instead of reading a single cell decimal string from the table. The table is mostly a visual aid in this context.

You can pass that bytes argument to your struct pack/unpack calls.

Cheers,
Rob

1 Like

@rob Thanks, I implemented that immediately, still getting that “struct.error: unpack requieres a buffer of bytes” though… I’ll continue my search tomorrow.

Thank you for your help! If anyone knows a solution for the problem above, I’m still open for suggestions!

Cheers,
ToelzerKing

Hello.
Change your tcp/ip DAT row/col format to ‘One for All Received Data’.

Then change your callback to something like:

import struct

def onReceive(dat, rowIndex, message, bytes, peer):
    	print('R:', len(bytes), type(bytes), bytes)	
    	unpacked_data = struct.unpack('cccHHH', bytes)
    	print('data is', unpacked_data)

The print statements are optional, but tell you how many bytes are in the message, which should guide what you try to unpack from it.

Note: You’ll have to change that format string ‘cccHHH’ to match your actual format.
In this example, I just guessed a 9 byte message consisting of three single characters (c) followed by three two-byte unsigned short integers (H).

If different types of messages are coming in, you’ll have to unpack accordingly.

For a full format list see:

Hope that helps.
Cheers,
Rob