%%{init: {'theme': 'base', 'themeVariables': {'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#16A085', 'secondaryColor': '#E67E22', 'tertiaryColor': '#7F8C8D'}}}%%
sequenceDiagram
participant TX as Transmitter
participant CH as Noisy Channel
participant RX as Receiver
Note over TX: Original Data: [0x45, 0x3F, 0x12]
TX->>TX: Calculate CRC-32
Note over TX: CRC = 0x7F82A3E9
TX->>CH: Send [Data + CRC]<br/>[0x45, 0x3F, 0x12, 0x7F, 0x82, 0xA3, 0xE9]
Note over CH: Noise corrupts<br/>0x45 becomes 0x44
CH->>RX: Receive [0x44, 0x3F, 0x12, 0x7F, 0x82, 0xA3, 0xE9]
RX->>RX: Recalculate CRC
Note over RX: Expected: 0x7F82A3E9<br/>Calculated: 0xB2C1D4F3<br/>MISMATCH!
RX->>TX: NAK - Request retransmit
TX->>CH: Resend [Data + CRC]
CH->>RX: Received intact
RX->>RX: CRC matches!
Note over RX: Data verified<br/>Accept packet
52 Error Detection: Checksums and CRC
52.1 Learning Objectives
By the end of this chapter, you will be able to:
- Calculate error detection: Compute checksums and understand CRC principles
- Compare detection methods: Evaluate trade-offs between checksums and CRC
- Choose appropriate methods: Select error detection for different reliability requirements
- Debug corrupted packets: Identify error sources using detection mechanisms
Prerequisites (Read These First): - Packet Anatomy - Headers, payloads, and trailers fundamentals - Data Representation - Binary and hexadecimal for calculations
Companion Chapters (Packet Structure Series): - Packet Structure Overview - Index of all packet structure topics - Frame Delimiters and Boundaries - How receivers detect packet boundaries - Protocol Overhead - Header comparison and encapsulation
Security: - Encryption Architecture - How encryption affects packet structure - Threats and Attacks - Packet sniffing, replay attacks
52.2 Error Detection: Checksums and CRC
Problem: Noise, interference, or hardware faults can corrupt data during transmission.
Solution: Add a calculated value in the trailer that the receiver can verify.
52.2.1 Simple Checksum
Add all bytes, take lowest 8 bits:
Payload bytes: [0x45, 0x3F, 0x12]
Checksum = (0x45 + 0x3F + 0x12) & 0xFF = 0x96
Trailer: [0x96]
Pros: Simple, fast Cons: Weak error detection (can miss burst errors)
52.2.2 Cyclic Redundancy Check (CRC)
Uses polynomial division for robust error detection: - CRC-16: 16-bit value, detects all single-bit and double-bit errors - CRC-32: 32-bit value, detects 99.9999% of errors - Used by: Ethernet, USB, LoRaWAN, Modbus
Example: Ethernet Frame Check Sequence (FCS) uses CRC-32
52.3 How CRC Works
CRC treats the data as a polynomial and divides it by a generator polynomial. The remainder becomes the CRC value:
- Data as polynomial: Each bit position represents a coefficient (e.g., 0x45 = x^6 + x^2 + 1)
- Generator polynomial: Standardized for each CRC variant (CRC-32 uses 0x04C11DB7)
- Division: XOR-based polynomial division (no carry, just XOR)
- Remainder: The final remainder is appended as the CRC
Why CRC is better than checksum:
| Error Type | Checksum | CRC-16 | CRC-32 |
|---|---|---|---|
| Single bit flip | ~50% | 100% | 100% |
| Two bit flips | ~50% | 100% | 100% |
| Transposed bytes | 0% (undetected!) | 100% | 100% |
| Burst < 16 bits | Poor | 100% | 100% |
| Burst < 32 bits | Poor | 99.998% | 100% |
| Random errors | ~50% | 99.998% | 99.9999% |
52.4 Common CRC Polynomials
| CRC Type | Polynomial | Size | Used By |
|---|---|---|---|
| CRC-8 | 0x07 | 1 byte | I2C, ATM |
| CRC-16-CCITT | 0x1021 | 2 bytes | Bluetooth, X.25 |
| CRC-16-Modbus | 0x8005 | 2 bytes | Modbus RTU |
| CRC-32 | 0x04C11DB7 | 4 bytes | Ethernet, USB, Zip |
| CRC-32C | 0x1EDC6F41 | 4 bytes | iSCSI, SCTP |
52.5 Error Detection vs. Error Correction
Error Detection (this chapter): Identifies that an error occurred, triggers retransmission
Error Correction (Forward Error Correction): Fixes errors without retransmission
| Approach | Overhead | Latency | Use Case |
|---|---|---|---|
| Detection + Retransmit | Low (2-4 bytes) | Variable | Wi-Fi, TCP, most IoT |
| FEC (Reed-Solomon) | High (10-30%) | Fixed | Satellite, LoRa physical layer |
| Hybrid ARQ | Medium | Medium | LTE, 5G |
For most IoT applications, error detection with retransmission is preferred because: 1. Errors are rare (< 1% on good links) 2. FEC overhead is expensive for constrained devices 3. Retransmission latency is acceptable for sensor data
52.6 Knowledge Check: Error Detection
Knowledge Check: Error Detection Methods Quick Check
Concept: Comparing checksum and CRC error detection.
Which error detection method can detect more types of errors: simple checksum or CRC-16?
C is correct. CRC (Cyclic Redundancy Check) uses polynomial mathematics that can detect: all single-bit errors, all double-bit errors, all odd-number errors, burst errors up to 16 bits, and 99.998% of all other errors. Simple checksums can miss transposed bytes (0x12 0x34 vs 0x34 0x12 have the same sum).
52.7 Scenario-Based Practice
52.8 Summary
Error detection ensures data integrity across noisy networks:
- Checksums: Simple addition-based method, fast but weak detection
- CRC: Polynomial-based method, detects 99.9999% of errors
- CRC-16/CRC-32: Standard choices for IoT protocols
- Trade-offs: More robust detection requires more computation and bytes
Key Takeaways: - CRC is much more reliable than simple checksums - Checksums can miss transposed bytes that CRC catches - Safety-critical systems should use CRC-32 or better - Error detection enables retransmission; error correction avoids it
52.9 What’s Next
Continue exploring packet structure with the final chapter in this series:
- Protocol Overhead - Comparing header sizes and encapsulation across protocols