Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How can i parse GT06N Protocol Data from HEX to Text

$
0
0

i have been working on receiving data from a GPS tracker using the GT06N Protocol and i have managed to successfully receive the packets but now my problem is extracting the data out from the packets, i have been following the GT06N protocol documentation but i am hitting dead ends, so what i need help with is to extract the data from the Packet, thats where am stuck.

Here is My Current Code

import socketimport binasciiimport structimport zlibimport osos.environ['TZ'] = 'Africa/Nairobi'# Function to calculate CRC-ITU (CRC-16-CCITT-FALSE) error checkdef crc_itu(data):    crc = 0xFFFF    polynomial = 0x1021    for byte in data:        crc ^= (byte << 8)        for _ in range(8):            if crc & 0x8000:                crc = ((crc << 1) ^ polynomial) & 0xFFFF            else:                crc = (crc << 1) & 0xFFFF    return crc.to_bytes(2, byteorder='big')# Set timezone to Africa/Nairobi# Create a TCP server socketserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((my_server_ip, my_server_port))server.listen(1)print("Waiting for connection...")# Accept a client connectionsocket, _ = server.accept()print("Connection established.")# Receive data from the clientencoded_data = socket.recv(1500)# Extracting the Information Serial Numberserial_number_hex = binascii.hexlify(encoded_data)[-10:-6]new_serial_number_hex = hex(int(serial_number_hex, 16) + 1)[2:].zfill(4)# Calculating the error check (CRC-ITU)error_check_data = binascii.hexlify(encoded_data)[4:-10]error_check = crc_itu(binascii.unhexlify(error_check_data))# Constructing the server's response packetserver_response_packet = b"78780501" + bytes.fromhex(new_serial_number_hex) + error_check + b"0D0A"TrackerData = binascii.hexlify(encoded_data).decode()print("Received: ", TrackerData)print("Server Reply: ", server_response_packet.hex().upper())# Send the server's response packet to the clientsocket.sendall(server_response_packet)def convert_to_decimal(hex_bytes):    return int.from_bytes(hex_bytes, byteorder='big')def parse_datetime(datetime_bytes):    year = convert_to_decimal(datetime_bytes[0:1]) + 2000    month = convert_to_decimal(datetime_bytes[1:2])    day = convert_to_decimal(datetime_bytes[2:3])    hour = convert_to_decimal(datetime_bytes[3:4])    minute = convert_to_decimal(datetime_bytes[4:5])    second = convert_to_decimal(datetime_bytes[5:6])    return f"{year}-{month}-{day} {hour}:{minute}:{second}"def parse_latitude(latitude_bytes):    latitude_decimal = convert_to_decimal(latitude_bytes)    latitude_decimal /= 30000 * 60    return latitude_decimaldef parse_longitude(longitude_bytes):    longitude_decimal = convert_to_decimal(longitude_bytes)    longitude_decimal /= 30000 * 60    return longitude_decimaldef parse_speed(speed_byte):    return convert_to_decimal(speed_byte)def parse_course(course_bytes):    return convert_to_decimal(course_bytes)packet = TrackerDatastart_bit = packet[0:4]packet_length = packet[4:6]protocol_no = packet[6:8]date_time = parse_datetime(bytes.fromhex(packet[8:20]))quantity_gps_info_satellites = packet[20:22]latitude = parse_latitude(bytes.fromhex(packet[22:30]))longitude = parse_longitude(bytes.fromhex(packet[30:38]))speed = parse_speed(bytes.fromhex(packet[38:40]))course = parse_course(bytes.fromhex(packet[40:44]))mcc = packet[44:48]mnc = packet[48:50]lac = packet[50:54]cell_id = packet[54:60]serial_no = packet[60:64]error_check = packet[64:68]stop_bit = packet[68:72]print("Start Bit:", start_bit)print("Packet Length:", packet_length)print("Protocol No:", protocol_no)print("Date Time:", date_time)print("Quantity of GPS Information Satellites:", quantity_gps_info_satellites)print("Latitude:", latitude)print("Longitude:", longitude)print("Speed:", speed, "km/h")print("Course:", course, "degrees")print("MCC:", mcc)print("MNC:", mnc)print("LAC:", lac)print("Cell ID:", cell_id)print("Serial No:", serial_no)print("Error Check:", error_check)print("Stop Bit:", stop_bit)# Close the connectionsocket.close()server.close()

Here is what i get

enter image description here

starting with the date its self its very wrong we are in 2024 its showing 2008

Here is a sample Packet

**78780d010865784052326215000516510d0a
**
according to the GT06N Documentation here is how to handle lat and long

Latitude:

Four bytes are consumed, defining the latitude value of location data. The range of the value is 0-162000000, indicating a range of 0°-90°. The conversion method thereof is as follow: Converting the value of latitude and longitude output by GPS module into a decimal based on minute; multiplying the converted decimal by 30000; and converting the multiplied result into hexadecimal Example: 22º32.7658‟=(22X60+32.7658)X30000=40582974, then converted into a hexadecimal number 40582974(Decimal)= 26B3F3E(Hexadecimal) at last the value is 0x02 0x6B 0x3F 0x3E

Longitude:

Four bytes are consumed, defining the longitude value of location data. The range of the value is 0-324000000, indicating a range of 0°-180°. The conversion method herein is same to the method mentioned in Latitude (see section 5.2.1.6).

Speed:

One byte is consumed, defining the running Speed of GPS. The value ranges from 0x00 to 0xFF indicating a range from 0 to 225km/h. e.g. 0x00 represents 0 km/h. 0x10 represents 16km/h. 0xFF represents 255km/h

Here is the Link to the documentation am following >> https://github.com/tobadia/petGPS/blob/master/resources/GT06_GPS_Tracker_Communication_Protocol_v1.8.1.pdf


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>