0% found this document useful (0 votes)
33 views3 pages

DC-18 App-B Vivek Ver2 PDF

This document contains the source code listing for a cyclic redundancy check (CRC) program. The program defines a data structure called "dat" used to pass data between objects. It also defines the polynomial used for CRC-16 checksums. The main function "whatcrc" takes a dat structure, calculates the CRC checksum by loading the data structure fields into a temporary buffer, performing the CRC algorithm on each byte, and updating the CRC field in the dat structure.

Uploaded by

floreda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

DC-18 App-B Vivek Ver2 PDF

This document contains the source code listing for a cyclic redundancy check (CRC) program. The program defines a data structure called "dat" used to pass data between objects. It also defines the polynomial used for CRC-16 checksums. The main function "whatcrc" takes a dat structure, calculates the CRC checksum by loading the data structure fields into a temporary buffer, performing the CRC algorithm on each byte, and updating the CRC field in the dat structure.

Uploaded by

floreda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Appendix B

Cyclic redundancy check


(CRC) program listing
#i ncl ude <mal l oc. h>
#i ncl ude <st dl i b. h>
#i ncl ude <st di o. h>

#i f ndef DAT / / t o avoi d mul t i pl e def i ni t i ons due t o or der
#def i ne DAT / / of #i ncl udes
st r uct dat / / t hi s i s a dat a st r uct ur e used f or passi ng bet ween obj ect s
{
i nt addr , / / addr ess of devi ce
f cn, / / number of f unct i on
dcount , / / number of el ement s i n dat a
cr c; / / cr c check code
char *dat a; / / poi nt er t o dat a
};
#endi f

st at i c unsi gned CRC16=0xA001; / / Pol ynomi al used f or CRC- 16 checksum

uni on doub { / / uni on f or CRC check
unsi gned i ; / / as an unsi gned wor d
char c[ 2] ; / / as t wo char act er s
st r uct bi t s{ / / as a bi t f i el d
332 Practical Data Communications for Instrumentation and Control
unsi gned msb: 1; / / most si gni f i cant bi t
unsi gned: 14;
unsi gned l sb: 1; / / l east si gni f i cant bi t
} b;
};

voi d what cr c( st r uct dat *d, i nt mode) / / cal cul at e a CRC gi ven a dat st r uct ur e
{
char *msg; / / buf f er t o message
i nt i , j , l en; / / count er s and l engt h of message
uni on doub sck, byt ;

l en=( mode?2: 0) +2+( d- >dcount ) ; / / cal cul at e l engt h ( dat a i s t he onl y f i el d
wi t hout f i xed l engt h)
msg = ( char *) mal l oc( l en) ; / / al l ocat e space f or message buf f er
i f ( ! msg) / / di dn' t happen? Say so and qui t .

{
pr i nt f ( " Sor r y, but I coul dn' t al l ocat e memor y\ n" ) ;
exi t ( 1) ;
}

/ / Load t he msg buf f er
msg[ 0] =d- >addr ; / / l oad t he addr f i el d as byt e 1
msg[ 1] =d- >f cn; / / l oad t he f cn f i el d as byt e 2

f or ( i =0; i <d- >dcount ; ++i )
msg[ i +2] =d- >dat a[ i ] ;

i f ( mode)
{
msg[ i +2] =( d- >cr c&0xFF00) >>8;
msg[ i +3] =d- >cr c&0x00FF;
}


/ / CRC check al gor i t hml i ve!

sck. i =0xFFFF; / / set i ni t i al r emai nder
f or ( i =0; i <l en; ++i ) / / f or each byt e i n buf f er
Appendix B CRC listing 333

{
byt . c[ 0] =msg[ i ] ; / / put t he cur r ent char act er at end of wor ki ng byt
byt . c[ 1] =0; / / set st ar t of byt t o 0
sck. i ^=byt . i ; / / set sck t o sck XOR byt ( MOD- 2 mat hs)
f or ( j =0; j <8; ++j ) / / f or each bi t
{
i f ( sck. b. msb)
{
sck. i >>=1; / / shi f t t he r emai nder r i ght 1 bi t
( di vi de by 2)
sck. b. l sb=0; / / and set t he MSB t o 0
sck. i ^=CRC16; / / set r emai nder = sck XOR t he CRC16
pol ynomi al
}
el se
{
sck. i >>=1; / / shi f t t he r emai nder r i ght 1 bi t
( di vi de by 2)
sck. b. l sb=0; / / and set t he MSB t o 0
}
}
}
d- >cr c = ( sck. i ) <<8; / / updat e t he CRC i n t he dat a st r uct ur e
d- >cr c | = ( sck. i &0xFF00) >>8;
f r ee ( msg) ; / / f r ee t he t empor ar y st or age ar ea
}

You might also like