Qt Creator - write serial received data to UI

时间:2015-10-06 08:32:35

标签: c++ qt user-interface serial-port

I'm working with qt creator and I want to get a QByteArray to the class that implements to UI, lets say class A. Class B implements a serial connection (RS232) and receives data in the following way:

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

So in my method SerialPortManager::GiveReceivedDataToUI(){} I will read all the data from my serial connection like this: serialport->readAll(); The data (stored in a QByteArray) is still in Class B. Now I want to get this data to a textbox in Class A.

I red some threads about accessing UI elements from a different class. Tried it with another connect, but I juist could figure out how to connect my data to a method in Class A(UI) that writes a textbox. I want to write it in decent OO, not just by making this public.

Any suggestions?

UPDATE

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

1 个答案:

答案 0 :(得分:0)

可能就是这样。这也是与类通信的概念,用于使用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)))

}