From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to make CRC using this algorithm?

I have a vacuum gauge that communicates with RS-485, and the programming manual gives some example C code for generating the required CRC byte. Unfortunately, my C programming is a bit rusty and I don't recognize some of the operators, and they don't provide a verbal description of the algorithm. Can anybody help me to translate this into LabVIEW? Below is the part of the manual that describes it.


Thanks,
Richard Swent

 

The command protocol for the IGM-402 has the following format:
Command: <!><addr><cmd><data><CRC>
Response: <*><addr><cmd><data><CRC>
The <data> portion of the command or response can be multiple bytes in length, depending upon the
command byte (<cmd>) sent. Do not include the ‘<’ or ‘>’ within the message string. They are used
here to delineate between bytes.
The Cycle Redundancy Check <CRC> is calculated for each message as follows:
// *ptr is a pointer to the message to be sent.
// Length is the length of the message.
char Calculate_CRC8(char *ptr, char Length)
{
char CRC_Value;
char Counter;
char BitCounter;
char XOR_Byte;
char TransmitByte;
// Initialize the local variable.
CRC_Value = 0xFF;
Instruction Manual InstruTech IGM-402 "IGM" Ionization Vacuum Gauge Module
InstruTech, Inc page 33
// Calculate the CRC.
for(Counter = 0; Counter < Length; Counter++)
{
TransmitByte = *ptr;
ptr++;
BitCounter = 8;
while(BitCounter != 0)
{
BitCounter--;
XOR_Byte = TransmitByte ^ CRC_Value;
if((XOR_Byte & 0x80) != 0)
{
CRC_Value ^= 0x0E;
CRC_Value <<= 1;
CRC_Value |= 1;
}
else
{
CRC_Value <<= 1;
}
// Left shift the calculation byte.
TransmitByte <<= 1;
}
}
// Return the calculated CRC value.
return(CRC_Value);
}

0 Kudos
Message 1 of 2
(2,751 Views)

char Calculate_CRC8(char *ptr, char Length)

--- Understand that "char" is the C name for "byte".  It might be numerical, it might be ASCII, it might be something else.  And, as preposterous as it sounds, there is such a thing as an "unsigned char".

 

char CRC_Value;
char Counter;
char BitCounter;
char XOR_Byte;
char TransmitByte;

 --- Local variables.


// Initialize the local variable.
CRC_Value = 0xFF;

 

--- Fetch a value via the pointer (in LabVIEW, this would be an auto-indexed array, most likely.

TransmitByte = *ptr;

 

--- Increment the pointer ( auto-indexed arrays, we love you!)
ptr++;

 

--- Init a variable
BitCounter = 8;

 

--- "!=" means NOT EQUAL

while(BitCounter != 0)
{

 

--- Decrement the BitCounter
BitCounter--;

 

--- A new variable having the unfortunate name of XOR_byte:

--- "^" is the C operator XOR.

XOR_Byte = TransmitByte ^ CRC_Value;

 

 

"&" is the C operator AND (bitwise):

"!=" is NOT EQUAL:
if((XOR_Byte & 0x80) != 0)

 
{

 

"^=" is the C operator XOR EQUAL (bitwise)

This statement could be expressed: CRC_Value = CRC_Value ^ 0x0F;
CRC_Value ^= 0x0E;

 

"<<=" means "Replace with left-shifted value", and could be expressed "CRC_Value = CRC_Value << 1"
CRC_Value <<= 1;

 

"|=" is the C operator OR EQUAL (bit wise), and could be expressed "CRC_Value = CRC_Value OR 1"
CRC_Value |= 1;
}
else
{

"<<=" means "Replace with left-shifted value", and could be expressed "CRC_Value = CRC_Value << 1"
CRC_Value <<= 1;


}
// Left shift the calculation byte.
TransmitByte <<= 1;
}
}
// Return the calculated CRC value.
return(CRC_Value);
}

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


Blog for (mostly LabVIEW) programmers: Tips And Tricks

Message 2 of 2
(2,713 Views)