beautypg.com

PNI CompassPoint Prime User Manual

Page 61

background image

Prime User Manual r08

Page 55

if(len > kBufferSize - kPacketMinSize) return;


// Store the total len of the packet including the len

// bytes(2), the frame ID(1), the data (len), and the crc (2).

// If no data is sent, the min len is 5

mOutData[index++] = count >> 8;

mOutData[index++] = count & 0xFF;

// store the frame ID

mOutData[index++] = frameType ;

// copy the data to be sent

while(len--) mOutData[index++] = *data++;

// compute and add the crc

crc = CRC(mOutData, index);

mOutData[index++] = crc >> 8 ;

mOutData[index++] = crc & 0xFF ;

// Write block will copy and send the data out the serial port

mSerialPort->WriteBlock(mOutData, index);

}



//
// Call the functions in serial port necessary to change the
// baud rate
//
void CommProtocol::SetBaud(UInt32 baud)
{

mSerialPort->SetBaudRate(baud);


// clear any data that was already waiting in the buffer

mSerialPort->InClear();

}

//
// Update the CRC for transmitted and received data using the
// CCITT 16bit algorithm (X^16 + X^12 + X^5 + 1).
//
UInt16 CommProtocol::CRC(void * data, UInt32 len)
{

UInt8 * dataPtr = (UInt8 *)data;

UInt32 index = 0;


UInt16 crc = 0;

while(len--)

{

crc = (unsigned char)(crc >> 8) | (crc << 8);

crc ^= dataPtr[index++];

crc ^= (unsigned char)(crc & 0xff) >> 4;

crc ^= (crc << 8) << 4;

crc ^= ((crc & 0xff) << 4) << 1;