Dynalite Checksum Formula

John PaulJohn Paul Junior MemberPosts: 143
I have a Dynalite device for which the commands require a checksum and i wanted to write a function call which will do the checksum calculation for me. I know there are modules available but i wanted to add a simple include file instead. Here is the function i have written, which is incomplete.

DEFINE_FUNCTION INTEGER CKS(INTEGER P1,INTEGER P2,INTEGER P3,INTEGER P4,INTEGER P5,INTEGER P6,INTEGER P7)
{
LOCAL_VAR INTEGER nCheckSum

nCheckSum = !(P1+P2+P3+P4+P5+P6+P7) +1


RETURN nCheckSum

}


Here is the formula for the checksum.

1) In Windows, Start / Programs / Accessories / Calculator

2) In Calculator, under View, Select Scientific

3) In Calculator, select Hex

4) Add the first 7 Hex bytes, then press = (ie: 1C + 01 + 20 + 00 + 00 + 00 + FF total = 13C)

5) Press the <Not> button (in our example, the total = FFFFFEC3)

6) Add 1 to the total (in our example, the total = FFFFFEC4)

7) The checksum is the two least significant characters (in our example, checksum = C4)

Comments

  • George KrietsepisGeorge Krietsepis Junior Member Posts: 159
    John Paul wrote: »
    I have a Dynalite device for which the commands require a checksum and i wanted to write a function call which will do the checksum calculation for me. I know there are modules available but i wanted to add a simple include file instead. Here is the function i have written, which is incomplete.

    DEFINE_FUNCTION INTEGER CKS(INTEGER P1,INTEGER P2,INTEGER P3,INTEGER P4,INTEGER P5,INTEGER P6,INTEGER P7)
    {
    LOCAL_VAR INTEGER nCheckSum

    nCheckSum = !(P1+P2+P3+P4+P5+P6+P7) +1


    RETURN nCheckSum

    }


    Here is the formula for the checksum.

    1) In Windows, Start / Programs / Accessories / Calculator

    2) In Calculator, under View, Select Scientific

    3) In Calculator, select Hex

    4) Add the first 7 Hex bytes, then press = (ie: 1C + 01 + 20 + 00 + 00 + 00 + FF total = 13C)

    5) Press the <Not> button (in our example, the total = FFFFFEC3)

    6) Add 1 to the total (in our example, the total = FFFFFEC4)

    7) The checksum is the two least significant characters (in our example, checksum = C4)



    John, try the below code:

    DEFINE_VARIABLE

    volatile CONTROL_MESSAGE[8]


    DEFINE_CALL 'CheckSum Calculator' (CHAR BYTE_1,CHAR BYTE_2,CHAR BYTE_3,CHAR BYTE_4,CHAR,BYTE_5,CHAR BYTE_6,CHAR BYTE_7)

    LOCAL_VAR
    CHAR COUNT
    CHAR TOTAL
    CHAR CHECKSUM
    {
    CONTROL_MESSAGE[1] = BYTE_1
    CONTROL_MESSAGE[2] = BYTE_2
    CONTROL_MESSAGE[3] = BYTE_3
    CONTROL_MESSAGE[4] = BYTE_4
    CONTROL_MESSAGE[5] = BYTE_5
    CONTROL_MESSAGE[6] = BYTE_6
    CONTROL_MESSAGE[7] = BYTE_7
    COUNT = 1
    TOTAL = 0
    WHILE (COUNT <8)
    {
    TOTAL = TOTAL+CONTROL_MESSAGE[COUNT]
    COUNT = COUNT+1
    }
    CHECKSUM = (TOTAL * $FF) BAND $FF
    CONTROL_MESSAGE[8] = CHECKSUM
    SET_LENGTH_STRING(CONTROL_MESSAGE,8)
    }

    George
  • ericmedleyericmedley Senior Member - 3709 Posts Posts: 4,126
    The 'NOT' function in Netlinx is BNOT,

    ! Is a comparison. BNOT is a calculation.
  • viningvining X Member Posts: 4,328
    I needed this calculation a couple of weeks ago so here's the function I came up with and a snippet of code that uses it.
    DEFINE_FUNCTION CHAR fn2CompChkSum(INTEGER iStartByte,INTEGER iEndByte,CHAR iStr[])
    	
         {
         STACK_VAR INTEGER i;
         STACK_VAR CHAR cChkSum;
         
         if(!iStartByte) iStartByte = 1;
         if(!iEndByte) iEndByte = LENGTH_STRING(iStr);
         for(i = iStartByte ; i <= iEndByte ; i++)
    	  {
    	  cChkSum = cChkSum + iStr[i];
    	  }
         cChkSum = ((BNOT cChkSum)+1);// & $FF);// & FF not needed since return is a single char
         //fnDev_DeBug("'2CompChkSum-[ ',cChkSum,' ] :DEBUG<',ITOA(__LINE__),'>'");
             
         RETURN cChkSum;
         }
         
    DEFINE_FUNCTION CHAR fnTV_ProcessQ() 
         
         {
         STACK_VAR INTEGER nFBS;
         
         nFBS = find_string(cTV_TxQueue,"TV_QUEUE_DELIM",1);
         if(nFBS)
    	  {
    	  STACK_VAR CHAR cQcmd[6];
    	  
    	  cQcmd = "SAM_CMD_PREFIX,GET_BUFFER_STRING(cTV_TxQueue,nFBS-1)";
    	  REMOVE_STRING(cTV_TxQueue,"TV_QUEUE_DELIM",1);
    	  
    	  cTV_LastCmd = "cQcmd,fn2CompChkSum(0,0,cQcmd)";
    	  SEND_STRING dvTV,cTV_LastCmd;
    

    The function allows you to pass it the start byte and end byte of the string to calculate or pass the function 0,0, and it will start at 1 and use the length of the string passed as the byte count.
  • champchamp Junior Member Posts: 261
    You really should just use the AMX module as it is really good and quick to implement.
  • John PaulJohn Paul Junior Member Posts: 143
    Thanks a lot for all the inputs.
  • JubalJubal Junior Member Posts: 77
    champ wrote: »
    You really should just use the AMX module as it is really good and quick to implement.
    where can i get the checksum module?
  • viningvining X Member Posts: 4,328
    Jubal wrote: »
    where can i get the checksum module?
    I think champ was referring to the Dynalite module not a checksum module since there is no checksum module that I am aware of. If you need a checksum function you might try posting your devices checksum requirements and maybe someone will help. This is now a ghost town of a forum so good luck with that.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file