Using QSerialPort in multi widget application
-
If it's non-static, I'm getting an error saying invalid use of non-static data member 'serialObject
The program is getting crashed from the mainWindow itself
connect(tetraSerial::serialObject, SIGNAL(readyRead()), this, SLOT(serialRecived()));Can't find what's wrong with my class definition.
@viniltc You need to use an instance of tetraSerial class
-
hI
Also, im a little concerned by sharing the serial idea.
Its fine for writing to it, however, if you connect say 3 other classed to
readyRead() signal, they all get the signal but whom of them will then read the data ?
and how will you know if that class was the intended receiver of the data?It would help if you explained more what the app does and its intended design.
wrote on 22 Mar 2019, 10:58 last edited byIt is a GUI to communicate with an FTDI based serial device.
-
There will be setup stages having three windows. At the end of setup, the GUI should send a 'config file' to the serial device (say CPU -A)
-
During those setup stages, GUI should also read the serial device to get real-time updates of some sensors connected to the device (CPU-B). Also writing to some registers of the device.
-
For example, I have three windows - window1, window2, window3. The 'config file' will be sent at window 3. The user will be seeing some sensor values in window2 and updating its parameters by writing into some short registers.
All the above operations will be done through a single FTDI based serial (UART).
So what I need is to get an instance of opened serial port' in any of those windows to read or writeAny thoughts?
-
-
wrote on 22 Mar 2019, 11:29 last edited by
I try to modify the
class fileas follows:#include "tetraserial.h" tetraSerial::tetraSerial(QObject *parent) : QObject(parent) { } void tetraSerial::serialOpen() { serialObject = new QSerialPort(); serialObject->setPortName("com5"); serialObject->setBaudRate(1000000); serialObject->setDataBits(QSerialPort::Data8); serialObject->setParity(QSerialPort::NoParity); serialObject->setStopBits(QSerialPort::OneStop); serialObject->setFlowControl(QSerialPort::HardwareControl); serialObject->open(QIODevice::ReadWrite); } void tetraSerial::serialRecived2() { }and the
headeras follows:#ifndef TETRASERIAL_H #define TETRASERIAL_H #include <QObject> #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> #include <QDebug> #include <QFile> #include <QMessageBox> class tetraSerial : public QObject { Q_OBJECT public: explicit tetraSerial(QObject *parent = nullptr); // static QSerialPort *serialObject; QSerialPort *serialObject; void serialOpen() signals: public slots: void serialRecived2(); };Then I use this in the
mainas:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); tetraSerial serial; serial.serialOpen(); connect(serial, SIGNAL(readyRead()), this, SLOT(serialRecived2())); }I've got an error says:
no matching member function for call to 'connect'I don't understand why the
connectfunction can't find a matching function. -
I try to modify the
class fileas follows:#include "tetraserial.h" tetraSerial::tetraSerial(QObject *parent) : QObject(parent) { } void tetraSerial::serialOpen() { serialObject = new QSerialPort(); serialObject->setPortName("com5"); serialObject->setBaudRate(1000000); serialObject->setDataBits(QSerialPort::Data8); serialObject->setParity(QSerialPort::NoParity); serialObject->setStopBits(QSerialPort::OneStop); serialObject->setFlowControl(QSerialPort::HardwareControl); serialObject->open(QIODevice::ReadWrite); } void tetraSerial::serialRecived2() { }and the
headeras follows:#ifndef TETRASERIAL_H #define TETRASERIAL_H #include <QObject> #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> #include <QDebug> #include <QFile> #include <QMessageBox> class tetraSerial : public QObject { Q_OBJECT public: explicit tetraSerial(QObject *parent = nullptr); // static QSerialPort *serialObject; QSerialPort *serialObject; void serialOpen() signals: public slots: void serialRecived2(); };Then I use this in the
mainas:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); tetraSerial serial; serial.serialOpen(); connect(serial, SIGNAL(readyRead()), this, SLOT(serialRecived2())); }I've got an error says:
no matching member function for call to 'connect'I don't understand why the
connectfunction can't find a matching function.@viniltc What is serial in
connect(serial, SIGNAL(readyRead()), this, SLOT(serialRecived2()));?
It needs to be a pointer:connect(&serial, SIGNAL(readyRead()), this, SLOT(serialRecived2())); -
@viniltc What is serial in
connect(serial, SIGNAL(readyRead()), this, SLOT(serialRecived2()));?
It needs to be a pointer:connect(&serial, SIGNAL(readyRead()), this, SLOT(serialRecived2()));wrote on 22 Mar 2019, 15:03 last edited by viniltcI'm getting a runtime error saying
QObject::connect: No such signal tetraSerial::readyRead() in ..\serialPortTest\mainwindow.cpp:54 QObject::connect: (receiver name: 'MainWindow')I already configured the serial port in the
tetraSerialclass as shown above. What might be the cause?Also
I'm a bit confused about where to define signals and slots. As you can see I've opened the device in mytetraSerialclass in a method calledserialOpen. Now I want to communicate with for example two forms. When I pressReadbutton in form1 , the data shluld be displayed on a textbox in form 1. In form 2, when I pressWrite, it shluld write a file into the opened device.My confusion where should I define signals and slots and where should I connect them?
-
I'm getting a runtime error saying
QObject::connect: No such signal tetraSerial::readyRead() in ..\serialPortTest\mainwindow.cpp:54 QObject::connect: (receiver name: 'MainWindow')I already configured the serial port in the
tetraSerialclass as shown above. What might be the cause?Also
I'm a bit confused about where to define signals and slots. As you can see I've opened the device in mytetraSerialclass in a method calledserialOpen. Now I want to communicate with for example two forms. When I pressReadbutton in form1 , the data shluld be displayed on a textbox in form 1. In form 2, when I pressWrite, it shluld write a file into the opened device.My confusion where should I define signals and slots and where should I connect them?
My confusion where should I define signals and slots and where should I connect them?
- signals: in the sender
- slots: in the receiver
- connect: in a class that knows both other classes or in the receiver (but not in the sender!)
-
Hi
QObject::connect: No such signal tetraSerial::readyRead() in ..\serialPortTest\mainwindow.cpp:54 QObject::connect: (receiver name: 'MainWindow')
its the serialObject inside tetraSerial that has the signal so that must be used in the connect
connect(serial.serialObject, SIGNAL(readyRead()), this, SLOT(serialRecived2()));
Also, here the tetraSerial serial; is a local variable and will not survive
long enough to get any data as it will be deleted as soon as constructor ends.
it should be as a member of MainWindowtetraSerial serial; <<< im local. serial.serialOpen(); connect(seriall.serialObject, SIGNAL(readyRead()), this, SLOT(serialRecived2()));
21/27