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. Multi serial port application
Forum Updated to NodeBB v4.3 + New Features

Multi serial port application

Scheduled Pinned Locked Moved Unsolved General and Desktop
49 Posts 5 Posters 14.1k 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.
  • V veera

    @jsulm ,
    In gui part i had created UI as a Dialog window in this window i need to connect three serial ports i am using qcombobox for taking serialportInfo as a

    foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
            {
    
               ui->Port_comboBox->addItem(serialPortInfo.portName());
            }
    
        foreach (const QSerialPortInfo &serialPortInfo2, QSerialPortInfo::availablePorts())
            {
    
               ui->Port2_comboBox->addItem(serialPortInfo2.portName());
            }
    
        foreach (const QSerialPortInfo &serialPortInfo3, QSerialPortInfo::availablePorts())
            {
    
               ui->Port3_comboBox->addItem(serialPortInfo3.portName());
            }
    int count_ports = QSerialPortInfo::availablePorts().count();
    

    with this serial count i am using to check port and serial attributes from gui after validating all assigning each port with qthread and qtimer for reading and writing data .....response window is plaintextwindow....how to start please guide me ........thanks for the valuabl response....

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

    @veera Are you sure you need threads and timers? Qt serial port implementation is asynchronous.

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

    1 Reply Last reply
    2
    • V Offline
      V Offline
      veera
      wrote on last edited by
      #7

      @jsulm
      yes i have to do it in qthreads and qtimer for managing three serial ports for application ,,,,,i don't know exactly you guide me in right manner ...

      aha_1980A 1 Reply Last reply
      0
      • V veera

        @jsulm
        yes i have to do it in qthreads and qtimer for managing three serial ports for application ,,,,,i don't know exactly you guide me in right manner ...

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

        @veera

        You didn't understand me. I said, with your task you can create one object containing the logic and the UI to talk to a serial device. There may be small differences between the devices, but I'm sure this can be configured. Once you have this object working (you can test it alone with one or all devices), you instantiate three instances of this object and let them run together.

        I don't know if you need threads, that's up to you. I guess you will need not and threads usually complicate things a lot, so I would avoid it.

        Regarding the UI <-> QSerialPort coupling: Have you already looked at and understand the Terminal example? [1]

        Regards

        [1] http://doc.qt.io/qt-5/qtserialport-terminal-example.html

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • V Offline
          V Offline
          veera
          wrote on last edited by
          #9

          @aha_1980
          okay thanks for the valuable reply i am sharing the code here for collecting all the parameters from gui storing into my structure for opening the serial ports .and how to set the mark and space parity ,i have to use the termios structure ?

          void Dialog::on_set_pushButton_clicked()
          {

          QString Setbutton = ui->set_pushButton->text();
          if (Setbutton == "setPort1" )
          {
              QString port1 = ui->port_comboBox->currentText();
              QString serPort1 = "ttyS4";
          
              DevSettings.port_name = port1;
              QString dev_name = "/dev/";
              QString dev_port = dev_name + port1;
              //qDebug () << "dev_port name is " << dev_port;
              DevSettings.port_name = dev_port;
              qDebug() << "DevSettings.port_name " << DevSettings.port_name;
              QString p1_speed = ui->speed_comboBox->currentText();
              QString p1_start = ui->start_comboBox->currentText();
              QString p1_stop = ui->stop_comboBox->currentText();
              QString p1_parity = ui->parity_comboBox->currentText();
              QString p1_data = ui->data_comboBox->currentText();
              QString p1_hwflow = ui->hwflow_comboBox->currentText();
              QString p1_swflow = ui->swflow_comboBox->currentText();
              bool p1_log   = ui->log_checkBox->checkState();
              QString p1_interval = ui->Interval_comboBox->currentText();
          
          
              DevSettings.baudRate = p1_speed;
              DevSettings.startbit = p1_start.toInt();
              DevSettings.parity = p1_parity;
              DevSettings.StopBits = p1_stop;
              DevSettings.hwflowControl = p1_hwflow;
              DevSettings.swflowControl = p1_swflow;
              DevSettings.dataBits = p1_data;
              DevSettings.logfileEnabled =p1_log;
              DevSettings.SerialPort_Interval = p1_interval;
              qDebug() << DevSettings.baudRate << DevSettings.startbit << DevSettings.parity << DevSettings.StopBits << DevSettings.hwflowControl <<  DevSettings.swflowControl <<  DevSettings.dataBits <<  DevSettings.logfileEnabled << DevSettings.SerialPort_Interval;
          
              }
          

          }

          aha_1980A 1 Reply Last reply
          0
          • V Offline
            V Offline
            veera
            wrote on last edited by
            #10

            here is my structure in dialog.h

            enum portWindow
            {
            WINDOW_1
            ,WINDOW_2
            ,WINDOW_3
            };

                struct Settings {
                    FILE *fd;
                    QString port_name;
                    QString baudRate;
                    QString dataBits;
                    qint64 startbit;
                    QString parity;
                    QString StopBits;
                    QString hwflowControl;
                    QString swflowControl;
                    bool logfileEnabled;
                    QString SerialPort_Interval;
                    enum portWindow serialPortWindow;
                };
            
                Settings DevSettings;
            

            and project files are

            SOURCES +=
            main.cpp
            dialog.cpp
            serialappparams.cpp

            HEADERS +=
            dialog.h
            serialappparams.h

            FORMS +=
            dialog.ui

            1 Reply Last reply
            0
            • V veera

              @aha_1980
              okay thanks for the valuable reply i am sharing the code here for collecting all the parameters from gui storing into my structure for opening the serial ports .and how to set the mark and space parity ,i have to use the termios structure ?

              void Dialog::on_set_pushButton_clicked()
              {

              QString Setbutton = ui->set_pushButton->text();
              if (Setbutton == "setPort1" )
              {
                  QString port1 = ui->port_comboBox->currentText();
                  QString serPort1 = "ttyS4";
              
                  DevSettings.port_name = port1;
                  QString dev_name = "/dev/";
                  QString dev_port = dev_name + port1;
                  //qDebug () << "dev_port name is " << dev_port;
                  DevSettings.port_name = dev_port;
                  qDebug() << "DevSettings.port_name " << DevSettings.port_name;
                  QString p1_speed = ui->speed_comboBox->currentText();
                  QString p1_start = ui->start_comboBox->currentText();
                  QString p1_stop = ui->stop_comboBox->currentText();
                  QString p1_parity = ui->parity_comboBox->currentText();
                  QString p1_data = ui->data_comboBox->currentText();
                  QString p1_hwflow = ui->hwflow_comboBox->currentText();
                  QString p1_swflow = ui->swflow_comboBox->currentText();
                  bool p1_log   = ui->log_checkBox->checkState();
                  QString p1_interval = ui->Interval_comboBox->currentText();
              
              
                  DevSettings.baudRate = p1_speed;
                  DevSettings.startbit = p1_start.toInt();
                  DevSettings.parity = p1_parity;
                  DevSettings.StopBits = p1_stop;
                  DevSettings.hwflowControl = p1_hwflow;
                  DevSettings.swflowControl = p1_swflow;
                  DevSettings.dataBits = p1_data;
                  DevSettings.logfileEnabled =p1_log;
                  DevSettings.SerialPort_Interval = p1_interval;
                  qDebug() << DevSettings.baudRate << DevSettings.startbit << DevSettings.parity << DevSettings.StopBits << DevSettings.hwflowControl <<  DevSettings.swflowControl <<  DevSettings.dataBits <<  DevSettings.logfileEnabled << DevSettings.SerialPort_Interval;
              
                  }
              

              }

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

              @veera said in multi serial port application:

              okay thanks for the valuable reply i am sharing the code here for collecting all the parameters from gui storing into my structure for opening the serial ports .

              I'm not sure if you need to store the data separately: you have it in the UI and you set them in the QSerialPort object. IMHO there's no need to cache them again... There is some caching in the Terminal example, but mostly because the data is transported from the settings dialog to the main window.

              and how to set the mark and space parity ,i have to use the termios structure ?

              With http://doc.qt.io/qt-5/qserialport.html#parity-prop ?

              Qt has to stay free or it will die.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                veera
                wrote on last edited by
                #12

                @aha_1980
                How would you know the which port is sending the port information ,how i need to handle this problem????

                aha_1980A 1 Reply Last reply
                0
                • V veera

                  @aha_1980
                  How would you know the which port is sending the port information ,how i need to handle this problem????

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

                  @veera said in multi serial port application:

                  @aha_1980
                  How would you know the which port is sending the port information ,how i need to handle this problem????

                  Sorry, but I don't understand your question.

                  As said, I would start creating ONE object (maybe a QPanel) configuring ONE serial port, sending data to ONE serial port and receiving data from ONE serial port. If you have this working, it's easy to have three of them so you can communicate with all serial ports.

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    veera
                    wrote on last edited by
                    #14

                    Hi @aha_1980 ,
                    I am working for single port now ,its not able to open the port here is my code by using send pushbutton and connect()

                    void Dialog :: open_serialport()
                    {
                    QSerialPort serial;
                    QString portName = ui->port_comboBox->currentText();
                    qDebug() << "The portName is " << portName;
                    bool currentPortNameChanged = false;
                    QString currentPortName;
                    if(currentPortName != portName)
                    {
                    currentPortName = portName;
                    currentPortNameChanged = true;
                    }

                    if(currentPortName.isEmpty())
                    {
                        qDebug() << "No port name specified";
                    }
                    
                    if(currentPortNameChanged)
                    {
                        serial.close();
                        serial.setPortName(portName);
                        qDebug() << "portName" << portName;
                    
                        if(!serial.open(QIODevice::ReadWrite))
                        {
                            qDebug() << "can not able to open the serial port";
                        }
                    }
                    

                    }
                    its giving error i am passing from gui ,please help me out....

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

                      @veera said in multi serial port application:

                      void Dialog :: open_serialport()
                      {
                      QSerialPort serial; <<<< WRONG ! its local variable

                      make it a class member of Dialog

                      1 Reply Last reply
                      3
                      • V Offline
                        V Offline
                        veera
                        wrote on last edited by
                        #16

                        @mrjj ,
                        just now declared a QSerialPort as data members of the dialog class in dialog.h still also i am getting same error...

                        here is the declaration of the QSerialPort

                        #ifndef DIALOG_H
                        #define DIALOG_H

                        #include <QDialog>
                        #include <stdio.h>
                        #include <QtSerialPort/QSerialPort>
                        
                        
                        namespace Ui {
                        class Dialog;
                        }
                        
                        class Dialog : public QDialog
                        {
                            Q_OBJECT
                        
                        public:
                            QSerialPort serial;
                        
                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #17

                          Super
                          but what error ?

                          1 Reply Last reply
                          0
                          • V Offline
                            V Offline
                            veera
                            wrote on last edited by
                            #18

                            can not able to open the serial port..what was the type its receiving whether its ttyS4 or /dev/ttyS4?

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              veera
                              wrote on last edited by
                              #19

                              @mrjj ,
                              I am directly assigning the portname from gui qcombox is it correct way for accessing the port name ?

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

                                Hi
                                normally its ttyS4
                                for
                                serial->setPortName(p.name);

                                Did you see the sample
                                http://doc.qt.io/qt-5/qtserialport-terminal-example.html

                                Its available directly in Creator and you can use to test.

                                1 Reply Last reply
                                0
                                • V Offline
                                  V Offline
                                  veera
                                  wrote on last edited by
                                  #21

                                  yes i have refer that example only.....

                                  1 Reply Last reply
                                  0
                                  • V Offline
                                    V Offline
                                    veera
                                    wrote on last edited by
                                    #22

                                    i have tried both the things not able to open the port .....

                                    mrjjM 1 Reply Last reply
                                    0
                                    • V veera

                                      i have tried both the things not able to open the port .....

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

                                      @veera

                                      so are you sure PC has that port ?

                                      if using a serial usb adapter, it has other name.

                                      If you cannot open port using the Terminal sample then
                                      something is wrong outside Qt.

                                      1 Reply Last reply
                                      0
                                      • V Offline
                                        V Offline
                                        veera
                                        wrote on last edited by
                                        #24

                                        yes having two ports only ttyS4 and ttyS5 i am not using any usb to serial converter i am directly connecting with serial port only .....i have tried terminal example its giving the permission error means sudo permission .....

                                        1 Reply Last reply
                                        0
                                        • V Offline
                                          V Offline
                                          veera
                                          wrote on last edited by
                                          #25

                                          permission is denied error while opening the port in Terminal Sample...

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