CheckSum
The CheckSum function returns a checksum signature for the characters in a string.
Syntax
Variable = CheckSum (ChkSumString, ChkSumType, CheckSumSize, ChkSumOption1, ChkSumOption2, ChkSumOption3)

In the following example program, the CheckSum function is used to return a Campbell Scientific signature checksum for a value that is received over the RS232 port.
Public CheckString AS String, CheckVal
BeginProg
SerialOpen (ComRS232,9600,3,0,10000)
Scan (5,Sec,3,0)
SerialIn (CheckString,ComRS232,500,-1,100)
CheckVal=CheckSum (CheckString,5,0 )
NextScan
EndProg
In the following example program, CheckSum options 25 to 28 are shown.
Public CheckString AS String, CheckVal
BeginProg
Scan (2,Sec,10,0)
'digest for sha1 is 20 bytes
Public checksums(5) As Long, Digest1(6) As Long, Digest2(6) As Long, DigestStr(2) as String * 30
checksums(1) = CheckSum("Hello",25,0,Digest1)
checksums(2) = CheckSum("Hello",27,0,Digest2,"mykey",0)
checksums(3) = CheckSum("01",7,0)'0x30 + 0x31 = 97,
checksums(4) = CheckSum("Hello",26,0,DigestStr(1))
checksums(5) = CheckSum("Hello",28,0,DigestStr(2),"mykey",0)
Public DigestHex(2) As String *41 'so we can see the characters
Sprintf(DigestHex(1),"%8.8x%8.8x%8.8x%8.8x%8.8x",Digest1(1),Digest1(2),Digest1(3),Digest1(4),Digest1(5))
Sprintf(DigestHex(2),"%8.8x%8.8x%8.8x%8.8x%8.8x",Digest2(1),Digest2(2),Digest2(3),Digest2(4),Digest2(5))
NextScan
EndProg
Remarks
The CheckSum function returns the checksum of a String A data type used when declaring a variable consisting of alphanumeric characters. into a variable. If the string for which the CheckSum is being calculated contains one or more null characters, the Size parameter should not be set to 0.
Parameters
ChkSumString (Checksum String)
The string value for which the checksum will be calculated. If a directory and filename are entered for this parameter (for example, CPU:checkfile.txt), the CheckSum will be performed on the file. All bytes in the file will be read, and the ChkSumSize parameter is ignored.
Type: String A data type used when declaring a variable consisting of alphanumeric characters.
ChkSumType (Checksum Method)
Specifies the method of the checksum calculation. The options are:
Code | Description |
---|---|
1 | CCITT CRC16 x16+x12+x5+1 (Xmodem, seed of 0). |
2 | CRC16 used by ModBus. |
3 | CRC16 used by ModBus with 0 initializer. |
4 | CCITT CRC32 used by Campbell Scientific BMP3 protocol. |
5 | Campbell Scientific signature. |
6 | Campbell Scientific signature nullifier. |
7 | Sum all the bytes and mod by 256. |
8 | Sum all the bytes and mod by 8192 (used by Campbell Scientific array-based dataloggers). |
9 | Checksum which performs exclusive OR on all the bytes; for example, first byte XOR second byte XOR third byte XOR… last byte. |
10 | CRC16 with seed of 0 and XOR polynomial of &hA001. |
11 | CRC16 with seed of &hFFFF and XOR polynomial of &hA001. |
12 | CRC16 with seed of 0 and XOR polynomial of &h8408. |
13 | CRC16 with seed of &hFFFF and XOR polynomial of &h8408. |
14 | CRC16 reverse bit order with seed of 0 and XOR polynomial of &hA001. |
15 | CRC16 reverse bit order with seed of &hFFFF and XOR polynomial of &hA001. |
16 | CRC16 reverse bit order with seed of 0 and XOR polynomial of &h8408. |
17 | CRC16 reverse bit order with seed of &hFFFF and XOR polynomial of &h8408. |
18 | CCITT CRC16 ITU/X25. |
19 | CRC16 UKMO (UK Met office). |
22 | CRC ![]() ![]() |
23 | Returns the sum of all bytes with no masking. |
24 | Fletcher-16. |
25 | SHA1. This option requires an additional parameter that is a destination array that can hold 20 bytes. |
26 | SHA1, base 64 encoded. This option requires an additional parameter, typed as a string that can hold 29 characters. The result is null-terminated. |
27 | HMAC SHA1. This option requires three additional parameters: a destination array that can hold 20 bytes, a key, and the length of the key. If length is 0, the length of the key is used. |
28 | HMAC SHA1, base 64 encoded. This option requires three additional parameters: a destination array typed as a string that can hold 29 characters, a key, and the length of the key. If length is 0, the length of the key is used. |
29 | ![]() |
ChkSumSize (Checksum Size)
Determines the size of the checksum. If 0 is entered the size of the string is used. However, if the string will contain null characters the size should not be set to 0.
Type: Variable or Expression
ChkSumOpt1 (Checksum Options)
Additional parameter for options 25-29
Type: Variable
ChkSumOpt2 (Checksum Options)
Additional parameter for options 27-28
Type: Variable
ChkSumOpt3 (Checksum Options)
Additional parameter for options 27-28
Type: Variable