Home > Software engineering >  Turn to stick: MFC PC serial port to send data to add two bytes of crc16 check
Turn to stick: MFC PC serial port to send data to add two bytes of crc16 check

Time:09-21

Crc16 calibration algorithm code, but not with,
Purpose: it is need to take before 14 bytes of data (hexadecimal) to calculate two bytes of crc16 check (hexadecimal), and then sent along to the next bit machine, code is c + + MFC,

Novice, a headache for a long time, oneself also baidu for a long time, just can't write, hope god can give a reference code, or advise me, thank you ~

CodePudding user response:

/* * * * * crc16. H * * * * */
//*
# define CRC16_DNP 0 x3d65//DNP, IEC 870, M - BUS, wM - BUS,...
# define CRC16_CCITT 0 x1021//x. 25, v. 41, in HDLC FCS, Bluetooth,...

//Other polynoms not *
# define CRC16_IBM 0 x8005//ModBus, USB, Bisync, CRC - 16, CRC - 16 - ANSI,...
# define CRC16_T10_DIF 0 x8bb7//SCSI DIF
# define CRC16_DECT 0 x0589//Cordeless Telephones
# define CRC16_ARINC 0 xa02b//ACARS Aplications

# define POLYNOM CRC16_XXX//define the informs the POLYNOM from one of the aboves

//It calculates the new crc16 have the newByte. Variable crcValue is the actual or initial value (0).
Unsigned int crc16 (unsigned int crcValue, unsigned char newByte);

/* * * * * crc16. C * * * * */

# include "crc16. H"

Unsigned int crc16 (unsigned int crcValue, unsigned char newByte)
{
Unsigned char I;

For (I=0; I & lt; 8; I++) {

If (((crcValue & amp; 0 x8000) & gt;> 8) ^ (newByte & amp; 0 x80)) {
CrcValue=https://bbs.csdn.net/topics/(crcValue <1) ^ POLYNOM;
} else {
CrcValue=https://bbs.csdn.net/topics/(crcValue <1);
}

NewByte & lt; <=1;
}

Return crcValue;
}

EXAMPLE/* * * * * * * * * */

Unsigned int exampleOfUseCRC16 (unsigned char * Data, usigned char len) {

Unsigned int CRC;
Unsigned char aux=0;

CRC=0 x0000;//Initialization of CRC to 0 x0000 for DNP
//CRC=0 XFFFF;//Initialization of CRC to 0 XFFFF for CCITT

While (aux & lt; Len) {
CRC=crc16 (CRC, Data/aux);
Aux++;
}

Return (CRC) ~;//The CRC value for DNP it is obtained by The NOT operation

//return CRC;//The CRC value for CCITT
}

CodePudding user response:

reference 1/f, panda vice secretary of party committee of the communist response:
/* * * * * crc16. H * * * * */
//*
# define CRC16_DNP 0 x3d65//DNP, IEC 870, M - BUS, wM - BUS,...
# define CRC16_CCITT 0 x1021//x. 25, v. 41, in HDLC FCS, Bluetooth,...

//Other polynoms not *
# define CRC16_IBM 0 x8005//ModBus, USB, Bisync, CRC - 16, CRC - 16 - ANSI,...
# define CRC16_T10_DIF 0 x8bb7//SCSI DIF
# define CRC16_DECT 0 x0589//Cordeless Telephones
# define CRC16_ARINC 0 xa02b//ACARS Aplications

# define POLYNOM CRC16_XXX//define the informs the POLYNOM from one of the aboves

//It calculates the new crc16 have the newByte. Variable crcValue is the actual or initial value (0).
Unsigned int crc16 (unsigned int crcValue, unsigned char newByte);

/* * * * * crc16. C * * * * */

# include "crc16. H"

Unsigned int crc16 (unsigned int crcValue, unsigned char newByte)
{
Unsigned char I;

For (I=0; I & lt; 8; I++) {

If (((crcValue & amp; 0 x8000) & gt;> 8) ^ (newByte & amp; 0 x80)) {
CrcValue=https://bbs.csdn.net/topics/(crcValue <1) ^ POLYNOM;
} else {
CrcValue=https://bbs.csdn.net/topics/(crcValue <1);
}

NewByte & lt; <=1;
}

Return crcValue;
}

EXAMPLE/* * * * * * * * * */

Unsigned int exampleOfUseCRC16 (unsigned char * Data, usigned char len) {

Unsigned int CRC;
Unsigned char aux=0;

CRC=0 x0000;//Initialization of CRC to 0 x0000 for DNP
//CRC=0 XFFFF;//Initialization of CRC to 0 XFFFF for CCITT

While (aux & lt; Len) {
CRC=crc16 (CRC, Data/aux);
Aux++;
}

Return (CRC) ~;//The CRC value for DNP it is obtained by The NOT operation

//return CRC;//The CRC value for CCITT
}

Thank you, this in MFC upper machine communication how to use this written crc16? Is obtained by the 15th and 16th byte value, the value of front side to send together to send it with?

CodePudding user response:

Before sending additional calibration can be 2 bytes
 void SendData publishes the event (const void * pSrcData, int iSrcLen) 
{
BYTE * sndData=https://bbs.csdn.net/topics/new BYTE [iSrcLen + 2];
Int iDataLen=0;
Memcpy (sndData + iDataLen srcData, iSrcLen);//copy the raw data
IDataLen +=iSrcLen;
WORD WCRC=crc16 (srcData iSrcLen);//CRC check
SndData [iDataLen + +]=HIBYTE (WCRC);//CRC high byte
SndData [iDataLen + +]=LOBYTE (WCRC);//CRC low byte

DWORD dwWritten=0;
WriteFile (hComm, sndData iDataLen, & amp; DwWritten, NULL);

The delete [] sndData;
}
  • Related