0

I want to send a message to the server as per the AIS 140 standard. Please explain how to calculate the checksum. Find below the sample message format.

$Header,iTriangle,KA01I2000,861693034634154,1_37T02B0164MAIS,AIS140,12.976545,N,77.5497 59,E*50 
Thirumal
  • 8,280
  • 11
  • 53
  • 103

1 Answers1

0

As per AIS140 Standard Checksum is calculated by performing xor to all bytes received from packet.

Note: You have to remove '$'.

Caution: Use data from device to verify this code (Example provided from doc doesnt have valid checksum)

This Javascript code will help your job done.

function checksum(packet) {
  const charArray = packet.split('');
  let xor = 0;
  const n = charArray.length;
  for (let i = 1; i < n - 3; i++) {
    xor = xor ^ charArray[i].charCodeAt(0);
  }
  const cs = parseInt("0x" + charArray[n - 2] + charArray[n - 1]);
  return xor === cs;
}
checksum('$Header,iTriangle,KA01I2000,861693034634154,1_37T02B0164MAIS,AIS140,12.976545,N,77.549759,E*50')