read and write in serial port
-
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.
I have a error:
"Break condition detected while reading" (Error code 6)
Please guide me.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(); MySerialPort iSerialPort; iSerialPort.openSerialPort(); 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::SerialPortError error) { if (error == QSerialPort::ResourceError) { closeSerialPort(); } } void MySerialPort::showStatusMessage(const QString &message) { qDebug() << message; }
myserialport.h:
#ifndef MYSERIALPORT_H #define MYSERIALPORT_H #include <QtSerialPort/QtSerialPort> #include <QObject> class MySerialPort: public QSerialPort { Q_OBJECT public: MySerialPort(); public slots: void openSerialPort(); void closeSerialPort(); void writeData(const QByteArray &data); void readData(); void handleError(QSerialPort::SerialPortError error); private: void showStatusMessage(const QString &message); QSerialPort *serial; }; #endif // MYSERIALPORT_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
-
Hi,
Do you have any other application also accessing that serial port ?
-
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 my application 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.
-
Are you using the same serial port setup in both your application and Hercules_3-2-6 ?
-
yes, I using the same serial port setup in both my application and Hercules_3-2-6 .
Specification of port: Name: COM3, Baud Rate: 9600, Data bits: 8, Parity: None, Stop bits: 1, Flow control: None
My program:
void MySerialPort::openSerialPort() { serial->setPortName("COM3"); if (serial->open(QIODevice::ReadWrite)) { serial->setParity(QSerialPort::NoParity); serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); showStatusMessage("Connectedd"); } else { showStatusMessage(tr("Open error")); } }
Meanwhile I have the same problem in qtserialport terminal example.
When I run qtserialport terminal example, program connects to port successfully, but does not receive any data.
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. -
Does the device need DTR ?
-
No,
I wrote a C# .net program. It worked very well likes to Hercules_3-2-6 Application and qt program worked after opening the program.
I added this line for handle error port.void MySerialPort::handleError(QSerialPort::SerialPortError error) { 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? -
@SGaist
Thank you very much my dear.
Your answer was a big help for me.My problem was solved by set "setDataTerminalReady" function to false.
I did not have information about the signal DTR, by pointing you I read about that and find my problem. -