I’m working on a Python TCP server to handle data from a Concox V5 GPS tracking device. The device successfully connects and sends location data and other messages, but it seems to disconnect after I send a heartbeat response.
I suspect the issue is with how I’m crafting the heartbeat response packet. Here’s the relevant portion of my code:
def create_heartbeat_response(status_info):
start_bit = b"x78x78"
protocol_number = b"x13" # Heartbeat response protocol
serial_number = status_info['serial_number'] # Use the serial number from status info
packet_length = b"x05" # Fixed length for response
# Calculate CRC for the packet
error_check = get_crc16(packet_length + protocol_number + serial_number)
stop_bit = b"x0Dx0A"
# Construct the full packet
return start_bit + packet_length + protocol_number + serial_number + error_check + stop_bit
And here is how I handle incoming data from the device:
def parse_concox_data(data, client_socket):
if data.startswith(b"x78x78"): # Check for start bits
protocol_number = data[3]
if protocol_number == 0x13: # Status information (Heartbeat)
logger.info("Heartbeat/Status message received")
status_info = parse_status_info(data)
return create_heartbeat_response(status_info)
return None
Observations:
It sends a heartbeat (status) message, and after I respond, the device disconnects.
The device sends the location data when i dont respond to the heartbeat message.
My Assumptions:
I suspect there might be something wrong with how the heartbeat response is being structured or how I’m calculating the CRC-16 checksum for the packet.
crctab16 = [
0x0000,
0x1189,
0x2312,
0x329B,....rest of table ]
# Function to calculate CRC-ITU (as per the protocol)
def get_crc16(data):
fcs = 0xFFFF # initialization
for byte in data:
fcs = (fcs >> 8) ^ crctab16[(fcs ^ byte) & 0xFF]
return (~fcs & 0xFFFF).to_bytes(2, "big")
The device might expect a different length or content for the heartbeat response.
Question:
What might be causing the Concox V5 device to disconnect after I send the heartbeat response? Is there something wrong with how I’m crafting the heartbeat response packet?
I tried constructing the heartbeat response packet based on the protocol documentation, including the start bit (0x78 0x78), protocol number (0x13 for heartbeat), and serial number from the status info, followed by a CRC-16 checksum and stop bit.
I expected the device to receive the heartbeat response, acknowledge it, and maintain the connection. However, after sending the heartbeat response, the device continues to send data for a short period, but then disconnects.
Heres is a link the to the specification im using : http://www.iconcox.in/images/tr02-protocol.pdf