RX python 3 fixes

picamera2
Mark Jessop 2022-03-19 14:38:27 +10:30
rodzic 3d29fe9d33
commit 96d081c66e
3 zmienionych plików z 15 dodań i 10 usunięć

Wyświetl plik

@ -556,7 +556,8 @@ def sec_payload_decode(packet):
packet = bytes(bytearray(packet))
message = {}
try:
message['id'] = struct.unpack("B",packet[1])[0]
#message['id'] = struct.unpack("B",packet[1:2])[0]
message['id'] = packet[1]
message['payload'] = packet[2:]
except:
return {'error': 'Could not decode secondary payload packet.'}

Wyświetl plik

@ -31,11 +31,12 @@ def process_sec_text_message(payload):
payload (str): The packet payload as a string, or a list.
"""
# Convert the payload into a string if it isn't already.
payload = str(bytearray(payload))
payload = bytes(bytearray(payload))
# Attempt to decode the text message from the payload.
try:
_len = struct.unpack("B",payload[1])[0]
#_len = struct.unpack("B",payload[1])[0]
_len = payload[1]
_count = struct.unpack(">H",payload[2:4])[0]
_text = payload[4:4+_len]
except:
@ -53,13 +54,14 @@ def process_sec_floats(payload):
payload (str): The packet payload as a string, or a list.
"""
# Convert the payload into a string if it isn't already.
payload = str(bytearray(payload))
payload = bytes(bytearray(payload))
output = []
try:
# Second byte in the packet is the number of floats present.
_len = struct.unpack("B", payload[1])[0]
#_len = struct.unpack("B", payload[1])[0]
_len = payload[1]
for _i in range(_len):
_float = struct.unpack(">f", payload[2+_i*4: 6+_i*4])[0]
@ -106,7 +108,7 @@ def process_udp(udp_packet):
""" Process received UDP packets. """
# Parse JSON
packet_dict = json.loads(udp_packet)
packet_dict = json.loads(udp_packet.decode())
# There may be other UDP traffic on this port, so we filter for just 'WENET'
# telemetry packets.
@ -114,6 +116,8 @@ def process_udp(udp_packet):
# Extract the actual packet contents from the JSON blob.
packet = packet_dict['packet']
print(packet)
# Check for a 'secondary payload' packet
if decode_packet_type(packet) == WENET_PACKET_TYPES.SEC_PAYLOAD_TELEMETRY:
# If we have one, extract the secondary payload ID, and the packet contents.

Wyświetl plik

@ -44,9 +44,9 @@ def emit_secondary_packet(id=0, packet="", repeats = 1, hostname='<broadcast>',
# Send to target hostname. If this fails just send to localhost.
try:
telemetry_socket.sendto(json.dumps(data), (hostname, port))
telemetry_socket.sendto(json.dumps(data).encode(), (hostname, port))
except socket.error:
telemetry_socket.sendto(json.dumps(data), ('127.0.0.1', port))
telemetry_socket.sendto(json.dumps(data).encode(), ('127.0.0.1', port))
telemetry_socket.close()
@ -77,7 +77,7 @@ def create_text_message(message):
_PACKET_COUNTER = text_message_counter
# Assemble the packet.
_packet = struct.pack(">BBH", _PACKET_TYPE, _PACKET_LEN, _PACKET_COUNTER) + message
_packet = struct.pack(">BBH", _PACKET_TYPE, _PACKET_LEN, _PACKET_COUNTER) + message.encode('ascii')
return _packet
@ -98,7 +98,7 @@ def create_arbitrary_float_packet(data=[0.0, 0.1]):
_PACKET_LEN = len(data)
# Convert the list of floats into a byte array representation.
_float_bytes = bytes("")
_float_bytes = b""
for _val in data:
_float_bytes += struct.pack(">f", _val)