ASCII Character Sum

I have a device that has a checksum that is the addition of the ASCII values and truncated to the rightmost four digits.

I have searched the documentation for a way to read this checksum and generate a similar one, but have so far come up empty handed. Any hints would be appreciated.

Kind Regards,
Josh

Can you give us a little more to go on here?
The device type and model number? Link to the manual perhaps?
Also are you trying to do the checksum calc in PAC Control or Node-RED or something else?

Sorry I should get better at including more information. The device is an LCI-90i ( Featured Products & Solutions | Rugged Controls ). The bits of interest are on pages 79-81. I am planning on using the MTNW1 or MTNW Legacy protocol.

The device generates a checksum by adding up the ASCII values of the string it is about to send then appends it to the end of the string. I am looking to be able to validate the data and recreate this type of checksum.

From the documentation I started to think I could go through it charecterwise, but then wasn’t sure how to go about that on the string.

I have used the PAC Control Checksum function successfully in the past with other devices.

Hmmm. I am rusty at this stuff… Paging @philip and @torchard

My simple mind just wants to undo the existing PAC Control command…

Can you just multiply the result by 256 to get back to step 1?
That way you just have the sum of the string which is what you need…

That was my first thought, but since it is an integer value it has lost any data that would be after the decimal point.

You’re right, multiplying the built in won’t work.

The checksum value can be calculated manually:

nLength = GetStringLength(sDataField);

if (nLength > 0) then
  nChecksum=0;
  for nIndex=0 to nLength - 1 step 1
    nChecksum = nChecksum + (sDataField[nIndex] bitand 0x7f);
  next
endif

I think you will need to convert this nChecksum integer value to it’s string representation though.

2 Likes

Thank you for this information. I am just trying to understand the function: sDataField is the string to calculate. I am unsure what bitand and 0x7f are.

Yes sDataField would be your payload. The 0x7f is 0111 1111 in binary, it is to look at the last 7 bits of the character only. Since it is an ASCII protocol, the first bit should be 0 for each character - so it is more of a defensive programming thing, you could leave the bitand 0x7f off and it should still work fine.

Converting to a string and truncating can be done like this:

//convert to string representation
NumberToString(nChecksum, sChecksum);

//get the last 4
nLength = GetStringLength(sChecksum);
if(nLength > 4) then
  //need to truncate
  GetSubstring(sChecksum,  nLength-4, 4, sChecksum);
endif


//Append to payload
sDataField = sDataField + sChecksum + Chr(13) + Chr(10)
1 Like