VTI Instruments Corp.
APPENDIX C
106
{
int
len = buf.GetCount();
printf(
"Packet length: %i\n"
, (
int
)len);
for
(
int
i = 0; i < len; i++)
{
printf(
"%02X "
, buf[i]);
}
printf(
"\n"
);
}
CRC Checking
The CRC check function is called to calculate a CRC16 for the written data and the read data on the
DS2431. Here is the code for, and a brief explanation of, this function:
unsigned
int
CRCcalc(CComSafeArray<
short
> buf,
int
offset,
int
len)
{
int
i, j, k;
short
byte;
unsigned
int
r = 0;
for
(k = (offset-1); k < (( len)-1); k++)
{
// CRC16 CALCULATION
byte = buf[k];
for
(i=0; i!=8; byte>>=1, i++)
{
j=(byte^r)&1;
r>>=1;
if
(j) {
r^=0xa001;
}
}
}
r = ( ((r & 0x00FF) << 8) | ((r & 0xFF00) >> 8) );
// bit-
swapping for endian-ness
r = (
unsigned
short
)~r;
// inverting to match MLAN
printf(
"CRC16: %02X\n"
, r);
return
r;
}
The function first calculates a normal CRC16 and then does a bit-shift operation to swap the top and
bottom halves of the CRC bytes (due to platform endian-ness) before inverting the CRC.
This is what the MLAN bus master will return to us on a little-endian host computer, like an x86
CPU (Intel or AMD): a byte-swapped, inverted CRC16. This way, the CRC16 value can be visually
compared instead of having to do bitwise operations on what the MLAN device gives us.
Version Information
The last function which has not yet been discussed is the REPEATER_TEST function. The purpose
of this function is to query the MLAN command repeater inside the instrument and retrieve several
items of data from it, including the version of the MLAN protocol it implements and the vendor
identification string.
NOTE
The version and vendor strings will both come back as null-terminated strings of hexadecimal digits,
as the same PrintPacket function is used to print them as is used for the rest of the packets.
For reference, the ML100 MLAN version string should appear as 4D 4C 31 30 30 00.