Skip to content

Instantly share code, notes, and snippets.

@tomazas
Last active October 8, 2023 17:59
Show Gist options
  • Save tomazas/3ab51f91cdc418f5704d to your computer and use it in GitHub Desktop.
Save tomazas/3ab51f91cdc418f5704d to your computer and use it in GitHub Desktop.
uBlox UBX checksum calculator
# CFG-RATE (0x06,0x08) packet (without header 0xB5,0x62)
# payload length - 6 (little endian format), update rate 200ms, cycles - 1, reference - UTC (0)
packet = [0x06,0x08, 0x06,0x00, 0x00,0xC8, 0x00,0x01, 0x00,0x00]
CK_A,CK_B = 0, 0
for i in range(len(packet)):
CK_A = CK_A + packet[i]
CK_B = CK_B + CK_A
# ensure unsigned byte range
CK_A = CK_A & 0xFF
CK_B = CK_B & 0xFF
print "UBX packet checksum:", ("0x%02X,0x%02X" % (CK_A,CK_B))
@vlsireddy
Copy link

vlsireddy commented Jul 19, 2016

Hi,
i tried it on on the last command at this link

https://ukhas.org.uk/guides:ublox_psm

GPS Cold Start (Forced Watchdog)
0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0xFF, 0x87, 0x00, 0x00, 0x94, 0xF5

it fails

it throws
0x94 0x1D instead of above 0x94 0xF5

not sure which is correct, can you please help

@tomazas
Copy link
Author

tomazas commented Dec 18, 2016

You are calculating it wrong. The checksum is calculated only from payload bytes, i.e.: 0x06, 0x04, 0x04, 0x00, 0xFF, 0x87, 0x00, 0x00
No header or checksum bytes are involved in calculation.

After running with your payload, the result is:
UBX packet checksum: 0x94,0xF5

@AmirMahdiHassanzadeh
Copy link

AmirMahdiHassanzadeh commented Oct 8, 2023

#UBX packet checksum

hi that's code works:
I will give you my tested sample
(rust lang..)
fn main() {
let data =[ 0xB5,0x62,0x01, 0x07,0x54,0x00, 0xBC,0xB5,0x4B, 0x0B,0xE2,0x07, 0x06,0x05,0x04, 0x26,0x0D,0x07, 0x04,0x00,0x00, 0x00,0x8A,0xCA, 0xF7,0x05,0x03, 0x01,0x0A,0x0C, 0x97,0x4A,0x5A, 0x57,0xA0,0xD3, 0x3B,0xE9,0xF8, 0xD5,0x00,0x00, 0xC6,0xD8,0x00, 0x00,0x59,0x05, 0x00,0x00,0x1E, 0x07,0x00,0x00, 0x09,0x00,0x00, 0x00,0xDB,0xFF, 0xFF,0xFF,0xF5, 0xFF,0xFF,0xFF, 0x26,0x00,0x00, 0x00,0x70,0x85, 0x1B,0x01,0xF5, 0x00,0x00,0x00, 0x4A,0x28,0x50, 0x00,0x90,0x00, 0x00,0x00,0x84, 0xD3,0x11,0x00, 0x93,0x4F];

let mut CK_A = 0;
let mut CK_B = 0;
let len =data.len();

for i in 0..len {
if i == 0 || i == 1 || i == len-2 || i ==len-1{continue;}
CK_A = CK_A + data[i];
CK_B = CK_B + CK_A;
}
CK_A = CK_A & 0xFF;
CK_B = CK_B & 0xFF;

println!("{:#x}",CK_A);
println!("{:#x}",CK_B);
}

//AmirMahdi Hassanzadeh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment