serial port communication: unstable reading (SOLVED)
hi,
i am using QextSerialPort to read a string "ABCDEF01" from serial port. i have a device sends this string every half second, and the baud rate is 115200.
my serial port setting is:
Code:
void MainWindow::openSerialPort()
{
serialPort = new QextSerialPort("/dev/ttyUSB0");
if (!serialPort) {
"Could not open serial port\n"
"Please check connection.",
}
else {
serialPort->setBaudRate(BAUD115200);
serialPort->setParity(PAR_NONE);
serialPort->setFlowControl(FLOW_OFF);
serialPort->setDataBits(DATA_8);
serialPort->setStopBits(STOP_1);
ui->label_temp->setText("can not open serial port.");
else
ui->label_temp->setText("serial port opened.");
}
}
My function serReceive() reads serial port every 100m/second which generated by a Qtimer event.
Code:
void MainWindow::serReceive()
{
char recBuffer[15];
sIn = serialPort->bytesAvailable();
if (sIn > 0) {
if (sIn >= 10)
sIn = 10;
int i = serialPort->read(recBuffer, sIn);
recBuffer[i] = '\0';
serMsg = recBuffer;
ui->label_temp->setText(serMsg);
sIn = 0;
serialPort->flush();
}
}
here is my problem:
when i run my program, the program freezes in the first 10 seconds or more. then, i can see the the string i received. but, after a while, maybe 20 seconds, program freezes again, and waiting for anther 10 or more seconds back to normal.
in the debug mode, the first time program processed
Code:
int i = serialPort->read(recBuffer, sIn);
it took about 10 seconds, that's why my program froze for 10 seconds.
it looks like the buffer waiting for full charged in the 10 seconds time.
can anyone help me please? i am really appreciate your help and time.
thanks in advance.
Re: serial port communication: unstable reading
solved :o
i need to put "unbuffered" when i open the serial port.
i hope this will be helpful for anyone else :)