1.VCä¸ç¨CSerialPortç±»è¿è¡ä¸²å£ç¼ç¨
VCä¸ç¨CSerialPortç±»è¿è¡ä¸²å£ç¼ç¨
serialPort.Readè¿ä¸ªå½æ°ç¨ä¸èµ·?串口串口vapy源码下载è¿æ¯ä¸èµ·ä½ç¨å¸§å
å®ä¹ä¸ä¸ª BYTE buffer[1++1]; ä¹å°±æ¯ buffer[];
åå®ä¹ä¸ä¸ªint bufferLength = 0;
OnCommunication(...)ä¸{
if (0 == bufferLength) // 帧头è¿æªæ¥æ¶
{
if (0xAF != ch) // ä¸æ¯å¸§å¤´
return; // è¿å
}
if ( == bufferLength) // 帧尾è¿æªæ¥æ¶
{
if (0xFA != ch) // ä¸æ¯å¸§å°¾
{
bufferLength = 0; // 丢å¼å·²ç»æ¥æ¶çæ°æ®ï¼éç½®ç¼å²åºæææ°æ®é¿åº¦ä¸ºé¶
return; // è¿å
}
}
// è¦å®æ¶æ¾ç¤ºæ¥æ¶å°ç ch ï¼å¨è¿éæ·»å 代ç å³å¯
buffer[bufferLength] = ch;
bufferLength++;
if ( == bufferLength)
{
// ä¸å¸§å·²ç»æ¥æ¶å®æ¯ï¼æ°æ®å¨ buffer ä¸
bufferLength = 0; // ååºå¸§åï¼éç½®ç¼å²åºæææ°æ®é¿åº¦ä¸ºé¶
}
}