Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multiple Serial Ports
Forum Updated to NodeBB v4.3 + New Features

Multiple Serial Ports

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 4.0k Views 2 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.
  • CodeFreaksC Offline
    CodeFreaksC Offline
    CodeFreaks
    wrote on last edited by CodeFreaks
    #1

    Hi every one.
    I want to open multiple serial ports at the same App. .
    I tried to open them sequentially. The result is:

    Started!
    Ports Recognition Finished!
    Number of Availabe Ports:  4
    Port 1: 
    "Serial Test! (Port1)\r\n"
    QIODevice::read (QSerialPort): device not open
    QIODevice::read (QSerialPort): device not open
    QIODevice::read (QSerialPort): device not open
    QIODevice::read (QSerialPort): device not open
    QIODevice::read (QSerialPort): device not open
    QIODevice::read (QSerialPort): device not open
    QIODevice::read (QSerialPort): device not open
    QIODevice::read (QSerialPort): device not open
    
    K 1 Reply Last reply
    0
    • CodeFreaksC CodeFreaks

      Hi every one.
      I want to open multiple serial ports at the same App. .
      I tried to open them sequentially. The result is:

      Started!
      Ports Recognition Finished!
      Number of Availabe Ports:  4
      Port 1: 
      "Serial Test! (Port1)\r\n"
      QIODevice::read (QSerialPort): device not open
      QIODevice::read (QSerialPort): device not open
      QIODevice::read (QSerialPort): device not open
      QIODevice::read (QSerialPort): device not open
      QIODevice::read (QSerialPort): device not open
      QIODevice::read (QSerialPort): device not open
      QIODevice::read (QSerialPort): device not open
      QIODevice::read (QSerialPort): device not open
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @CodeFreaks

      Hi, not sure what you are expecting as response?

      Out of personal experience I know that at least the number of available ports can be opened in parallel. In my case 2 ports are available through serial (2 ports) to usb converter. That is on windows 10 64 bit.

      Therefore there are a couple of questions you need to answer at least to yourself.

      Can you handle at least one serial port?
      How many ports do you have available?

      In my case I tested with one port and when I was able to handle that part I had to simply duplicate a part and opened 2 ports.

      Vote the answer(s) that helped you to solve your issue(s)

      CodeFreaksC 1 Reply Last reply
      3
      • aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @CodeFreaks You will need to show at least some code in order to get help...

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • K koahnig

          @CodeFreaks

          Hi, not sure what you are expecting as response?

          Out of personal experience I know that at least the number of available ports can be opened in parallel. In my case 2 ports are available through serial (2 ports) to usb converter. That is on windows 10 64 bit.

          Therefore there are a couple of questions you need to answer at least to yourself.

          Can you handle at least one serial port?
          How many ports do you have available?

          In my case I tested with one port and when I was able to handle that part I had to simply duplicate a part and opened 2 ports.

          CodeFreaksC Offline
          CodeFreaksC Offline
          CodeFreaks
          wrote on last edited by CodeFreaks
          #4

          @koahnig
          @aha_1980
          Thanks for your reply.
          I have 4 ports available and I can communicate with each one if I remove others.
          Codes:

                    for (int i = 0 ; i < List.size() ; i++ ){
                        tempSerialPort = new QSerialPort(List.at(i));
                        portPointer = i;    tempSerialPort->setPortName(availableList.at(i).portName());     tempSerialPort->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);
                        tempSerialPort->setDataBits(QSerialPort::Data8);
                        tempSerialPort->setParity(QSerialPort::NoParity);
                        tempSerialPort->setStopBits(QSerialPort::OneStop);
                tempSerialPort->setFlowControl(QSerialPort::NoFlowControl);
                       if (tempSerialPort->open(QIODevice::ReadWrite)){
                           qDebug()<<"Started!";
                           connect(tempSerialPort, &QSerialPort::readyRead, this, &MainWindow::readData);
                           QByteArray data("Serial Test!");
                           tempSerialPort->write(data,qstrlen(data));
                      }
                    }
          
          
              QByteArray arreyData = tempSerialPort->readAll();
              tempData += QString::fromStdString(arreyData.toStdString());
          
              if(arreyData.contains("\r\n") && tempData.contains("Port1")){
                  qDebug()<<"Port 1: ";
                tempSerialPort->close();
          //      delete tempSerialPort;
                SerialPort1     = new QSerialPort(availableList.at(portPointer));
                              qDebug()<<tempData;
                              tempData = "";
              }
              if(arreyData.contains("\r\n") && tempData.contains("Port2")){
                  qDebug()<<"Port 2: ";
                tempSerialPort->close();
          //      delete tempSerialPort;
                SerialPort2    = new QSerialPort(availableList.at(portPointer));
                              qDebug()<<tempData;
                              tempData = "";
              }
              if(arreyData.contains("\r\n") && tempData.contains("Port3")){
                  qDebug()<<"Port 3: ";
                tempSerialPort->close();
          //      delete tempSerialPort;
                SerialPort3 = new QSerialPort(availableList.at(portPointer));
                              qDebug()<<tempData;
                              tempData = "";
          }
          ...
          

          Logic of coding is that app. sends "Serial Test!" and devices return it with port id (Port1,...,Port4).

          aha_1980A 1 Reply Last reply
          0
          • CodeFreaksC CodeFreaks

            @koahnig
            @aha_1980
            Thanks for your reply.
            I have 4 ports available and I can communicate with each one if I remove others.
            Codes:

                      for (int i = 0 ; i < List.size() ; i++ ){
                          tempSerialPort = new QSerialPort(List.at(i));
                          portPointer = i;    tempSerialPort->setPortName(availableList.at(i).portName());     tempSerialPort->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);
                          tempSerialPort->setDataBits(QSerialPort::Data8);
                          tempSerialPort->setParity(QSerialPort::NoParity);
                          tempSerialPort->setStopBits(QSerialPort::OneStop);
                  tempSerialPort->setFlowControl(QSerialPort::NoFlowControl);
                         if (tempSerialPort->open(QIODevice::ReadWrite)){
                             qDebug()<<"Started!";
                             connect(tempSerialPort, &QSerialPort::readyRead, this, &MainWindow::readData);
                             QByteArray data("Serial Test!");
                             tempSerialPort->write(data,qstrlen(data));
                        }
                      }
            
            
                QByteArray arreyData = tempSerialPort->readAll();
                tempData += QString::fromStdString(arreyData.toStdString());
            
                if(arreyData.contains("\r\n") && tempData.contains("Port1")){
                    qDebug()<<"Port 1: ";
                  tempSerialPort->close();
            //      delete tempSerialPort;
                  SerialPort1     = new QSerialPort(availableList.at(portPointer));
                                qDebug()<<tempData;
                                tempData = "";
                }
                if(arreyData.contains("\r\n") && tempData.contains("Port2")){
                    qDebug()<<"Port 2: ";
                  tempSerialPort->close();
            //      delete tempSerialPort;
                  SerialPort2    = new QSerialPort(availableList.at(portPointer));
                                qDebug()<<tempData;
                                tempData = "";
                }
                if(arreyData.contains("\r\n") && tempData.contains("Port3")){
                    qDebug()<<"Port 3: ";
                  tempSerialPort->close();
            //      delete tempSerialPort;
                  SerialPort3 = new QSerialPort(availableList.at(portPointer));
                                qDebug()<<tempData;
                                tempData = "";
            }
            ...
            

            Logic of coding is that app. sends "Serial Test!" and devices return it with port id (Port1,...,Port4).

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by aha_1980
            #5

            @CodeFreaks said in Multiple Serial Ports:

            for (int i = 0 ; i < List.size() ; i++ ){

            What is the definition of List ?

            Edit: and maybe you describe what you want to do with the serial ports.

            QByteArray arreyData = tempSerialPort->readAll();
            tempData += QString::fromStdString(arreyData.toStdString());

            This could easily be: tempData += QLatin1String(tempSerialPort->readAll());

            (assuming the data is pure ASCII).

            Qt has to stay free or it will die.

            CodeFreaksC 1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              @CodeFreaks said in Multiple Serial Ports:

              tempSerialPort = new QSerialPort(List.at(i));

              You are overwriting tempSerialPort on each iteration.

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

              CodeFreaksC 1 Reply Last reply
              1
              • aha_1980A aha_1980

                @CodeFreaks said in Multiple Serial Ports:

                for (int i = 0 ; i < List.size() ; i++ ){

                What is the definition of List ?

                Edit: and maybe you describe what you want to do with the serial ports.

                QByteArray arreyData = tempSerialPort->readAll();
                tempData += QString::fromStdString(arreyData.toStdString());

                This could easily be: tempData += QLatin1String(tempSerialPort->readAll());

                (assuming the data is pure ASCII).

                CodeFreaksC Offline
                CodeFreaksC Offline
                CodeFreaks
                wrote on last edited by CodeFreaks
                #7

                @aha_1980
                Thanks for reply.
                List = QSerialPortInfo::availablePorts();

                1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  @CodeFreaks said in Multiple Serial Ports:

                  tempSerialPort = new QSerialPort(List.at(i));

                  You are overwriting tempSerialPort on each iteration.

                  CodeFreaksC Offline
                  CodeFreaksC Offline
                  CodeFreaks
                  wrote on last edited by CodeFreaks
                  #8

                  @SGaist
                  thanks for reply.
                  I also tried an array of tempSerialPort[] but result doesn't changed.
                  I also delete the temSerialPort after any data received from the port.

                  aha_1980A 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    What about printing the error you got if the open call failed ? You only do something if it succeeded.

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

                    CodeFreaksC 1 Reply Last reply
                    2
                    • CodeFreaksC CodeFreaks

                      @SGaist
                      thanks for reply.
                      I also tried an array of tempSerialPort[] but result doesn't changed.
                      I also delete the temSerialPort after any data received from the port.

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @CodeFreaks

                      Before commenting on the implementation, let's focus on the architecture.

                      So again:

                      and maybe you describe what you want to do with the serial ports.

                      Qt has to stay free or it will die.

                      CodeFreaksC 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        What about printing the error you got if the open call failed ? You only do something if it succeeded.

                        CodeFreaksC Offline
                        CodeFreaksC Offline
                        CodeFreaks
                        wrote on last edited by CodeFreaks
                        #11

                        @SGaist
                        Thanks for reply.
                        I must add some codes for different error conditions, in future.
                        But right now I'm sure that ports are opened.
                        I think the problem should be about rewriting the tempSerialPort, as you said (but I don't have any idea ...).

                                     if (tempSerialPort[portPointer]->open(QIODevice::ReadWrite)){
                                         qDebug()<<QString ("Port %1 Started!").arg(portPointer);
                        ...
                        
                        "Port 0 Started!"
                        "Port 1 Started!"
                        Ports Recognition Finished!
                        Number of Availabe Ports:  2
                        Port 2: 
                        "Serial Test! (Port2)\r\n"
                        QIODevice::read (QSerialPort): device not open
                        QIODevice::read (QSerialPort): device not open
                        QIODevice::read (QSerialPort): device not open
                        QIODevice::read (QSerialPort): device not open
                        QIODevice::read (QSerialPort): device not open
                        QIODevice::read (QSerialPort): device not open
                        
                        1 Reply Last reply
                        0
                        • aha_1980A aha_1980

                          @CodeFreaks

                          Before commenting on the implementation, let's focus on the architecture.

                          So again:

                          and maybe you describe what you want to do with the serial ports.

                          CodeFreaksC Offline
                          CodeFreaksC Offline
                          CodeFreaks
                          wrote on last edited by
                          #12

                          @aha_1980
                          I want to send/receive data to/from them with same app. .

                          K 1 Reply Last reply
                          0
                          • CodeFreaksC CodeFreaks

                            @aha_1980
                            I want to send/receive data to/from them with same app. .

                            K Offline
                            K Offline
                            koahnig
                            wrote on last edited by
                            #13

                            @CodeFreaks

                            It might be a stupid question, but is it possible that you are trying to open each time the port again?

                            At least that would be a conclusion from your tempSerialPort set up as indicated by @SGaist.

                            The serial ports are not closed, they are staying open. When you try to open each time, you cannot the second time, because it is still open from first initialization.

                            The code fragments you are delivering are not giving enough detail to get a complete overview.

                            However, to work properly you need

                            • initilization section,
                            • reading section (apparently working with your signal slots),
                            • writing section (where you require access to previously opened ports)
                            • eventually closing section (for wrapping up when finished)

                            First and last section may be used only at start and end of application for instance. The reading and writing section would be could be looped over frequently.

                            Vote the answer(s) that helped you to solve your issue(s)

                            1 Reply Last reply
                            3
                            • aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @CodeFreaks

                              c'mon, a bit more information would be needed, e.g.

                              • do you need to open/close them all the time?
                              • do you just write to them or do you also receive data?

                              I think closing a port if you don't need it for some time is good practice, remember to set it up correctly when opening again (someone else might have used it in meantime).

                              But I would not create and destroy them all the time. Rather have a member variable: QList<QSerialPort *> m_ports and add the tempSerialPort's to the list: m_ports.append(tempSerialPort).

                              In the slot you can access them with sender(), so you know where the answer is coming from.

                              When I see this:

                              if(arreyData.contains("\r\n")

                              then I think about QSerialPort::canReadLine()

                              Regards

                              PS: tempData = ""; can be replaced by tempData.clear();

                              Qt has to stay free or it will die.

                              CodeFreaksC 1 Reply Last reply
                              3
                              • aha_1980A aha_1980

                                @CodeFreaks

                                c'mon, a bit more information would be needed, e.g.

                                • do you need to open/close them all the time?
                                • do you just write to them or do you also receive data?

                                I think closing a port if you don't need it for some time is good practice, remember to set it up correctly when opening again (someone else might have used it in meantime).

                                But I would not create and destroy them all the time. Rather have a member variable: QList<QSerialPort *> m_ports and add the tempSerialPort's to the list: m_ports.append(tempSerialPort).

                                In the slot you can access them with sender(), so you know where the answer is coming from.

                                When I see this:

                                if(arreyData.contains("\r\n")

                                then I think about QSerialPort::canReadLine()

                                Regards

                                PS: tempData = ""; can be replaced by tempData.clear();

                                CodeFreaksC Offline
                                CodeFreaksC Offline
                                CodeFreaks
                                wrote on last edited by CodeFreaks
                                #15

                                @aha_1980
                                @koahnig
                                Thanks for replies.
                                I've connected readyRead signal to the Mainwindow::readData.
                                There is only one slot to read the data, only the last port in the list will receive the data.

                                I need all ports to be open. I want to communicate with all of them.

                                @aha_1980 said in Multiple Serial Ports:

                                In the slot you can access them with sender(), so you know where the answer is coming from.

                                May I ask you to explain more?

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @CodeFreaks said in Multiple Serial Ports:

                                  QByteArray arreyData = tempSerialPort[portPointer]->readAll();

                                  Because you are using portPointer which value won't change after your opened all your serial ports.

                                  @CodeFreaks said in Multiple Serial Ports:

                                  @aha_1980 said in Multiple Serial Ports:

                                  In the slot you can access them with sender(), so you know where the answer is coming from.

                                  May I ask you to explain more?

                                  Use the doc Luke: QObject::sender.

                                  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
                                  2
                                  • BuckwheatB Offline
                                    BuckwheatB Offline
                                    Buckwheat
                                    wrote on last edited by
                                    #17

                                    Hi @CodeFreaks

                                    I would like to address your design. It is extremely confusing. It is great you are checking port existence.

                                    Since you close them before you re-open them as-needed, I would 1) Not worry about doing new QSerialPort into a temp pointer and 2) connecting it at this point.

                                    Just create a stack-instance (or use 'new'), do your check, close the port and if you used 'new' delete it right away. This will ensure you keep your resources clear and free. All you need to do is make your like of valid ports.

                                    When you finally open the port for use, do your connect. This will ensure your signal and its sender are valid. Doing your connect on the temp pointer (which you delete) frees that connection. Once you are sure you have your ports opened correctly you can use sender () in your slot as follows:

                                    QSerialPort* port = dynamic_cast <QSerialPort*> (sender ()); // You can also use qobject_cast
                                    if (port) {
                                    ...
                                    }

                                    I use multiple ports with auto-baud detection constantly along with CAN and UDP so you are close and you have no fear! This is all good.

                                    Dave Fileccia

                                    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 Extensions
                                    • Unsolved