Concox V5 device disconnects after sending heartbeat response from Python TCP server

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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
</code>
<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 </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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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")
</code>
<code>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") </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật