diff --git a/rx/WenetPackets.py b/rx/WenetPackets.py index 83a569e..c8f2b54 100644 --- a/rx/WenetPackets.py +++ b/rx/WenetPackets.py @@ -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.'} diff --git a/rx/sec_payload_rx_example.py b/rx/sec_payload_rx_example.py index 3f3ca38..f661743 100644 --- a/rx/sec_payload_rx_example.py +++ b/rx/sec_payload_rx_example.py @@ -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. diff --git a/tx/examples/sec_payload_tx_example.py b/tx/examples/sec_payload_tx_example.py index 543b8f5..3f11ee7 100644 --- a/tx/examples/sec_payload_tx_example.py +++ b/tx/examples/sec_payload_tx_example.py @@ -44,9 +44,9 @@ def emit_secondary_packet(id=0, packet="", repeats = 1, hostname='', # 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)