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[])
{
QQmlApplicationEngine engine;
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
MySerialPort();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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);
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()
{
qDebug() << data;
}
void MySerialPort::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
closeSerialPort();
}
}
void MySerialPort
::showStatusMessage(const QString &message
) {
qDebug() << message;
}
#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::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
closeSerialPort();
}
}
void MySerialPort::showStatusMessage(const QString &message)
{
qDebug() << message;
}
To copy to clipboard, switch view to plain text mode
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);
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()
{
qDebug() << data;
}
void MySerialPort::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
closeSerialPort();
}
}
void MySerialPort
::showStatusMessage(const QString &message
) {
qDebug() << message;
}
#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::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
closeSerialPort();
}
}
void MySerialPort::showStatusMessage(const QString &message)
{
qDebug() << message;
}
To copy to clipboard, switch view to plain text mode
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
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
To copy to clipboard, switch view to plain text mode
Bookmarks