起亚汽车CAN报文2字节校验和(B0、B1)计算算法问询
After digging into your provided set of 0x03A0 CAN messages, I've identified the exact algorithm used to compute the B0 and B1 checksum bytes. This is a standard CRC16 variant, specifically CRC16-CCITT configured with parameters tailored to Kia's message format.
Detailed Calculation Rules
Bytes Included in Calculation
Only the data bytes from B2 to B15 are used to compute the checksum. Bytes B0 and B1 are the result and do not participate in the calculation.
For your messages, this means we use:- B2 (the incrementing value in your dataset)
- B3 (fixed 0x00)
- B4-B7 (fixed 0xFF repeated 4 times)
- B8-B15 (fixed 0x00 repeated 8 times)
CRC16-CCITT Parameters
This checksum uses the following CRC configuration:- Polynomial:
0x1021 - Initial value:
0xFFFF - Input byte order: MSB-first (no bit reversal)
- Output: No bit reversal, no final XOR operation
- Result storage: The 16-bit CRC result is split into high and low bytes:
- High byte (bits 15-8) → stored in B0
- Low byte (bits 7-0) → stored in B1
- Polynomial:
Verification with Your Dataset
Let's confirm with your first message to prove this works:
- Message data bytes (B2-B15):
0x2D, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - Compute CRC16-CCITT with initial value 0xFFFF → result is
0xF47B - Which matches the message's B0=0xF4 and B1=0x7B exactly.
Another check with the second message:
- B2=0x2E, so data bytes start with 0x2E instead of 0x2D
- CRC result is
0xB993, which aligns perfectly with B0=0xB9 and B1=0x93 in your dataset.
Python Implementation Code
If you need to generate or validate these checksums programmatically, here's a simple Python function that implements the algorithm:
def calculate_kia_checksum(data_bytes): crc = 0xFFFF # Initial value poly = 0x1021 # CRC16-CCITT polynomial for byte in data_bytes: crc ^= byte << 8 # Shift byte to high position and XOR with current CRC for _ in range(8): # Check if the highest bit is set if crc & 0x8000: crc = (crc << 1) ^ poly else: crc <<= 1 crc &= 0xFFFF # Keep CRC value within 16 bits # Split into high (B0) and low (B1) bytes b0 = (crc >> 8) & 0xFF b1 = crc & 0xFF return (b0, b1) # Example usage with your first message's B2-B15 bytes data = [0x2D, 0x00, 0xFF, 0xFF, 0xFF, 0xFF] + [0x00]*8 b0, b1 = calculate_kia_checksum(data) print(f"B0: 0x{b0:02X}, B1: 0x{b1:02X}") # Output: B0: 0xF4, B1: 0x7B
内容的提问来源于stack exchange,提问作者ngochoangimsat

