A
A
AlexSer2020-01-26 13:41:14
Qt
AlexSer, 2020-01-26 13:41:14

How to organize simultaneous operation of n com ports in QT C++?

I am writing a program in QT to work with Com ports. Since a beginner, much is not clear.
The problem is this, it is not possible to bind actions (connections to com ports) to the slot. If I make a connection to the com port, then only the data on the last connection is displayed in the debug.
I'm trying to make sure that all connections to the com port go through 1 slot.
If I make two different slots for a connection to a com port, then the data goes fine. Debug displays data from 2 - com ports. but there is already code duplication. And since I will have up to 5 connections with com ports, it’s not convenient.
In mainwindow.h I declare an instance of the class

ConnectionCom *sPort;     //мой класс для соединение с ком портом.

ConnectionCom() class code
bool ConnectionCom::OpenConCom(QString port, int baudrate, int bits, QString parity, int stopBits, QString flowControl, bool CTS_RTS){
               serialPort=new QSerialPort();
               serialPort->setPortName(port);
               serialPort->setBaudRate(baudrate);
               serialPort->setDataBits(QSerialPort::Data8);
               serialPort->setParity(QSerialPort::NoParity);
               serialPort->setStopBits(QSerialPort::OneStop);
               serialPort->setFlowControl(QSerialPort::NoFlowControl);

               if(CTS_RTS==true){
                   serialPort->setRequestToSend(true);
                   serialPort->setDataTerminalReady(true);
               }
               serialPort->open(QIODevice::ReadOnly);
               if(serialPort->isOpen()){

                   return  true;

               }else{
                   return false;
               }

}

mainwindow.cpp code
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);



    //соединение с сервером БД;
    ConnectionSql *sql;
    sql=new ConnectionSql();
    bool condsql=sql->OpenConnection("192.168.50.100","user_serial","[email protected]");

    ui->terminal->append("Проверка доступа к серверу Mysql:........ " + QVariant(condsql).toString());
    ui->terminal->append("Провека доступа к локальной БД SQLite:...........");
    //coeдинение с com port
    connect(ui->actionSwelab_Alfa_1, SIGNAL(triggered()), this, SLOT(ComPortConnection()));
    connect(ui->actionSwelab_Alfa_2, SIGNAL(triggered()), this, SLOT(ComPortConnection()));
    connect(ui->actionMeldonic_1, SIGNAL(triggered()), this, SLOT(ComPortConnection()));
    connect(ui->actionUrit_3020, SIGNAL(triggered()), this, SLOT(ComPortConnection()));

 //  connect(ui->actionSwelab_Alfa_1, SIGNAL(triggered()), this, SLOT(Help()));
  // connect(ui->actionSwelab_Alfa_2, SIGNAL(triggered()), this, SLOT(Help()));
 //  connect(ui->actionMeldonic_1, SIGNAL(triggered()), this, SLOT(Help()));



}

void MainWindow::ComPortConnection(){
       QString port;


    if(QObject::sender()->objectName()=="actionSwelab_Alfa_1"){
           port="COM1";
           analizator="Swelab";

     }
     if(QObject::sender()->objectName()=="actionSwelab_Alfa_2"){
           port="COM3";
           analizator="Swelab";

     }
     if(QObject::sender()->objectName()=="actionMeldonic_1"){
           port="COM6";
           analizator="Medonic";

     }
     if(QObject::sender()->objectName()=="actionUrit_3020"){
          analizator="Urit3020";
          port="COM6";

     }

     sPort=new ConnectionCom();
       if(sPort->OpenConCom(port, 9600, 8,"none", 1, "none", false)==true){
             connect(sPort->serialPort,SIGNAL(readyRead()),this,SLOT(ReadDataSerial()));
        
       }
}


void MainWindow::ReadDataSerial(){
    QString byte;
    byte=sPort->serialPort->readAll();
          qDebug()<<sPort->serialPort->portName()+":"+byte;

}

How to make all connections refer to 1 data read slot, and debug display data from all ports?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question