How can I get a value like this "0x5000" from a PoolByteArray?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gamemap

I want to decode the Art-Net protocol (Page 25) but have a problem with the OpCodes.
They should be like “0x5000” but I don’t know how I can get such a data from a PoolByteArray [65, 114, 116, 45, 78, 101, 116, 0, 0, 33, 192, 168, 0, 111, 54, 25, 0, ...] the first 8 values are in the ascii format and writes “Art-Net” with an null character (0x00 (but not shown)). I think the next two characters (0,33) have to build the number (or one from the other values on page 25-26).

If I forgot any details please write a comment.
I hope someone can help me.

:bust_in_silhouette: Reply From: Foreman21

Hi Gamemap!

If you want to debug your Artnet trame I recomand you to use Wireshark.

If you want to get a from an poolbytearray do like for an array

var OPCodes = packet[9] # In reality it is two bytes, number 8 (0x00) & 9( 0x50)
print("OPCodes: %x" % OPCodes) # Use "%x" % to print ax hexadecimal
# OPCodes: 50

If you want to get all the 512 dmx values, use:

DMX = packet.subarray(18,529) # Packet.size() = 530 bytes

Here is an complete example. I don’t check Artnet version or univers. It is not really complex to do.

extends Node
const ARTNET_PORT = 6454
var artnetRX: PacketPeerUDP = PacketPeerUDP.new()
var DMX = []

# Set the PacketPeerUDP to listener the ARTNET_PORT (6454)
func _ready():
	print("start")
	if (artnetRX.listen(ARTNET_PORT) != OK):
		print("Error listening to port: ", ARTNET_PORT)
	else:
		print("Listening to port: " , ARTNET_PORT)
		
# Check at each frame if received a udp ARTNET packet at ARTNET_PORT
# You can set physics process at 40 Hz or use _process()
func _physics_process(delta):
	if artnetRX.get_available_packet_count() > 0:
		var packet = artnetRX.get_packet()
		print("Receive Artnet trame, len: ", packet.size()) 
		# Should be 530, 18 bytes of Artnet identifier and 512 bytes of values.
		var OPCodes = packet[9] # In reality it is two bytes, 
		#number 8 & 9
		print("OPCodes: %x" % OPCodes) # Use "%x" % to print ax hexadecimal
		DMX = packet.subarray(18,529) # See PoolByteArray class
		print("CH1 = ", DMX[0],", CH2 = ", DMX[1],", CH7 = ", DMX[6],", CH8 = ", DMX[7]) 
		# Here to get your values as an array.

This example will print:

Receive Artnet trame, len: 530
OPCodes: 50
CH1 = 255, CH2 = 255, CH7 = 255, CH8 = 0

Regards, Foreman21.