Home > Software engineering >  MFC serial communication data missing/incomplete
MFC serial communication data missing/incomplete

Time:10-01

MSCOOm control PC program, used to equipment, serial communication to send instructions in the past, equipment response instructions, I will divide the two will receive complete, (occasional), is there any way to avoid, or the disadvantages of serial port communication is like this?
 
Void CConnectTest2Dlg: : OnCommMscomm1 ()//serial port control program
{
//TODO: add message handler code here
//status bar content
If (m_mscom get_CommEvent ()==2)
{
//ComThreadInfo * comInfo=new ComThreadInfo;
Char STR [1024]={0};
Cstrings strtemp;
Byte reArray [1024].
Long k, len;
The VARIANT InputData=https://bbs.csdn.net/topics/m_mscom.get_Input ();//read buffer
COleSafeArray fs.
Fs=InputData;
Len=fs. GetOneDimSize ();//the VARIANT type variable converting a quantity COleSafeArray type variable
For (k=0; KFs. GetElement (& amp; K, reArray + k);//type into a BYTE array

//-- -- -- -- -- -- -- -- turn hexadecimal ASCII -- -- -- -- -- -//
Char reBuf [1024].
Int j=0;
Char temp.
for(int i=0; I{
Temp=(reArray [I] & amp; 0 xf0) & gt;> 4.
ReBuf [j]=(temp<=9)? Temp + '0' (temp - 10) + 'A';
j++;
Temp=(reArray [I] & amp; 0 x0f);
ReBuf [j]=(temp<=9)? Temp + '0' (temp - 10) + 'A';
j++;
} reBuf [j]='\ 0';
//return reBuf;
M_EditRecevie2 +=reBuf;
}




Want to above, the first two lines is received only twice, the third line is complete reply the correct instruction, I am to send and receive a few times more complete,
Novice, students, every brother please answer, thank!

CodePudding user response:

In asynchronous mode is such, own put in buffer, divided into data frame according to the agreement

CodePudding user response:

A serial port is streaming devices, the need to buffer data parsing

CodePudding user response:

I use the official serial communication software, to software unicom, and I won't appear the case that a less, but with the device to connect, is easy to appear, only a piece of content, but the equipment and the serial communication software testing is no problem (possible use serial communication method is not the same)

CodePudding user response:

CreateFile serial port to open the time (), read the data when using ReadFile () to read, do not, because you can use first ClearCommError () to check the data buffer to read all of the data and ReadFile specify read length; If you are willing to change, speaking, reading and writing scheme I can give you a reference for the demo

CodePudding user response:

With buffer array and receive all well data, and then to deal with according to the format

CodePudding user response:

One-time charge up is normal, the oneself to deal with,
After sending instructions, it's best to do a time delay, and then receive,

CodePudding user response:

Receive fast, leading to receive incomplete,
I every time it is read only one byte, and then processing,

CodePudding user response:

Very classic streaming receiving data problems,

Problems that all the flow of communication, data truncation,
May, for example, 123 is the correct data for the first time only received 1, received 2 second, third received 3. May also be a one-off received 123.
After receiving the data, data splicing, validated the validity is decided to continue to receive data or submit directly,

CodePudding user response:

Received in the buffer, according to the protocol parsing into a data frame

CodePudding user response:

Use serial assistants try whether the underlying slow reply to you

CodePudding user response:

Fyi:
//loop to a function every time send 200 bytes (this is the fixed) buffer, 
//a function need to incoming buffer, the circulation of 240 bytes (fixed) new buffer for processing,
//when dealing with each new printing takes two bytes of the buffer
# ifdef _MSC_VER
# pragma warning (4996) disable:
# endif
#include
#include
#include
# ifdef _MSC_VER
#include
#include
#include
# define MYVOID void
# define vsnprintf _vsnprintf
# the else
#include
#include
#include
# define CRITICAL_SECTION pthread_mutex_t
# define MYVOID void *
# endif
//the Log {
# define MAXLOGSIZE 20000000
# define MAXLINSIZE 16000
#include
#include
#include
Char logfilename1 []="MyLog1. Log";
Char logfilename2 []="MyLog2. Log";
The static char logstr [MAXLINSIZE + 1];
Char datestr [16].
Char timestr [16].
Char MSS [4].
CRITICAL_SECTION cs_log;
The FILE * flog;
# ifdef _MSC_VER
The Lock (CRITICAL_SECTION void * l) {
The EnterCriticalSection (l);
}
Void Unlock (CRITICAL_SECTION * l) {
LeaveCriticalSection (l);
}
Void sleep_ms (int) ms {
Sleep (ms).
}
# the else
The Lock (CRITICAL_SECTION void * l) {
Pthread_mutex_lock (l);
}
Void Unlock (CRITICAL_SECTION * l) {
Pthread_mutex_unlock (l);
}
Void sleep_ms (int) ms {
Usleep (ms * 1000);
}
# endif
Void LogV (const char * pszFmt, va_list argp) {
* struct tm now;
Struct timeb TB;

If (NULL==pszFmt | | 0==pszFmt [0]) return;
Vsnprintf (logstr, MAXLINSIZE pszFmt, argp);
Ftime (& amp; TB);
Now=localtime (& amp; TB. Time);
Sprintf (datestr, "% d % % 4 d - 02-02 d", now - & gt; Tm_year + 1900, now - & gt; Tm_mon + 1, now - & gt; Tm_mday);
Sprintf (timestr, "02 02 02 % d: % d: % d", now - & gt; Tm_hour, now - & gt; Tm_min, now - & gt; Tm_sec);
Sprintf (MSS, "% 3 d", TB. Millitm);
Printf (" % s % s. % s % s ", datestr, timestr, MSS, logstr);
Flog=fopen (logfilename1, "a");
If (NULL! Flog)={
Fprintf (flog, "% s % s. % s % s", datestr, timestr, MSS, logstr);
If (ftell (flog) & gt; MAXLOGSIZE) {
The fclose (flog);
If (rename (logfilename1 logfilename2)) {
Remove (logfilename2);
Rename (logfilename1 logfilename2);
}
} else {
The fclose (flog);
}
}
}
Void the Log (const char * pszFmt,... ) {
Va_list argp;

The Lock (& amp; Cs_log);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related