Decoding Packets: GT06 & Teltonika Protocols



In Part 2, we built a listener that assembles incoming TCP streams into complete frames. Now it’s time to read what’s inside. Bytes like 0x78 0x78 0x0D 0x01 ... need to become useful information, such as IMEI 123456789012345, position -7.2575, 112.7521, heading 180°.

While writing this article, I was convinced that the GT06 datasheet’s login packet example was ... 6C 00 01 8C ... and that its CRC could use standard CRC-CCITT. Both were wrong, hahaha. I only realized it when my own code rejected the datasheet’s example. Lesson: with binary protocols, trust test vectors over your memory.

We’ll pick GT06 apart first, then look at Teltonika and where its design differs. All code below has been checked against official examples from the Concox GT06 v1.8.1 datasheet and the decoder structure of Traccar1, an open-source engine that has been used in production for years.

Trap #1: GT06’s “CRC-ITU” Isn’t the CRC-CCITT You Think It Is #

The GT06 datasheet just says that the CRC field contains a CRC-ITU value. The most common reference for CRC-ITU is CRC-16/CCITT using polynomial 0x1021. And here’s the trap: more than a dozen CRC variants share that polynomial, differing in initial value (init), processing direction (reflected), and final XOR mask (xor-out).

I brute-forced all common 0x1021 variants against the official datasheet sample, a login packet 78 78 0D 01 01 23 45 67 89 01 23 45 00 01 8C DD 0D 0A, whose CRC is listed as 0x8CDD:

Variant (poly 0x1021) Init Reflected XOR out Result
CCITT-FALSE 0xFFFF no 0x0000 0x81F6 ❌
XMODEM 0x0000 no 0x0000 0x55F9 ❌
X25 0xFFFF yes 0xFFFF 0x8CDD ✅

The answer is the X25 variant, processed reflected with a final XOR of 0xFFFF. The CRC scope doesn’t start at the protocol number either. It spans from the length byte up through the serial number (inclusive), matching the WD-209 datasheet wording: “from packet length to information serial number (including)”.

Here is the implementation in Go:

// crcITU = GT06's "CRC-ITU": CRC-16/X25 variant
// (poly 0x1021, init 0xFFFF, reflected, xor-out 0xFFFF).
// Scope: from packet LENGTH byte to SERIAL byte (inclusive).
func crcITU(data []byte) uint16 {
    crc := uint16(0xFFFF)
    for _, b := range data {
        crc ^= uint16(b)
        for i := 0; i < 8; i++ {
            if crc&1 != 0 {
                crc = (crc >> 1) ^ 0x8408 // reflected version of 0x1021
            } else {
                crc >>= 1
            }
        }
    }
    return crc ^ 0xFFFF
}

In production, Traccar (the most widely deployed open-source GPS tracking server) does not validate inbound CRCs for GT06. The market is full of GT06 clones and OEM variants with slightly different CRC calculations. Here we still validate the CRC and drop corrupt packets as the datasheet recommends. The catch: some cloned devices with quirky CRCs may get rejected too.

Frame Parser with CRC Validation #

Extending readFrame from Part 2, let’s unpack an intact frame:

type GT06Frame struct {
    Protocol byte
    Data     []byte   // payload content
    Serial   uint16   // used for ACK matching
}

func parseGT06(frame []byte) (*GT06Frame, error) {
    if len(frame) < 9 || frame[0] != 0x78 || frame[1] != 0x78 {
        return nil, fmt.Errorf("not a GT06 frame")
    }
    length := int(frame[2])
    body := frame[2 : 3+length] // length..crc: CRC calculation scope!

    got := binary.BigEndian.Uint16(body[len(body)-2:])
    want := crcITU(body[:len(body)-2])
    if got != want {
        return nil, fmt.Errorf("CRC mismatch: packet=0x%04X calculated=0x%04X", got, want)
    }

    return &GT06Frame{
        Protocol: body[1],
        Data:     body[2 : len(body)-4],
        Serial:   binary.BigEndian.Uint16(body[len(body)-4 : len(body)-2]),
    }, nil
}

The first test that must pass: the datasheet login example has to pass validation, and our recalculated CRC must match exactly: 0x8CDD.

Login: BCD IMEI and Mandatory ACK Responses #

The login packet (protocol 0x01) stores the IMEI in BCD format: 8 bytes storing 16 decimal digits, even though an IMEI is only 15 digits long. The first digit is simply a leading 0 padding that needs to be stripped:

// IMEI: 8 bytes BCD -> 16 hex digits -> drop first digit -> 15 digits
func decodeIMEI(data []byte) string {
    return fmt.Sprintf("%X", data)[1:]
}

Datasheet example: 01 23 45 67 89 01 23 45 -> "0123456789012345" -> IMEI 123456789012345.

Once the IMEI is decoded, we have to reply. As noted in Part 1: a device that isn’t acknowledged will keep retransmitting until it sulks. The login ACK packet format is 78 78 05 <protocol> <serial> <crc> 0D 0A:

// buildAck: 78 78 05 <protocol> <serial> <crc> 0D 0A
// CRC is computed from length byte (05) up to serial number.
func buildAck(f *GT06Frame) []byte {
    body := []byte{0x05, f.Protocol, byte(f.Serial >> 8), byte(f.Serial)}
    crc := crcITU(body)
    out := []byte{0x78, 0x78}
    out = append(out, body...)
    out = append(out, byte(crc>>8), byte(crc), 0x0D, 0x0A)
    return out
}

The test vector: for serial 0001, the correct response is 78 78 05 01 00 01 D9 DC 0D 0A, and our implementation above produces exactly that. (D9 DC comes from CRC X25 over 05 01 00 01, so the length byte is part of the CRC scope.)

Location: Converting Raw Bytes into Coordinates #

The most common position packet uses protocol 0x12. Here is the GPS payload breakdown:

Offset Size Field Notes
+0 6 Datetime BCD: YY MM DD HH MM SS
+6 1 Satellites low nibble only
+7 4 Latitude big-endian, unit in minutes × 30,000
+11 4 Longitude big-endian, unit in minutes × 30,000
+15 1 Speed km/h
+16 2 Course + flags see breakdown below

Two things that are easy to miss:

  1. Coordinates are not decimal degrees. The raw integer value must be divided by 60 × 30,000 = 1,800,000. For instance, Surabaya at -7.2575° is stored as 7.2575 × 60 × 30,000 = 13,063,500 -> 00 C7 55 4C.
  2. Hemisphere signs (south/west) are not stored in the numbers, all coordinate values are positive. The signs live in the flags bits within the course bytes.

Flag layout (2 bytes): bit 0-9 = heading (0-359°), bit 10 = latitude direction (1 = north/positive, 0 = south/negative), bit 11 = longitude direction (1 = west/negative), bit 12 = GPS fix valid. (I took these bit assignments from Traccar’s production-tested decoder, since datasheet wording varies between revisions. Once again: verify with real hardware.)

func decodeLocation(data []byte, tz *time.Location) Location {
    t := time.Date(
        bcd(data[0])+2000, time.Month(bcd(data[1])), bcd(data[2]),
        bcd(data[3]), bcd(data[4]), bcd(data[5]), 0, tz)

    sat := int(data[6] & 0x0F)
    lat := float64(binary.BigEndian.Uint32(data[7:11])) / 60.0 / 30000.0
    lng := float64(binary.BigEndian.Uint32(data[11:15])) / 60.0 / 30000.0
    speed := int(data[15])

    flags := binary.BigEndian.Uint16(data[16:18])
    course := int(flags & 0x3FF)
    if flags&(1<<10) == 0 { // bit 10: 0 = South
        lat = -lat
    }
    if flags&(1<<11) != 0 { // bit 11: 1 = West
        lng = -lng
    }
    valid := flags&(1<<12) != 0

    return Location{t, sat, lat, lng, speed, course, valid}
}

func bcd(b byte) int { return int(b>>4)*10 + int(b&0x0F) }

How to test the decoder without physical hardware: I constructed a synthetic 0x12 packet in the tests (Surabaya, 12 satellites, 42 km/h, heading 180°, valid fix) and decoded it back. The output was -7.2575, 112.7521, heading 180, valid fix. The full byte stream is 78 78 17 12 26 09 12 14 30 00 0C 00 C7 55 4C 0C 18 D4 34 2A 10 B4 00 01 00 A2 0D 0A, which you can replay directly over /dev/tcp as shown in Part 2.

Note: following the GPS block, a real 0x12 packet contains cellular base station (LBS) data for positioning fallback when GPS fix is lost, alongside terminal status info. We can parse that later if needed. For building core tracking, the GPS segment is all we need.

Teltonika: A Different Packet Design #

Teltonika covers the same needs with a different packet format and flow. The differences show up in its handshake, data batches, coordinates, and acknowledgements:

1. Handshake first, data later. A new connection begins with an identification packet: 2 bytes of length (00 0F) + 15 ASCII digits of IMEI + \r\n. The server responds with a single byte: 01 (IMEI accepted) or 00 (rejected):

func decodeIMEITeltonika(b []byte) (string, bool) {
    n := int(binary.BigEndian.Uint16(b[0:2]))
    if n != 15 || len(b) < 2+n {
        return "", false
    }
    imei := string(b[2 : 2+n])
    for _, c := range imei {
        if c < '0' || c > '9' {
            return "", false
        }
    }
    return imei, true // reply: 0x01 = accept, 0x00 = reject
}

2. Batches instead of one-by-one. A single AVL packet carries multiple records at once. Here is the Codec 8 structure:

00 00 00 00        preamble (4 zero bytes)
00 00 00 39        data length (4 bytes: codec ID up to num2)
08                 codec ID (0x08 = codec8)
01                 number of records
... AVL record ... (timestamp 8B, priority 1B, GPS element, IO elements)
01                 number of records (repeated, for validation)
00 00 0X XX        CRC-16 (4 bytes, 16-bit value in lower bytes)

3. Degrees × 10,000,000, straight. No minutes/30,000 gymnastics: longitude and latitude are signed 4-byte big-endian integers. Divide by 10,000,000 and you get plain decimal degrees. A single record’s GPS element contains: longitude (4B), latitude (4B), altitude (2B), angle (2B), satellites (1B), and speed (2B).

4. CRC-16 IBM (poly 0x8005, init 0, reflected: the CRC-16/ARC variant), calculated from the codec ID through number of data 2:

func crc16IBM(data []byte) uint16 {
    crc := uint16(0x0000)
    for _, b := range data {
        crc ^= uint16(b)
        for i := 0; i < 8; i++ {
            if crc&1 != 0 {
                crc = (crc >> 1) ^ 0xA001
            } else {
                crc >>= 1
            }
        }
    }
    return crc
}

To confirm receipt, the server replies with the count of records received as a 4-byte integer. For one record, it returns 00 00 00 01. The device retransmits if the count does not match, so per-packet serial numbers are not needed.

Test Results #

All code in this article passes 13 targeted assertions2:

Scenario Result
GT06 datasheet login packet (8C DD) passes CRC validation ✓
Recalculated CRC = 8CDD; serial 1 ACK = D9 DC ✓
BCD IMEI -> 123456789012345 (15 digits) ✓
Round-trip location 0x12: coordinates, course, speed, satellites, time ✓
Teltonika handshake with 356307042441013 ✓
AVL Codec 8: valid CRC, correct coordinates & speed ✓

Notes for Testing #

Next up is storing the decoded positions. If 500 trackers report every 10 seconds, that’s 4.3 million points per day. A plain PostgreSQL table will struggle without optimization, so Part 4 looks at TimescaleDB.

What’s Next #

This post is part of the Building a GPS Tracking Backend from Scratch series.

If you get different results when testing GT06 or Teltonika packets, let’s talk via Contact. Subscribe via RSS to catch the next article.


  1. Traccar’s decoder source code for GT06 and Teltonika is a gold standard for production-tested behavior. I used it to cross-verify field layouts and unit scaling factors. ↩︎

  2. The Concox GT06 v1.8.1 datasheet example (login and ACK) was used as an external test vector. Location packets and AVL records were validated via round-trip encoding and decoding with manually confirmed coordinates for Surabaya. ↩︎