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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    Only one solution: have as many QSerialPort objects.

    Using e.g. a QVector<QSerialPort*> as member variable to keep them easily manageable.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • M Offline
      M Offline
      mostefa
      wrote on last edited by
      #3

      Hi @erytcg

      To open more than one serialport

      you have two create more than one instance of QSerialPort:

      For example you have two serial ports on your device : (ttyUSB0, ttyUSB1)

      To open these ports you have to do something like this:

      QSerialPort* firstSerialPort = new QSerialPort();
      QSerialPort* secondSerialPort = new QSerialPort();
      
      firstSerialPort->setPortName("ttyUSB0");
      secondSerialPort->setPortName("ttyUSB1");
      .
      .
      .
      if (firstSerialPort->open(QIODevice::ReadWrite))
      .
      .
      .
      if (secondSerialPort->open(QIODevice::ReadWrite))
      .
      .
      .
      

      I hope this can help you

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

        @mostefa I think it's not good idea i don't know how many devices I have to connect
        @SGaist can yoiu show me example how add add object to QVector and obtain acces to serialport name

        M 1 Reply Last reply
        0
        • E erytcg

          @mostefa I think it's not good idea i don't know how many devices I have to connect
          @SGaist can yoiu show me example how add add object to QVector and obtain acces to serialport name

          M Offline
          M Offline
          mostefa
          wrote on last edited by mostefa
          #5

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

          @mostefa I think it's not good idea i don't know how many devices I have to connect

          Then what about ?

          QVector<QSerialPort *> v;
              Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) {
                  QSerialPort* serialPort = new QSerialPort() ;
          
          //Here set port informations
                  serialPort->setPort(port);
          .
          .
          .
          
                   if (serialPort->open(QIODevice::ReadWrite))
          {
          .
          .
                        v.append(serialPort);
          }
            }
          
          1 Reply Last reply
          1
          • E Offline
            E Offline
            erytcg
            wrote on last edited by
            #6
            void MainWindow::openSerialPort()
            {         
                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);
            
                        if (serialPort->open(QIODevice::ReadWrite)) {
                             v.append(serialPort);
                             ui->listWidget->addItem(p.name);
                          
                        } else { 
                            QMessageBox::critical(this, tr("Error"), serialPort->errorString());
            
                            showStatusMessage(tr("Open error"));
                        }
                    }
            
            }
            
            void MainWindow::closeSerialPort()
            {
                if (serialPort->isOpen())
                {
                    serialPort->close();
                    QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
                    foreach(QListWidgetItem * item, items)
                    {
                        delete ui->listWidget->takeItem(ui->listWidget->row(item));
                    }
                }
            
                showStatusMessage(tr("Disconnected"));
            }
            

            it's ok?

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

              Hi
              In void MainWindow::closeSerialPort()
              you call if (serialPort->isOpen())

              But that is only serialPort pointer. you are not using the
              Vector<QSerialPort *> v; at all so i wonder if this is what u want ?

              I imagine you need to loop over v and close all and not just whatever
              serialPort points to ?

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

                I don't understand good with this QVector. I don't know how acces to one of port and close one port not all.

                mrjjM 1 Reply Last reply
                0
                • E erytcg

                  I don't understand good with this QVector. I don't know how acces to one of port and close one port not all.

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

                  @erytcg
                  Hi
                  its a list of comports.

                  so v[index] gives a QSerialPort *

                  But you will have to answer what app does.

                  Currently you open all comports available on device.

                  Should closeSerialPort() close them all again?

                  to close one you would do
                  QSerialPort * Cur=v[0];
                  Cur->close();
                  or even
                  v[0]->close();

                  Notice i use 0 for closing the first.

                  So to help you , i need to understand what app does as its uncommon to open ALL comports :)

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

                    So I need more functions, one for add aviable ports to vector and one to open specific port? For close functions I want close only one port.

                    mrjjM 1 Reply Last reply
                    0
                    • E erytcg

                      So I need more functions, one for add aviable ports to vector and one to open specific port? For close functions I want close only one port.

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

                      @erytcg

                      • close functions I want close only one port.

                      But which one?

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

                        Choosing from:
                        QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
                        foreach(QListWidgetItem * item, items)
                        {
                        delete ui->listWidget->takeItem(ui->listWidget->row(item));
                        }

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

                          Hi
                          if you move
                          QVector<QSerialPort *> v;
                          to the MainWindow.h (in the class)
                          and remove serialPort variable also!

                          then you could do

                          void MainWindow::closeSerialPort( int index)
                          {
                          QSerialPort * serialPort = v[index];
                          if (serialPort->isOpen())
                          {
                          serialPort->close();
                          ..

                          and close the one u want with
                          closeSerialPort( some number)

                          1 Reply Last reply
                          0
                          • 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

                                          • Login

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