You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在接收的APRS消息比特流上应用CRC16算法(Python实现)

Correctly Applying CRC16 for APRS Messages

Let’s work through fixing your CRC16 calculation for your APRS message—this is a common pitfall because AX.25 (the protocol underpinning APRS) uses specific CRC parameters that are easy to mix up.

Key APRS CRC16 Parameters You Need to Follow

You’re using the right polynomial (x¹⁶+x¹²+x⁵+1 = 0x1021), but there are other critical details you might have missed:

  • Initial CRC value: 0xFFFF (not 0x0000, which is a common mistake)
  • Bit processing order: Each byte is processed from least significant bit (LSB) to most significant bit (MSB)
  • Final output: The calculated CRC value is used directly as the checksum (sent as two bytes, low byte first, high byte second)

Python Implementation for APRS CRC16

Since you’re working in Python, here’s a bit-level implementation that follows AX.25 standards perfectly. This handles the LSB-first processing your stream requires (since you mentioned stream is the message before reversing each byte’s bit sequence):

def calculate_aprs_crc16(data: bytes) -> int:
    crc = 0xFFFF
    poly = 0x1021  # x^16 + x^12 + x^5 + 1

    for byte in data:
        # Process each bit of the byte from LSB to MSB
        for _ in range(8):
            # Extract the current LSB of the byte
            bit = byte & 0x01
            # Get the MSB of the current CRC value
            crc_msb = (crc >> 15) & 0x01

            # Shift CRC left by 1 bit, keeping it 16-bit
            crc = (crc << 1) & 0xFFFF

            # If the bit and CRC MSB differ, apply the polynomial
            if bit != crc_msb:
                crc ^= poly

            # Shift the byte right to process the next bit
            byte = byte >> 1

    return crc

How to Use This

  1. Convert your stream variable to a bytes object (if it’s an ASCII string, use stream.encode('ascii'))
  2. Call the function: checksum = calculate_aprs_crc16(your_stream_bytes)
  3. The result will be the 16-bit CRC remainder you’re expecting. If you need to split it into the two bytes sent in APRS, use low_byte = checksum & 0xFF and high_byte = (checksum >> 8) & 0xFF

Troubleshooting Tips

If you’re still getting the wrong remainder:

  • Double-check that you correctly removed bit stuffing (AX.25 uses 0x7E as a flag, and stuffs a 0 after five consecutive 1s—make sure you didn’t accidentally remove valid bits)
  • Verify your decoded stream matches the raw message exactly (excluding flags and stuffed bits)
  • Confirm you’re not accidentally reversing bytes in the stream (APRS messages are sent in byte order as they appear in the ASCII string)

内容的提问来源于stack exchange,提问作者Andreas Hornig

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:20:47