FNW
FNW

Reputation: 21

Calculate checksum for communication with Studer inverter in JavaScript (Node-RED)

I am currently working on getting the communication with an inverter of the swiss company Studer Innotec working in the Node-RED platform. I have already established TCP communication (over a MOXA ethernet to RS-232 bridge) with the device successfully and it's all about encoding and decoding the corresponding messages.

The manufacturer provides a public documentation of its RS-232 communication that also contains the following rather simple checksum algorithm:

A = 0xFF
B = 0
For I FROM 0 TO number_of_bytes -1 DO
  A := (A + DATA[I]) mod 0x100;
  B := (B + A) mod 0x100;
END
checksum[0] := A
checksum[1] := B

I tried to come up with a JavaScript version of this that works in Node-RED, but unfortunately my function does not output the same result as mentioned in an example within the specification:

function computeChecksum(buffer) {
  let a = 255;
  let b = 0;
  
  for (let i = 0; i < buffer.length; i++) {
    a = (a + buffer[i]) % 256;
    b = (b + a) % 256;
  }
  
  let checksum = Buffer.alloc(2);
  checksum[0] = a; // should be 0x6F, but is 0x19 (= 25 as decimal)
  checksum[1] = b; // should be 0x71, but is 0x68 (= 104 as decimal)
  
  return checksum;
}

I am using the following (currently still static for test purposes) input buffer (as mentioned on page 30 of the specification for the header checksum):

[0xAA, 0x00, 0x01, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x0A, 0x00]

The return values of my checksum calculation are the bytes

[0x19, 0x68]

but they should be

[0x6F, 0x71]

So I guess I have gotten something wrong in my function...

As I am not really a "low-level" developer, I hope some experienced (micro controller?) folks can help me out here.

Thanks in advance for any support!

Andreas

Upvotes: 1

Views: 275

Answers (1)

FNW
FNW

Reputation: 21

After some more fiddling around, I can answer the question myself: The checksum calculation above works just fine.

It was just pure stupidity: The first byte (0xAA) simply does not belong to the buffer passed to the checksum function according to the specification.

Without that byte, the result is correct - so sorry for wasting your time...

Upvotes: 1

Related Questions