PDA

View Full Version : can't set serial port data bits



yyiu002
23rd June 2010, 05:18
Hi everyone,

I had bit problem on setting up serial port parameters.


port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->setRts(true);
port->setDtr(true);
port->setTimeout(500);

if (port->open(QIODevice::ReadWrite))
{
qDebug()<<"Port Info";
qDebug()<<"---------";
qDebug()<<port->baudRate();
qDebug()<<port->dataBits();
qDebug()<<port->portName();
qDebug()<<"---------";
}


What I get back from dataBits() funtion is 3, not 8, what's wrong?

yyiu002
23rd June 2010, 05:28
DATA_8 might be 3, silly me.

But my problem is if port was set correctly, why can't it receive correct data?
After I use VB application initialized the serial port, send out data correctly, then went back to this QT application, I can send and receive correctly.

But if not using this VB application refresh the port, QT App won't run properly, has anyone met with problem before?

kuzulis
23rd June 2010, 05:41
yyiu002,

Can you advise to use the library QSerialDevice. As an alternative QextSerialPort:

http://qt-apps.org/content/show.php/QSerialDevice?content=112039

But version 0.2.0 is already out of date. The latest version is on the SVN here: fireforge.net

But the site fireforge.net are no longer working :(.
I will try to create a repository QSerialDevice today at: http://gitorious.org/

Lesiok
23rd June 2010, 08:07
DataBitsType retiurned by dataBits() is an enum. And 3 is DATA_8.
Are You sure that all parameters are correct, ie. parity, stop bits.

msrihari
23rd June 2010, 08:38
are you using rts and dtr signals, if not set them false and make a try.

yyiu002
23rd June 2010, 21:38
I also tried with qextserialport 's example project, event folder, it gives me the same thing, not working!


QextSerialPort * port = new QextSerialPort("COM8", QextSerialPort::EventDriven);
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->open(QIODevice::ReadWrite);
if (!(port->lineStatus() & LS_DSR)) {
out << "warning: device is not turned on" << endl;
}

yyiu002
23rd June 2010, 22:28
It works for me now, have to open the serial port first, then set parameters.



port = new QextSerialPort(comPort, QextSerialPort::EventDriven);
port->open(QIODevice::ReadWrite);

// qDebug()<<baudSel;

port->setBaudRate(baudSel);
port->setDataBits(DATA_8);
port->setParity(PAR_NONE);
port->setStopBits(STOP_1);
port->setFlowControl(FLOW_OFF);
port->setTimeout(500);
port->setRts(true);
port->setDtr(true);



QextSerialPort seems work fine.

will try QSerialPort later if having other problems and can't be resolved.

Many thanks on everyone on helping me on this.