CRC
Volume Number: 9
Issue Number: 4
Column Tag: Pascal Workshop
CRCs in Pascal 
Checking your data when transferring from one computer to another
By Mark R. Drake, Minneapolis, Minnesota
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
About the author
Mark R. Drake is a programmer for Detector Electronics in Minneapolis,
Minnesota. His main areas of interest are communications and industrial computer
control.
When transferring data from one computer to another via serial devices, the
careful application of data integrity checking is required to ensure the validity of the
data. In the industrial computer world, data that is transferred from one computer to
another may be controlling a large piece of machinery and if the data contains an error
and the error is not detected, damage to the machinery and injury to personal may
occur.
Several different methods of data error checking are used, but we will cover the
one that is used most often in the Macintosh world.
One of the best data error checking methods that can be used is the “Cyclical
Redundancy Check” (CRC). Without getting into the mathematical reasons why CRC is
good, we will just let stand that it offers a high level of protection. If you want more
information on the mathematical properties of CRC generation, your local library
should have plenty of information.
You can calculate a CRC check value on a block of bytes by performing a few
initializations and then test each bit in each byte to determine if you do one of two
possible operations. Refer to Example One. You would pass each byte in the block to the
“DoCrcCal” procedure. After you have tested all of the bytes in the block, you will
have the CRC check value. As you can see, that method is very slow because you have to
test every bit and then do a logical operation to decide which operation to perform. On a
Macintosh with a 2400 baud modem, you will not notice the loss of time. With any
higher baud rates the lost time is substantial.
Example One
var
crcCC: longint;
procedure DoCrcCal(theData:longint);
const
hiBitMask = $08000;
polyCCITT = $01021; {CRC-CCITT polynomial}
polyCRC16 = $0A001; {CRC16 polynomial}
var
loop: integer;
tempdata, crcXor: longint;
begin
crcCC:= BitXor(BitShift(theData,8),crcCC);
for loop := 1 to 8 do
begin
crcXor := BitAnd(crcCC,hiBitMask);
if BitTst(@crcXor,16) then