Sending and receiving data from/to PC/Arduino through serial port using QT
-
wrote on 2 Dec 2021, 15:54 last edited by
Hello,
i am new in QT and I am working on a small project to be able to send and receive the data through serial port which is connected to an Arduino uno. (I want to send "1" to arduino by push a button in QT). I have the following code as my QT code but it does not open the port. Could you please help me to find the problem.
Widget.cpp:
#include "Widget.h"
#include "ui_Widget.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QMessageBox>
#include <QDebug>
#include <QTimer>
#include <QElapsedTimer>QSerialPort serial;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);// Read Serial port Information: arduino_is_available = false; arduino_port_name = ""; arduino = new QSerialPort(); qDebug() << "Number of available ports: " << QSerialPortInfo::availablePorts().length(); foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){ qDebug() << "Has vendor ID: " << serialPortInfo.hasVendorIdentifier(); if(serialPortInfo.hasVendorIdentifier()){ qDebug() << "Vendor ID: " << serialPortInfo.vendorIdentifier(); } qDebug() << "Has Product ID: " << serialPortInfo.hasProductIdentifier(); if(serialPortInfo.hasProductIdentifier()){ qDebug() << "Product ID: " << serialPortInfo.productIdentifier(); } } foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){ if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){ if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){ if(serialPortInfo.productIdentifier() == arduino_uno_product_id){ arduino_port_name = serialPortInfo.portName(); arduino_is_available = true; } } } } arduino = new QSerialPort(this); arduino->setPortName(arduino_port_name); if(arduino->open(QIODevice::ReadWrite)) { qDebug("Serial Opened"); arduino->setBaudRate(QSerialPort::Baud115200); arduino->setDataBits(QSerialPort::Data8); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); arduino->setFlowControl(QSerialPort::NoFlowControl); } else { qDebug ("Serial NOT Opened"); } QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial()));
}
Widget::~Widget(){}
void Widget::readSerial(){
QByteArray dataSerial = arduino->readAll();
qDebug()<< dataSerial;
}void Widget::on_SendPushButton_clicked()
{
arduino->write("1");
}widget.h:
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimer>
#include <QElapsedTimer>namespace Ui
{
class Widget;
}class QSerialPort;
class Widget : public QWidget
{
Q_OBJECT
public:
QByteArray read_from_arduino();
int wite_to_arduino(QByteArray);
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void readSerial();
void on_SendPushButton_clicked();private:
Ui::Widget *ui;
QSerialPort *arduino;
static const quint16 arduino_uno_vendor_id = 9025;
static const quint16 arduino_uno_product_id = 67;
QString arduino_port_name;
bool arduino_is_available;
QElapsedTimer timer;
};#endif // WIDGET_H
main.cpp:
#include "Widget.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle("fusion");
Widget w;
w.show();
return a.exec();
} -
Hi and welcome to devnet,
Which OS are you on ?
If Linux, do you have proper rights to access the serial port ?
You should print the error you get from QSerialPort::error. -
Hi and welcome to devnet,
Which OS are you on ?
If Linux, do you have proper rights to access the serial port ?
You should print the error you get from QSerialPort::error.wrote on 5 Dec 2021, 20:23 last edited by@SGaist Hi, thanks a lot. for the moment I run my code in MAC but finally i have to do all in linux.
I think my problem is solved, I had not closed serial port in my code and when I was running for the second time, I got this error because the port was busy.Now, I want to send and receive the data as bytes, In the current code I send only the string ("1" or "0"). I think I should use QBytesarray, could you please help me to define it correctly in my code?
And also, is it possible to have an action in QT when receiving the data through serial port? for example , changing the colour of one button?
-
serialPort->write("0");
Did you already read about signals and slots in Qt ?
-
serialPort->write("0");
Did you already read about signals and slots in Qt ?
wrote on 6 Dec 2021, 21:54 last edited by@SGaist This line is what i am doing now for turning on an LED on Arduino. but I want to send an array contains 8 bytes ( command of 5 bytes, int (2 bytes), status(2 bytes) ) which is 8 bytes data array. I have the corresponding Arduino code that receives the same data frame in PC as was sent to Arduino. Many thanks in advance for any help.
-
wrote on 7 Dec 2021, 14:01 last edited by
Nobody helps? I need to write 8 bytes data in Arduino and receive the same data sent in PC, I need to read the serial and convert it to the float and print it. I would be grateful if anybody can help me.
-
Nobody helps? I need to write 8 bytes data in Arduino and receive the same data sent in PC, I need to read the serial and convert it to the float and print it. I would be grateful if anybody can help me.
@DanAm Did you take a look at example applications: https://doc.qt.io/qt-5/qtserialport-examples.html ?
-
Nobody helps? I need to write 8 bytes data in Arduino and receive the same data sent in PC, I need to read the serial and convert it to the float and print it. I would be grateful if anybody can help me.
@DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:
Nobody helps? I need to write 8 bytes data in Arduino and receive the same data sent in PC, I need to read the serial and convert it to the float and print it. I would be grateful if anybody can help me.
Please have some patience and let at least 24 hours before bumping your own thread. This is a voluntary driven forum and people might not even live in the same timezone as you.
As @jsulm suggested there are several examples in Qt's documentation to get you started with bi-directionnal serial communication.
As for the array you want to send:
@DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:
I want to send an array contains 8 bytes ( command of 5 bytes, int (2 bytes), status(2 bytes) ) which is 8 bytes data array.
That makes it 9 bytes. Also, an int is not 2 bytes, that's rather a short. So what exactly do you want to send ?
-
@DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:
Nobody helps? I need to write 8 bytes data in Arduino and receive the same data sent in PC, I need to read the serial and convert it to the float and print it. I would be grateful if anybody can help me.
Please have some patience and let at least 24 hours before bumping your own thread. This is a voluntary driven forum and people might not even live in the same timezone as you.
As @jsulm suggested there are several examples in Qt's documentation to get you started with bi-directionnal serial communication.
As for the array you want to send:
@DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:
I want to send an array contains 8 bytes ( command of 5 bytes, int (2 bytes), status(2 bytes) ) which is 8 bytes data array.
That makes it 9 bytes. Also, an int is not 2 bytes, that's rather a short. So what exactly do you want to send ?
wrote on 8 Dec 2021, 08:50 last edited by@SGaist Yes you are right.
My mistake, I want to send a command of 4 bytes and status of 2 bytes and a counter that has 2 bytes. It makes 8 bytes that I want to send and receive. And also I want to convert the received command to float in QT and display it. Can I use this way :
QByteArray command ;
command.resize(4);
command[0]=0x41;
command[1]=0xf8;
command[2]=0xf6;
command[3]=0x00;float value;
QDataStream stream(command);
stream >> value;qDebug() << value;
Thanks
-
Why not use QByteArray::toFloat ?
-
Why not use QByteArray::toFloat ?
-
@SGaist thanks, i think the problem solved.
One general question : to send through serial port, do we need to stream the byte array (QByteArray type) using QDataStream or we can write the array in the port directly ?
wrote on 9 Dec 2021, 12:07 last edited by@DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:
the problem solved
so please don't forget to mark this post as such!
-
@SGaist thanks, i think the problem solved.
One general question : to send through serial port, do we need to stream the byte array (QByteArray type) using QDataStream or we can write the array in the port directly ?
wrote on 9 Dec 2021, 12:21 last edited by@DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:
One general question : to send through serial port, do we need to stream the byte array (QByteArray type) using QDataStream or we can write the array in the port directly ?
Either, you do not have to use
QDataStream
.
1/13