PDA

View Full Version : Serial Port Communication (QextSerialPort): Unstable Reading (SOLVED)



cooper
1st July 2009, 00:19
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:


void MainWindow::openSerialPort()
{
serialPort = new QextSerialPort("/dev/ttyUSB0");
if (!serialPort) {
QMessageBox::warning(this, "Warning",
"Could not open serial port\n"
"Please check connection.",
QMessageBox::Ok);
}
else {
serialPort->setBaudRate(BAUD115200);
serialPort->setParity(PAR_NONE);
serialPort->setFlowControl(FLOW_OFF);
serialPort->setDataBits(DATA_8);
serialPort->setStopBits(STOP_1);
if (!serialPort->open(QIODevice::ReadOnly))
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.


void MainWindow::serReceive()
{
serialPort->open(QIODevice::ReadOnly);
char recBuffer[15];
QString serMsg;
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

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.

cooper
1st July 2009, 01:36
solved :o

i need to put "unbuffered" when i open the serial port.


serialPort->open(QIODevice::ReadOnly | QIODevice::Unbuffered)

i hope this will be helpful for anyone else :)