Platform: VS2013, MFC
I see you on the net a c + + serial port programming examples, link http://blog.csdn.net/jiangqin115/article/details/40391289
Join the project using the test after receiving phenomenon, there is no stable specific descriptions are as follows:
1, use the serial port: 9600 baud, 1 start bit, 1 stop bit, no partities;
2, send is normal, but when receiving, the occasional unpacking,
Const UINT SLEEP_TIME_INTERVAL=5;
/* * set the timeout of serial ports, are set to 0, said don't use timeouts */
COMMTIMEOUTS COMMTIMEOUTS;
CommTimeouts. ReadIntervalTimeout=0;
CommTimeouts. ReadTotalTimeoutMultiplier=0;
CommTimeouts. ReadTotalTimeoutConstant=0;
CommTimeouts. WriteTotalTimeoutMultiplier=0;
CommTimeouts. WriteTotalTimeoutConstant=0;
//thread loop, polling way to read serial data
while (! PSerialPort - & gt; S_bExit)
{
UINT BytesInQue=pSerialPort - & gt; GetBytesInCOM ();
/* * if countless according to a serial port in the input buffer, then rest for a while and then query */
If (BytesInQue==0)
{
Sleep (SLEEP_TIME_INTERVAL);
continue;
}
BResult=ReadFile (m_hComm, PTR, BytesInQue, & amp; BytesRead, NULL);
}
30 bytes actually have hair, for example, but GetBytesInCOM () only apply to 8 bytes returned, the remaining 22 bytes to return next time, turn a packet into two return;
When the SLEEP_TIME_INTERVAL spacing increased from 5 ms to 100 ms, it can't take the probability at a time will become smaller, but still can appear,
I use a virtual serial port is no problem, very stable, is it the problem of hardware interrupt?
How to ensure that each receive a complete packet?
Thank you very much!
Online etc.!
CodePudding user response:
Can't guarantee that...If can guarantee. The "agreement" anything else?
Head to tail and check what's the meaning of these things exist?
So in guarantee under the condition of "full" package. Must pass the so-called "agreement" to assemble a complete package.
CodePudding user response:
Judging whether certain need to according to the communication protocol, to spell or demolition, to form a complete packet?If so, what is the good strategy to complete the received packet processing?
CodePudding user response:
Should use protocol does not guarantee, virtual serial port, is pure usb implementation, and hair have cache, caused the effect is not consistent,CodePudding user response:
For example your signal design is "every news \ r" end, so when you receive a message, the last byte not \ n cannot message parsing (should be cached), and hypothesis is \ n to cache all the bytes of the according to the \ n split into multiple messages, and then one by one analytical processing,Really qualified communication program, don't write any Sleep, wrote it is pit yourself, because you never know how much Sleep milliseconds, and the deliberately slows communication program a see be acceptable, hard is technically helpless,
CodePudding user response:
For example your signal design is "every news \ r" end - & gt; For example your signal design is "every news with \ n" to an endCodePudding user response: