Home > Net >  Complete a serial port receives packets
Complete a serial port receives packets

Time:09-24

Each great spirit:
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 end

CodePudding user response:

refer to the second floor mh20008 response:
, is the need to a judging by 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?


So-called "communication protocols", will I not not hard, the best is to experience problems,

Total is more than a dozen lines of code, and is the key to understanding, and is the initiative, not a real programmers programming, this is basically not impossible by programmers to test,

CodePudding user response:

reference 4 floor interacting in a professional developer response:
, for example, your design is signaling "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 the helpless, technology
hello, please according to the specific character or symbol is divided into multiple messages, how to execute? Cycle, specific how to implement bother guidance, thank you

CodePudding user response:

A serial port receives, I it is a time to receive a byte, and...

CodePudding user response:

refer to the eighth floor vcf_reader response:
serial port receive, my way is to receive only one bytes at a time, and...
continue to write!

CodePudding user response:

Define your own tail frame head frame data bits control these things such as the check digit is to prevent packet loss, string bag
  • Related