# Flymaster GPS Communication Protocol

Based on the code analysis, the Flymaster GPS device uses a combination of NMEA-style commands and a binary protocol for data transfer. Here's a detailed explanation of the protocol:

## 1. Basic Communication Parameters
- **Baud Rate**: 57600 bps (defined as `BAUDRATE`)
- **Flow Control**: Uses XON (0x11) and XOFF (0x13) characters for flow control
- **Line Termination**: Commands end with CR+LF (`\r\n`)

## 2. NMEA Command Structure
The Flymaster uses a standard NMEA-style command format for basic commands:

```
$<command>,<param1>,<param2>,...*<checksum>\r\n
```

Where:
- Commands start with `$`
- Fields are separated by commas
- The command ends with `*` followed by a two-digit hexadecimal checksum
- The checksum is calculated by XORing all characters between `$` and `*` (exclusive)
- The message ends with CR+LF

## 3. Command Prefixes
- `PFM` prefix is used for Flymaster-specific commands
- Examples of commands:
  - `PFMSNP`: Used to get device information
  - `PFMDNL`: Used to download data (with parameters like "LST" for track list)
  - `PFMLST`: Used to receive track list data

## 4. Binary Protocol for Track Download
For downloading track data, a binary packet-based protocol is used:

1. **Packet Structure**:
   - 2-byte packet ID
   - 1-byte length
   - Variable-length data (as specified by the length byte)
   - 1-byte checksum (XOR of the length and all data bytes)

2. **Packet Types** (identified by packet ID):
   - `0xa0a0`: Flight information
   - `0xa1a1`: Key position data (base position)
   - `0xa2a2`: Delta position data (incremental changes from base position)
   - `0xa3a3`: End of transmission marker

3. **Acknowledgment**:
   - `0xb1`: Positive acknowledgment (continue sending)
   - `0xb3`: Negative acknowledgment (error or cancel)

## 5. Data Structures
The protocol uses specific data structures:
- `FM_Flight_Info`: Contains flight information
- `FM_Key_Position`: Contains base position data (latitude, longitude, altitude, etc.)
- `FM_Point_Delta`: Contains delta values for position updates

## 6. Communication Flow

### Initialization:
1. Set baud rate to 57600
2. Send `PFMSNP` to get device information
3. Calculate device ID from the response

### Track List Download:
1. Send `PFMDNL,LST` command
2. Receive track list data with `PFMLST` responses
3. Parse track information (date, start time, duration)

### Track Data Download:
1. Send `PFMDNL,<timestamp>` with the timestamp of the desired track
2. Read binary packets:
   - Process flight info packets (0xa0a0)
   - Process key position packets (0xa1a1)
   - Process delta position packets (0xa2a2)
   - Send acknowledgment (0xb1) after each packet
   - Continue until end marker (0xa3a3) is received

## 7. Error Handling
- Checksum verification for both NMEA commands and binary packets
- Timeout handling (1 second timeout during download)
- Size verification for data structures
- Error reporting through exceptions

This protocol efficiently combines text-based commands for control operations with a compact binary format for transferring large amounts of track data.