Qt Creator语言 - 写入串行接收数据到UI

Qt Creator - write serial received data to UI

本文关键字:数据 UI 语言 Creator Qt      更新时间:2023-10-16

我正在与qt creator一起工作,我想获得一个QByteArray到实现UI的类,让我们说类a类B实现串行连接(RS232)并以以下方式接收数据:

QSerialPort::connect(serialport,SIGNAL(readyRead()),this,SLOT(SerialPortManager::GiveReceivedDataToUI()));

因此,在我的方法SerialPortManager::GiveReceivedDataToUI(){}中,我将从我的串行连接中读取所有数据,如下所示:serialport->readAll();数据(存储在QByteArray中)仍然在b类中。现在我想将这些数据获取到a类中的文本框中。

我看到了一些关于访问来自不同类的UI元素的线程。尝试了另一个连接,但我只能弄清楚如何将我的数据连接到类a (UI)中编写文本框的方法。我想用体面的OO来写它,而不仅仅是把它公开。

有什么建议吗?

更新
class SerialPortManager : QMainWindow
{
     Q_OBJECT
private:    
    static SerialPortManager* instance;    
protected:
    SerialPortManager();
public:
    static SerialPortManager* GetInstance();
    void OpenSerialConnection();
    void CloseSerialConnection();
    void WriteSingleACLCommand(QString);
    void WriteMultipleACLCommands();
public slots:
    void GiveReceivedDataToUI();
signals:
    void Send(QByteArray& s);
};
#endif // SERIALPORTMANAGER_H

可能是这样的。这也是与使用QThread发送和接收数据SIGNAL和SLOT的类通信的唯一概念。这也是安全的,因为我们可以避免冻结。(未测试的代码)。

        class SerialPortManager : QSerialPort
        {
        Q_OBJECT
        public:
        SerialPortManager();
        private slots:
        SLOT_ReciveData()
        // here slot which will be received command from difrent class
        public slots:
        void CommandFromMainWindow(QByteArray command); 
        // here signal which will be emited if data was be received
        signals:
        void SendToMainWindow(QByteArray s); 
        };
        /******SOURCE********/
        SerialPortManager:: SerialPortManager()
        {
    // here also all setup Your serial port
        // in this connection we will be received data. This happen also in diffrent thread, because this is a part signal which was be send from main window
        QObject::connect(this , SIGNAL(readyRead()) , this , SLOT(SLOT_ReciveData()) , Qt::QueuedConnection);
}
// and slot to recived directly form Serial Port               
        void SerialPortManager::SLOT_ReciveData()
        {
                /************HERE WE GOT SOME DATA********/
        QByteArray buffer;
        while(this->bytesAvailable() > 0) // we read to time is data is available in buffer
        {
        buffer.append(this->readAll());
        }
        /******If any data is yet avaiable we send SIGNAL to mainwindow!!!!****/
        SendToMainWindow(buffer)
        }

MainWindowClass是Singelton

class MainWindow : public QMainWindow
{
private:
QThread *threadCOM;
SerialPortManager *comconfigure;         
// From this signal we something send to Serial Port for example some command
signals:
void SIGNAL_To_SerialManager_LetMeSomeData(QByteArray cmd);
// And here we something received form Serial Port
public slots:
void SLOT_Yeah_I_Get_Data(QByteArray &s);
};
MainWindow::MainWindow()
{
threadCOM = new QThread(this); // new thread for your class with serial port
comconfigure = new SerialPortManager();
comconfigure->moveToThread(threadCOM); // attachment particular thread to pinter Your class
threadCOM->start(QThread::HighestPriority); // and start, but wait for signal
// here connect signal form main windowd which send command to serial port
QObject::connect(this , SIGNAL(SIGNAL_To_SerialManager_GevMeSomeData(QByteArra);) , comconfigure , CommandFromMainWindow(QByteArray)))
// also connect signal to recived data to main window
QObject::connect(comconfigure , SIGNAL(SendToMainWindow(QByteArray)) , this, SLOT_Yeah_I_Get_Data(QByteArray)))
}