PDA

View Full Version : read and write in serial port



neda
6th February 2016, 06:53
Hi
I'm using Qt version 5.5.1 from windows 8.1.
I create a Qt Quick Controls Application.
Port opens, but I can not read data.

main.cpp:

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtSerialPort/QtSerialPort>
#include <myserialport.h>
#include <mythread.h>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

MySerialPort();

return app.exec();
}



myserialport.cpp:


#include "myserialport.h"
#include <QtSerialPort/QSerialPort>
#include <QMessageBox>
#include <QObject>

MySerialPort::MySerialPort()
{
serial = new QSerialPort(this);
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
openSerialPort();
}


void MySerialPort::openSerialPort()
{
serial->setPortName("COM3");
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if (serial->open(QIODevice::ReadWrite)) {

showStatusMessage("Connectedd");

} else {

showStatusMessage(tr("Open error"));
}
}

void MySerialPort::closeSerialPort()
{
if (serial->isOpen())
serial->close();

showStatusMessage(tr("Disconnected"));
}

void MySerialPort::writeData(const QByteArray &data)
{
serial->write(data);
}

void MySerialPort::readData()
{
QByteArray data = serial->readAll();

qDebug() << data;

}

void MySerialPort::handleError(QSerialPort::SerialPortE rror error)
{
if (error == QSerialPort::ResourceError) {
closeSerialPort();
}
}


void MySerialPort::showStatusMessage(const QString &message)
{
qDebug() << message;
}


myserialport.h:


#include "myserialport.h"
#include <QtSerialPort/QSerialPort>
#include <QMessageBox>
#include <QObject>

MySerialPort::MySerialPort()
{
serial = new QSerialPort(this);
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
openSerialPort();
}


void MySerialPort::openSerialPort()
{
serial->setPortName("COM3");
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if (serial->open(QIODevice::ReadWrite)) {

showStatusMessage("Connectedd");

} else {

showStatusMessage(tr("Open error"));
}
}

void MySerialPort::closeSerialPort()
{
if (serial->isOpen())
serial->close();

showStatusMessage(tr("Disconnected"));
}

void MySerialPort::writeData(const QByteArray &data)
{
serial->write(data);
}

void MySerialPort::readData()
{
QByteArray data = serial->readAll();

qDebug() << data;

}

void MySerialPort::handleError(QSerialPort::SerialPortE rror error)
{
if (error == QSerialPort::ResourceError) {
closeSerialPort();
}
}


void MySerialPort::showStatusMessage(const QString &message)
{
qDebug() << message;
}


myproject.pro:

TEMPLATE = app

QT += qml quick widgets
QT += serialport

SOURCES += main.cpp \
myserialport.cpp \
mythread.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

HEADERS += \
myserialport.h \
mythread.h

anda_skoa
6th February 2016, 09:19
int main(int argc, char *argv[])
{
MySerialPort();
}


Creates and immediately destroys a temporay instance of MySerialPort

Cheers,
_

neda
6th February 2016, 09:56
Creates and immediately destroys a temporay instance of MySerialPort

Cheers,
_

Thank you for your reply.

I change my code:

main.cpp:

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtSerialPort/QtSerialPort>
#include <myserialport.h>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

//MySerialPort();
MySerialPort iSerialPort;
iSerialPort.openSerialPort();

return app.exec();
}



But now I have a error:

Error code 6

Break condition detected while reading

anda_skoa
6th February 2016, 10:27
Did you remove the openSerialPort() call from the constructor or are you calling it twice now?

Cheers,
_

neda
6th February 2016, 10:40
I remove the openSerialPort() call from the constructor.

MySerialPort::MySerialPort()
{
serial = new QSerialPort(this);


connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
SLOT(handleError(QSerialPort::SerialPortError)));

//openSerialPort();
}

anda_skoa
6th February 2016, 12:01
If I understand the information on serial break correctly, you are getting a sort of data timeout on the line.
This is a hardware thing: http://ltxfaq.custhelp.com/app/answers/detail/a_id/736/~/what-is-a-serial-break%3F

Maybe the device you are connected to can't keep up or is using different settings?

Cheers,
_

neda
6th February 2016, 12:08
If I understand the information on serial break correctly, you are getting a sort of data timeout on the line.
This is a hardware thing: http://ltxfaq.custhelp.com/app/answers/detail/a_id/736/~/what-is-a-serial-break%3F

Maybe the device you are connected to can't keep up or is using different settings?

Cheers,
_

I read data with Hercules_3-2-6 Application (rs232 terminal software) http://www.qtcentre.org/threads/65107-read-data-from-serial-port-just-after-open-by-another-program

neda
7th February 2016, 06:12
My program worked today, and I have not any error.
But I do not any change to program.

When I run program,
program connects to port successfully, but I receive just one data (space).

But when I close this program and open Hercules_3-2-6 Application (rs232 terminal software), that application read data,
and after close Hercules_3-2-6 application and open terminal example again, this program works and reads data until restarting computer.

I repeat this process many times.

But my project does not receive data after restarting system until port opens one time by Hercules_3-2-6 Application.

Meanwhile I have virtual USB COM port and my hardware connected to computer with USB.

neda
8th February 2016, 08:46
I wrote a .net program.It worked very well likes to Hercules_3-2-6 Application and qt program worked after opening the program. :confused:
Is it bug of Qt 5.5.1 ? :( :(

neda
10th February 2016, 06:47
I added this line for handle error port.

voidMySerialPort::handleError(QSerialPort::SerialP ortErrorerror)
{
qDebug()<<"Error:"<<error;//I added this line
if(error==QSerialPort::ResourceError)
{
showStatusMessage(serial->errorString());
closeSerialPort();
}
}

Result of run program:

Error: 0
"Connectedd"
Error: 6
"\x00"


Just sometimes I have error 6.
How do I fix break condition error?

kuzulis
10th February 2016, 07:41
> How do I fix break condition error?

Nohow. It is HW/driver issue.

UPD: Future Qt 5.6.0 won't be process the Parity/Frame and BreakCondition errors anymore. So, you can try QtSeriaslPort from 5.6.0 branch.

neda
10th February 2016, 08:54
> How do I fix break condition error?

Nohow. It is HW/driver issue.

UPD: Future Qt 5.6.0 won't be process the Parity/Frame and BreakCondition errors anymore. So, you can try QtSeriaslPort from 5.6.0 branch.

Of course,My program is problem because other programs (C# .net , Hercules) work very well.
My problem is bug of QSerialPort? :( :(

kuzulis
10th February 2016, 10:18
> Of course,My program is problem because other programs (C# .net , Hercules) work very well.

So what? Maybe they (those programs) do not trace the EV_ERR event flag. This flag coming from the device driver, and QtSerialPort < 5.6 handles this error when it is occurred.

> My problem is bug of QSerialPort?

You problem is bug of HW/driver of device.

neda
10th February 2016, 12:07
Thanks you very much.
Your answer was a big help for me.

serial->setDataTerminalReady(false);