Dynalite Checksum Formula
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)
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)
0
Comments
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
! Is a comparison. BNOT is a calculation.
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.