How dose the checksum work in ascii protocol?

I’m working in python script to let Odrive work , but I really can’t understand the checksum in ascii protocl is how to work.
The official document said:

The ASCII protocol is human-readable and line-oriented, with each line having the following format:

command *42 ; comment [new line character]

  • *42 stands for a GCode compatible checksum and can be omitted. If and only if a checksum is provided, the device will also include a checksum in the response, if any. If the checksum is provided but is not valid, the line is ignored. The checksum is calculated as the bitwise xor of all characters before the asterisk (*).
    Example of a valid checksum: r vbus_voltage *93.
  • comments are supported for GCode compatibility
  • the command is interpreted once the new-line character is encountered

How can calculate ‘r vbus_voltage’ to ‘*93’

Finally,I know how can it be.
If you need to calculate ‘r vbus_voltage ’, just turning ‘r’ and ’ ’ to ASCII, and xor them to first checksum. Then, continue turing and xor them until the last one ’ '.
now you get the bitwise xor of all characters, you can get the ‘93’, after you turn it to ‘int’


If anyone is looking for an implementation of “GCode compatible checksum”, here’s mine. Given the command is at least 2 chars long:

def gcode_csum(cmd):
    cmd = cmd + " "
    cs = ord(cmd[0]) ^ ord(cmd[1])
    for c in cmd[2:]:
        cs = cs ^ ord(c)
    return cmd + "*" + str(cs)

cmd = "r vbus_voltage"
# prints: r vbus_voltage *93

Works against the example, but if you notice bugs, please feel free to contribute :slight_smile:


Man thank you so much this code helped me with my 3d printer terminal sorry if this is off topic I just wanted to thank you to share this