calc_cksum = 0 for s in nmeadata: calc_cksum ^= ord(s)
return nmeadata,'0x'+cksum,hex(calc_cksum)
if __name__=='__main__': line = "$GPGGA,092725.00,4717.11399,N,00833.91590,E,1,08,1.01,499.6,M,48.0,M,,*5b\n" data, cksum, calc_cksum = checksum(line)
if cksum != calc_cksum: print("Error in checksum for: %s" % (data)) print("Checksums are %s and %s" % (cksum, calc_cksum)) else: print("checksum is: %s" % (calc_cksum))
-- GPS串口写命令操作 -- @string cmd,GPS指令(cmd格式:"$PGKC149,1,115200*"或者"$PGKC149,1,115200*XX\r\n") -- @bool isFull,cmd是否为完整的指令格式,包括校验和以及\r\n;true表示完整,false或者nil为不完整 -- @return nil -- @usage gps.writeCmd(cmd) function writeCmd(cmd,isFull) local tmp = cmd if not isFull then tmp = 0 for i=2,cmd:len()-1 do tmp = bit.bxor(tmp,cmd:byte(i)) end tmp = cmd..(string.format("%02X",tmp)):upper().."\r\n" end uart.write(uartID,tmp) log.info("gps.writecmd",tmp) --log.info("gps.writecmd",tmp:toHex()) end
开发者也可以使用以下代码测试校验值:
1 2 3 4 5 6 7 8 9 10 11 12 13
functioncheckSum(cmd) local tmp = cmd
tmp = 0 for i=1,cmd:len() do tmp = bit.bxor(tmp,cmd:byte(i)) end tmp = cmd..(string.format("%02X",tmp)):upper().."\r\n"
return tmp end print(checkSum("GPGGA,092725.00,4717.11399,N,00833.91590,E,1,08,1.01,499.6,M,48.0,M,,"))