A
A
AlexSer2020-01-22 10:20:46
Qt
AlexSer, 2020-01-22 10:20:46

How to make simultaneous reception from two com ports in QT?

Hello everyone, how to organize simultaneous reception of data from two com ports. It seems that I am making a connection,
and both com ports are connected, but the data in debug goes only to the last port that I connected.

делаю коннекты актионов:
    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()));

COMPORTConnect slot:
sPort=new ConnectionCom();  //класс соединения с ком портом
         if(sPort->OpenConCom(port, 9600, 8,"none", 1, "none", false)==true){
                 ui->terminal->append(port+" соединение открыто");
                 if(QObject::sender()->objectName()=="actionSwelab_Alfa_1"){
                     ui->actionSwelab_Alfa_1->setEnabled(false);
                 }
                      //выводим сообщение
                 QMessageBox::information(this, "Внимание","Соединение с "+port+" установлено успешно");
                connect(sPort->serialPort,SIGNAL(readyRead()),this,SLOT(ReadDataSerial()));
                 ui->terminal->append("Получение данных с анализатора  " + analizator+ " по порту " + sPort->serialPort->portName());
                 qDebug()<<"Получение данных с анализатора  " + analizator+ " по порту " + sPort->serialPort->portName();

         }else{
               QMessageBox::warning(this, "Внимание","Порт "+port+" не доступен!");
               ui->terminal->append(port+"соединение не доступно");
         }

Feed port connection class:
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;
               }

}

Data Reading:
void MainWindow::ReadDataSerial(){
    QString byte;
    byte = sPort->serialPort->readAll();
      ui->terminal->append("Принятые данные  по порту"+sPort->serialPort->portName()+"\n" + byte);
 qDebug()<<byte;

    }



}

What is the problem, tell me please. Data is received only on the port to which it was connected last, on the first port there are only empty values ​​in the debug.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artisan, 2020-01-22
@AlexSer

Maybe we should declare another instance of the ConnectionCom() class (not one sPort, but two sPort1 and sPort2). Each COM port has its own instance. And in the slot, check if one COM port is already in use, create a second instance, and open the second COM port in it.
Or in ConnectionCom() have two instances of QSerialPort() (serialPort1 and serialPort2).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question