Qt serial communication using UART
-
i want to connect qt and device using UART cable (RS232C) in linux. so i writing code and making ui and operating ,but it does not work.
i want to connecting when i click some button(ui) device turn on and connect. also i want to make a function that if i enter some command device cognize and execute.
below is my code , someone help me please.
<mainwindow.cpp> #include "mainwindow.h" #include "ui_mainwindow.h" #include <QtSerialPort/QSerialPort> #include <QMessageBox> #include <QObject> #include <QIODevice> #include <QDebug> QSerialPort serial; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { QSerialPort*port=new QSerialPort(); port->setPortName("/dev/ttyUSB0"); port->setBaudRate(QSerialPort::Baud19200); port->setDataBits(QSerialPort::Data8); port->setParity(QSerialPort::NoParity); port->setStopBits(QSerialPort::OneStop); port->setFlowControl(QSerialPort::NoFlowControl); port->open(QIODevice::ReadWrite); ui->setupUi(this); serial = new QSerialPort(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_connect_clicked() { port=new QSerialPort(); QObject::connect(port,SIGNAL(readyRead()),this, SLOT(on_pushButton_connect_clicked())); if(!port->open(QIODevice::ReadWrite)){ QMessageBox::information(this, tr("connect"), "serialcommunication start"); } else { QMessageBox::critical(this, tr("fail"), serial- >errorString()); } } void MainWindow::on_pushButton_disconnect_clicked() { port->close(); QMessageBox::information(this, tr("disconnect"), "serial communication end"); }
<mainwindow.h> #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QtSerialPort/QSerialPort> #include <QMessageBox> #include <QIODevice> #include <QDebug> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); QSerialPort*serial; //plus QSerialPort*port; QWidget*main_widget; void readData(); ~MainWindow(); private slots: void on_pushButton_connect_clicked(); void on_pushButton_disconnect_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
<main.cpp> #include "mainwindow.h" #include <QApplication> #include <QSerialPort> #include <QSerialPortInfo> #include <QDebug> #include <QMessageBox> #include <QIODevice> int main(int argc, char *argv[]) { QApplication a(argc, argv); foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()){ QSerialPort serial; serial.setPort(info); if (serial.open(QIODevice::ReadWrite)) serial.close(); } MainWindow w; w.show(); return a.exec(); }
-
You set up the port config in the constructor, but then you don't use it at all, in your button click slot you create a new QSerialPort object (without deleting the one from constructor - that's a memory leak).
I suggest you remove the
port = new QSerialPort()
from your button click, and use the single object you create in the constructor. In response to button clicks it should be enough to callport->open()
andport->close()
.Also, you don't need the
serial
variable at all. -
@segtteee said in Qt serial communication using UART:
additionaly to what @sierdzio said,
change your constructor fromQSerialPort*port=new QSerialPort();
to
port = new QSerialPort();
otherwise you config a new Variable instead of your member variable.
-
Thank you for your reply. I delete "port=new QSerialPort();" and fix from "QSerialPort*port=new QSerialPort();" to "port = new QSerialPort();"
, insert "port->open(QIODevice::ReadWrite);"
but how can i remote device when i enter the command at line_Edit . ( like echo ____ > /dev/ttyUSB0 in linux terminal )
i want to remote device using qt ui line Edit and button .
please let me know TT
-
@segtteee I'm not entirely what you want to do, but I'll take quick look at my crystal ball and write something down:
To send something via your SieralPort you can do something like this:
void onSendClicked(){ QString s("String to Send via Serial"); QByteArray dataToSend; dataToSend.append(s); if(->isOpen()) serialPort->write(dataToSend); }
-
Thank you for your reply. but i can't understand that you say.
how can i apply "QByteArray data = port.read();" , "port.write("sth");" in my code?
i think // void MainWindow::lineEdit_command (){ // is that right?
QByteArray data = port.read();
port.write("sth"); -
@segtteee said in Qt serial communication using UART:
Thank you for your reply. but i can't understand that you say.
Please take a look at some Qt examples to better understand how it works. My code snippets are only an example how to use the QSerialPort, they have little to do with your actual use case.
So, if I get it right, you want to take the command which is written (by user) into a QLineEdit, and (when user clicks a button) send it via the serial port?
Then:
- connect the
clicked()
signal of the button to a slot you create in your main window class. In your case, that could bevoid on_pushButton_connect_clicked();
slot, or something else if you want to add another button - in the slot, open the port
- in the slot, write line edit data into your port
- if needed, add some sanity checking/ validation to the input data. Otherwise users will surely put in some garbage there
So, in essence, you need something like:
void MainWindow::on_someButton_clicked() { port->open(QSerialPort::ReadWrite); port->write(ui->yourLineEditName->text().toLatin1()); }
- connect the
-
@J.Hilk
Thank you for your reply.you mean if i want use several command , i have to enter code that you tell me one by one?
otherwise " QString s("String to Send via Serial"); " can understand any commands ? "String to Send via Serial" what mean?
-
Please read any C++ book at first.
-
Thaky you for your reply.
i add
//void MainWindow::on_pushButton_send_clicked()
{
port->open(QSerialPort::ReadWrite);
port->write(ui->lineEdit_command->text().toLatin1());}// in my code and enter the command at lineEdit , but device can't conduct command..
Is there something else i have to anything for device's behavior?
-
@segtteee said in Qt serial communication using UART:
in my code and enter the command at lineEdit , but device can't conduct command..
Well, I can't guess what error codes are you getting, right? I don't even know if you connected to the device correctly. Did it get the command? Did it reply with anything? Have you connected the readyRead() to some slot, or used waitForReadyRead() to wait for the reply?
I don't give you whole solution for your problem, I'm not here to write the code for you. I'm happy to help you overcome problems and learn.
-
okay thank you for your reply .
i have question , how to check connected device with qt ? only i enter the command and device conduct command?
when i debuging the code there is no error message and no problem i think . hot to check in qt ?
-
@segtteee: I can only recommend you to have a look at the examples (there are a lot of them):
http://doc.qt.io/qt-5/qtserialport-examples.html
Take, e.g. the Terminal example http://doc.qt.io/qt-5/qtserialport-terminal-example.html and try to understand how it works. It should be very near to what you want to achive.
PS: This example also shows how to check for some errors. Depending on the communication protocol, you may have to add more error checks.
-
@segtteee said in Qt serial communication using UART:
i have question , how to check connected device with qt ? only i enter the command and device conduct command?
First step is to check if
port->open()
returnedtrue
.
When callingport->write()
you can check if the amount of bytes written is the same you sent. If not, something went wrong. Then you can check QSerialPort::error and see what was the problem.Another clue might be whether
readyRead()
is emitted in response to your command. If there is no reply, then again - something might be wrong (or not - that depends on what the command is and which device you connect to). -
Hi
For a pretty good example, please see
http://doc.qt.io/qt-5/qtserialport-terminal-example.html
It has dialog to select comport properties and allows to send text out of the box.
Its also pretty good explained.~