0

I'm building and UDP server to communicate with GPS devices. It's built in node using dgram.

I have this data to calculate its checksum:

>RGP261222120013-3520041-05908923000176700DF4400;ID=4718;#44E6;*24<

GPS device manual says something like this: Checksum: in hex format, it's calculated by XOR all ASCII codes of the characters composing the message starting with the > and finishing at the last ; (included) but not including the last asterisk

Therefore, the string to calculate the checksum is >RGP261222120013-3520041-05908923000176700DF4400;ID=4718;#44E6;

Device's manual gives an example in C#, and I tried to adapt it to JavaScript:

C# example

public static string calculateChecksum(string data)
{
  int r;
  int calc = 0;
  byte caracter;
  string calculated_checksum; 
  for (r = 0; r < data.Length; r++) {
    if ((data[r] == '*') && (data[r-1] == ';')) break;
    caracter = (byte)data[r];
    calc = calc ^ (byte)caracter; 
  }
  calculated_checksum = calc.ToString("X"); 
  return calculated_checksum;
}

My JS adaptation so far

export const calculateChecksum = (packet) => {
  const len = packet.length;
  const position = packet.indexOf(';*') + 1;
  const packetToCheck = packet.slice(0, position);

  let checksum = 0;

  for (let char of packetToCheck) {
    checksum ^= char.charCodeAt(0);
  };

  return checksum.toString(16);
}

From other software I get that the response to that data is:

>ACK;ID=4718;#44E6;*26<

Being 26 the calculated checksum (and this is the right result). The device will repeat the message if the checksum sent by the server is wrong.

With my approach, I'm getting a result of 24.

I really don't know how to move on.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
leandronn
  • 173
  • 1
  • 17
  • I ran your C# code and it also returns 24. – trincot Dec 31 '22 at 20:33
  • And I get 24 using some python code I cooked up. – President James K. Polk Dec 31 '22 at 20:41
  • 1
    Maybe you are mixing up things. There are two strings you presented. They have their checksum following after the `;*` sequence near the end. The first string has checksum 24 and the second (the "ACK" one) has checksum 26. This is also what the C# code returns for these strings, and so also the JS code. I don't see really what your question is. – trincot Dec 31 '22 at 20:46
  • Oh my god. You're right. I was mixing up things. The 26 in the "ack" message is the checksum for that message. I need to check if the checksum of the RGP is right and then return the ACK. Really appreciate the help. Thanks a lot. – leandronn Dec 31 '22 at 21:20
  • Please, if you want, add an answer with this response and I'll accept it as the correct one – leandronn Dec 31 '22 at 21:25

1 Answers1

0

I was misunderstanding the documentation. I don't delete the question so everyone has access to the function.

leandronn
  • 173
  • 1
  • 17