Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to open more than once SerialPort
Qt 6.12 LTS is out! See what's new in the release blog

How to open more than once SerialPort

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 5 Posters 23.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #14

    Oh u posted while i was writing

    Can you use the row index directly to the V list ?

    1 Reply Last reply
    0
    • E Offline
      E Offline
      erytcg
      wrote on last edited by
      #15

      yes I want to add opened COMports to ListWidget and close selected ports

      mrjjM 1 Reply Last reply
      0
      • E erytcg

        yes I want to add opened COMports to ListWidget and close selected ports

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #16

        @erytcg

        If there is a 1 to 1 to the V list ( please rename to comports or something :)

        Then you can just use the
        void MainWindow::closeSerialPort( int index)
        {
        QSerialPort * serialPort = v[index];
        ...

        and call it from
        foreach(QListWidgetItem * item, items) {
        int index= ui->listWidget->row(item); //
        closeSerialPort( index);

        1 Reply Last reply
        1
        • E Offline
          E Offline
          erytcg
          wrote on last edited by
          #17

          Hey can you look at code? Now it's ok? :)

          void MainWindow::addAvailableSerialPorts()
          {
              QVector<QSerialPort *> v;
              Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts())
                  {
                     QSerialPort* serialPort = new QSerialPort();
                     serialPort->setPort(port);
                     serialPort->setBaudRate(QSerialPort::Baud9600);
                     serialPort->setDataBits(QSerialPort::Data8);
                     serialPort->setParity(QSerialPort::NoParity);
                     serialPort->setStopBits(QSerialPort::OneStop);
                     serialPort->setFlowControl(QSerialPort::NoFlowControl);
                     v.append(serialPort);
                  }
          }
          void MainWindow::openSerialPort(int index)
          {         
              QSerialPort * serialPort = v[index];
              if (serialPort->open(QIODevice::ReadWrite)) 
               {
                   showStatusMessage(tr("Connected to %1 : OK").arg(p.name));
               } 
              else 
              {
                   QMessageBox::critical(this, tr("Error"), serialPort->errorString());
                   showStatusMessage(tr("Open error"));
              }
          }
          
          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #18

            @erytcg said in How to open more than once SerialPort:

            Almost but one fatal error !

            void MainWindow::addAvailableSerialPorts()
            {
            QVector<QSerialPort *> v; <<<< This is a local var. It should be in class. windows .h. should not be defined here. only used.

            So in
            void MainWindow::openSerialPort(int index)
            {
            QSerialPort * serialPort = v[index]; <<< what v is this . it cannot be the one from addAvailableSerialPorts as that was local.

            1 Reply Last reply
            1
            • E Offline
              E Offline
              erytcg
              wrote on last edited by
              #19

              ok I add *QVector<QSerialPort > v; to private: mainwindow.h
              I have problem with close function port chooseing from list widget

              mrjjM 1 Reply Last reply
              0
              • E erytcg

                ok I add *QVector<QSerialPort > v; to private: mainwindow.h
                I have problem with close function port chooseing from list widget

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #20

                @erytcg

                you need to use the row int as index

                foreach(QListWidgetItem * item, items) {
                int index= ui->listWidget->row(item); //
                closeSerialPort( index);
                
                1 Reply Last reply
                1
                • E Offline
                  E Offline
                  erytcg
                  wrote on last edited by
                  #21

                  Hey it's good idea to recall to function

                  void MainWindow::on_connectAction_clicked()
                  {
                      openSerialPort(ui->serialPortInfoListBox->currentIndex());
                  }
                  

                  I think it's not MVC

                  mrjjM 1 Reply Last reply
                  0
                  • E erytcg

                    Hey it's good idea to recall to function

                    void MainWindow::on_connectAction_clicked()
                    {
                        openSerialPort(ui->serialPortInfoListBox->currentIndex());
                    }
                    

                    I think it's not MVC

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #22

                    @erytcg
                    Hi
                    Im not sure what you talk about.
                    MVC= model, view , controller?

                    Is there a question ?

                    What you mean recall ?
                    Do you open it in addAvailableSerialPorts ?

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      erytcg
                      wrote on last edited by
                      #23

                      Yes I talk about model, view , controller.
                      I add all avaibleports to vector in addAvailableSerialPorts ()
                      i open in openSerialPort(int index) / index=QComboBox->currentIndex();
                      and close in closeSerialPort(int index) / index=QListWidget->currentRow()+1;

                      and all works fine I think, but if I want show received data on QCustomPlot widget.
                      I don't know how to convert QByteArray to QVector<double>.
                      My received data from qDebug() - "\x0B"
                      Thanks a lot for help :)

                      mrjjM 1 Reply Last reply
                      0
                      • E erytcg

                        Yes I talk about model, view , controller.
                        I add all avaibleports to vector in addAvailableSerialPorts ()
                        i open in openSerialPort(int index) / index=QComboBox->currentIndex();
                        and close in closeSerialPort(int index) / index=QListWidget->currentRow()+1;

                        and all works fine I think, but if I want show received data on QCustomPlot widget.
                        I don't know how to convert QByteArray to QVector<double>.
                        My received data from qDebug() - "\x0B"
                        Thanks a lot for help :)

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #24

                        @erytcg
                        Nope a list of Class * is not really MVC.

                        Do you really need to convert QByteArray to QVector<double> ?
                        Why not convert to doubles when you read and add directly to QVector instead?

                        Also who sends the doubles? Its not trivial to send doubles over serial as they dont map
                        nicely to chars and there can be a difference in byteorder and precision loss.

                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          erytcg
                          wrote on last edited by erytcg
                          #25

                          I need convert because QCustomPlot func need doubles

                          void 	addData (const QVector< double > &keys, const QVector< double > &values, bool alreadySorted=false)
                          or
                          void 	addData (double key, double value)
                          
                          mrjjM 1 Reply Last reply
                          0
                          • E erytcg

                            I need convert because QCustomPlot func need doubles

                            void 	addData (const QVector< double > &keys, const QVector< double > &values, bool alreadySorted=false)
                            or
                            void 	addData (double key, double value)
                            
                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #26

                            @erytcg
                            Yes, i understand CustomPlot wants QVector<double> but i was wondering
                            instead of storing all values in ByteArray and then convert, if it would be more easy
                            to store directly in QVector when reading them off the serial port.

                            Anyway, how do u know how many indexes one double will take in ByteArray ?
                            double is normally 8 bytes (32 bit). Do you know the byte order from the sender?

                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #27

                              Hi
                              You can test with this sample
                              Uses QDataStream which should be good and fast.

                              int main(int argc, char *argv[])
                              {
                              
                              QByteArray byteArray;
                              
                              //generate ten random float value
                              QVector<float> v;
                              for (int i =0; i < 10;++i)
                              {
                              v << 100. * qrand()/RAND_MAX;
                              }
                              //ouput vector
                              qDebug() << v;
                              
                              //write float value to array
                              {
                              QDataStream out (&byteArray,QIODevice::WriteOnly);
                              foreach(float f,v)
                              {
                              out << f;
                              }
                              }
                              
                              //read all float from array
                              QVector<float> v2;
                              {
                              QDataStream in(byteArray);
                              while (!in.atEnd())
                              {
                              float f;
                              in >> f;
                              v2 << f;
                              }
                              }
                              //ouput vector
                              qDebug() << v2;
                              
                              return 0;
                              }
                              

                              From here
                              http://www.qtcentre.org/archive/index.php/t-24744.html

                              1 Reply Last reply
                              0
                              • E Offline
                                E Offline
                                erytcg
                                wrote on last edited by
                                #28

                                Maybe I try explain what should my applications do. I send to device command for example: LB - measure temperature and I receive
                                AAAA, data is exchanged in ASCII format. Number is in Hex format. for example 0x075d. And I want add this received data to plot. And set interval between measurement.

                                jsulmJ 1 Reply Last reply
                                0
                                • E erytcg

                                  Maybe I try explain what should my applications do. I send to device command for example: LB - measure temperature and I receive
                                  AAAA, data is exchanged in ASCII format. Number is in Hex format. for example 0x075d. And I want add this received data to plot. And set interval between measurement.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #29

                                  @erytcg You get double numbers as hex?! Is the double format used by this device clearly specified? I mean, you need to know how to convert those hex strings to doubles for that you need to know the format and byte order.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  2

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt
                                  • Unsolved