Receiving garbage data from serial port
-
I'm using serial port class for transferring data from machine to machine. Before I open the port I define the following settings:
@Port = new QSerialPort(this);
Port ->setPortName("COM16");
Port ->setBaudRate(QSerialPort::Baud115200);
Port ->setDataBits(QSerialPort::Data8);
Port ->setParity(QSerialPort::NoParity);
Port ->setFlowControl(QSerialPort::NoFlowControl);
Port ->setStopBits(QSerialPort::OneStop);
Port ->clear();@
My problem is, that sometimes when I run my app I get from the Serial port garbage data, the issue gets fixed when I open ExtraPutty tool (http://www.extraputty.com/download.php) on the same port- I get their the correct data and afterwards when I open my application everything is OK and I receive normal data. Probably the ExtraPutty defines some setting that I missed and the settings stays when I open the port from my application. can some one help me figure out what is the issue? -
Hi,
You didn't open the serial port before doing the rest
-
well, I didn't write here the line that I'm opening the port, I open the port after setting these above details.
for the case it makes sense I'm adding here the code that opens the port:@Port->open(QIODevice:: ReadWrite);
if ((Port->isOpen()))
{
Port->clear();connect(Port, SIGNAL(readyRead()), this,SLOT(DataReceived())); } else { QMessageBox::critical(this, tr("Error"), Port->errorString()); }@
-
You have to open it before setting anything
-
Also, you should to check the return from the all setXX() methods.
-
bq. what does it mean,
It means that the device isn't opened.
bq. and what should I do in order to fix it?
For the old Qt versions (is lower than 5.3.1) it is necessary: at first - try to open device, and second - try to do setXX() methods (but not vice versa!!!).
Now (for the Qt 5.3.1 and above) in it there is no need: it is possible to do setXX() before device opening.
But anyway it is necessary always to do check of return codes.