Home AMX User Forum NetLinx Studio

CheckSum8 Modulo 256

Hi,

I need advice on how to implement CheckSum8 Modulo 256 in Netlinx Studio.

In past, I used that function to calculate checksum:

DEFINE_FUNCTION CHAR BXORcheckSum(CHAR sBytes[])
{
stack_var char this_sum
STACK_VAR INTEGER nX
this_sum = $00

FOR(nX=2;nX<=5;nX++)
{
this_sum = this_sum BXOR sBytes[nX]

SEND_STRING 0, ("'Checksum PRocess Result: ',this_sum")

}

RETURN this_sum

}

That function worked correctly, but now I control different TVs, and now works only with checksum calculation using CheckSum8 Modulo 256

https://www.scadacore.com/tools/programming-calculators/online-checksum-calculator/

Comments

  • Marc ScheibeinMarc Scheibein Posts: 881
    edited March 25

    It's pretty simple... create sum of all bytes the checksum has to be calculated over, and then do a MOD 256 to that sum, to get just the lower 8bits of the sum

    // calculates the MOD256 value of a String
    // char sString: the string to calculate the checksum of
    // returns a char with the chksum value
    define_function char chkSumMod256(char sString[])
    {
        stack_var integer nSum;
        stack_var char cChkSumByte;
        stack_var nX;
        for(nX=1;nx<=length_array(sString),nX++)
        {
            nSum = nSum + sString[nX];
        }
        cChkSumByte = type_cast(nSum % 256); // MODulo; 256dec / $100 hex; typecasting required as converting  integer (16bit) to char(8bit)
        return cChkSumByte;
    }
    
  • Hi,

    Thanks for the advice, but unfortunately this code doesn't work entirely correctly.
    I want to send a string to increase and decrease the volume like that: $AA$12$01$01$0F$(CRC)
    Where I have to calculate CRC every time that's why I need this function.

    And now when I put this string into the CRC calculator from this website:
    https://www.scadacore.com/tools/programming-calculators/online-checksum-calculator/
    in this form: 1201010F, I get CRC = 23 and this is how it should be(result in attachment).
    Whereas with your code I get 12

  • hm, I added a little code around, the function is unchanged, working as expected...
    rename the txt file to axs.

    The only difference I see just now is that in the first code I had a comma in the FOR() parameters instead of a semicolon. But this gives a syntax error when compiling, so should not have an effect to the result.

  • richardhermanrichardherman Posts: 401

    I couldn't see what would be wrong with the code either, so I ran it and it produces the right checksum. when I put in:

    "$12,$01,$01,$0F"

    23 comes out as the checksum, as expected. So don't know what is different on your side

  • Hi.

    Yes, the function works correctly, it's my fault in implementing it in my code.
    Thanks, Marc Scheibein for help.

Sign In or Register to comment.