Such as: 0 x00 0 x80 xf2 x1e 0 0 0 x00 0 x80 x15 0 0 x32
Here are the two data frame, four byte as a data frame, in the agreement without any validation, frame head and tail frame, just continue to send four bytes of data, on my PC with serial debugging assistant test to a data read no error has occurred,
If under Linux serial port to read data, then it will appear this kind of circumstance
Such as: 0 x00 0 x80 xf2 x1e 0 0 0 x00 0 x00 0 x80 x15 0 0 x32
More is between the two data frames a byte, because there is no frame head frame tail judgement, on the program can't judge which one is illegal byte, only through once every four bytes from the data, so they all lead to the later data disorder,
Question is why read application will appear this kind of mistake,
A serial port initialization code below
/*
Initialize the serial port, the configuration parameters of the serial port,
Parameters: the BaudRate: baud rate
DataBits:
the data bitsStopBits: stop bit
ParityBit:)
*/
Bool SerialPort: : InitSerialPort (int BaudRate, int DataBits, int StopBits, int ParityBit)
{
If (1==fd)
return false;
If (0!=tcgetattr (fd, & amp; M_Setting))
{
Printf (" InitSerialPort tcgetattr () line: % d failed \ n ", __LINE__);
return false;
}
//set the baud rate
For (int I=0; i{
If (BaudRate==m_SpeedArr [I])
{
Tcflush (fd, TCIOFLUSH);//to empty to send and receive buffer
Cfsetispeed (& amp; M_Setting, m_BaudRateArr [I]);//set the input baud rate
Cfsetospeed (& amp; M_Setting, m_BaudRateArr [I]);//set output baud rate
break;
}
If (I==sizeof (m_SpeedArr)/sizeof (int))
return false;
}
M_Setting. C_cflag |=CLOCAL;//control mode to ensure that the program does not become a possession of the port
M_Setting. C_cflag |=CREAD;//control mode, can make port read input data
//set the data bits
M_Setting. C_cflag & amp;=~ CSIZE;
The switch (DataBits)
{
Case 6: m_Setting c_cflag |=CS6; break;//6 bits
Case 7: m_Setting c_cflag |=CS7; break;//seven bits
Case 8: m_Setting c_cflag |=CS8 gives; break;//8 bits of data a
Default:
Fprintf (stderr, "unsupported dataBits \ n");
return false;
}
//set the stop bit
The switch (StopBits)
{
Case 1: m_Setting c_cflag & amp;=~ CSTOPB; break;//one stop bit
Case 2: m_Setting c_cflag |=CSTOPB; break;//two stop bits
Default:
return false;
}
//set the parity bit
The switch (ParityBit)
{
Case 'n' :
Case 'N' :
M_Setting. C_cflag & amp;=~ PARENB;//close the check digit that can mark PARENB c_cflag)
M_Setting. C_iflag & amp;=~ INPCK;//close the input parity check
break;
Case: 'o'
Case: 'O'
M_Setting. C_cflag |=(PARODD | PARENB);//activate the check digit that can mark PARENB c_cflag, simultaneously odd parity
M_Setting. C_iflag |=INPCK;//open input parity check
break;
Case 'e' :
Case 'E' :
M_Setting. C_cflag |=PARENB;//activate the check digit that can mark c_cflag PARENB
M_Setting. C_cflag & amp;=~ PARODD;//using parity
M_Setting. C_iflag |=INPCK;//open input parity check
break;
In case the 's' :
In case the 'S' :
M_Setting. C_cflag & amp;=~ PARENB;//close the check digit that can mark PARENB c_cflag)
M_Setting. C_cflag & amp;=~ CSTOPB;//set the stop bit bit a
break;
Default:
Fprintf (stderr, "unsupported parityBit \ n");
return false;
}
M_Setting. C_oflag & amp;=~ OPOST;//set to the original output mode
M_Setting. C_lflag & amp;=~ (ICANON | ECHO | ECHOE | ISIG);//set to the original input mode
M_Setting. C_cc [VTIME]=1;
M_Setting. C_cc [VMIN]=1;
M_Setting. C_iflag & amp;=~ (BRKINT | ICRNL | INPCK | ISTRIP | IXON);
Tcflush (fd, TCIFLUSH);
//active configuration
If (0!=tcsetattr (fd, TCSANOW, & amp; M_Setting))
{
Printf (" InitSerialPort tecsetattr failed () % d \ n ", __LINE__);
return false;
}
return true;
}
CodePudding user response:
More than two data frame that a byte sometimes appear, won't appear sometimes, sometimes there will be two, don't know is it read more data or data loss