PDA

View Full Version : <solved> QtSerialPort: Open multiple ports



kfp
31st August 2015, 11:53
Hi all. Hope you are able to help me with this.
I'm fairly new to both C++ and the qt framework so I apologize if the question is a bit vague.

I have made a simple desktop serial port application using Qt Creator 3.5.0 (opensource) under windows 7.
The program works using one serial port but I need to read data from two barcode scanners, connected to two different serial ports, and compare the received data to a string.

I don't know if it is of any importance, but the barcode only passes one of the scanners, so the scanners will never transmit any data at the same time.

I have pasted my code below:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtSerialPort/QSerialPort>
#include <QMessageBox>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();


private slots:
void readData();
void openSerial(const int comNr);
void closeSerial(const int comNr);

private:
Ui::MainWindow *ui;
QSerialPort *serial;
};

#endif // MAINWINDOW_H

mainwindow.c

#include "mainwindow.h"
#include "ui_mainwindow.h"

#define nrBytes 8

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort(this);

openSerial(3);

connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
}

MainWindow::~MainWindow()
{
delete ui;
}


void MainWindow::readData()
{
if (serial->bytesAvailable() >= nrBytes)
{
QByteArray data = serial->readAll();
QString rxString = QString(data);
ui->serialTxt->setText(rxString);
if (rxString == "test")
{
//Do stuff
}
}
}

void MainWindow::openSerial(const int comNr)
{
serial->setPortName(QString("COM%1").arg(comNr));
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if (serial->open(QIODevice::ReadWrite))
{
ui->serialTxt->append("Connected");
}
else
{
QMessageBox::critical(this, tr("Error"), serial->errorString());
}
}

I'm aware my questions are plagued my my lacking c++ knowledge, but here we go:

How do I make single function, openSerial(), that both constructs and opens a new serial port? Do i pass a QSerialPort by reference to openSerial(). Or is it possible to create the function, so calling Eg. openSerial(3) would create: QSerialPort *serial3 and open COM3?
- And if so how do i make eg. serial3 visible to my other functions?

Thanks in advance

Kind regards
Kfp

Lesiok
31st August 2015, 13:56
Define 2 pointers to QSerialPort object and open both ports - line 28 in mainwindow.h should be :
QSerialPort *serial1,*serial2;
Lines 23 and 24 should be :
QSerialPort *openSerial(const int comNr);
void closeSerial(QSerialPort *com_ptr);

kfp
31st August 2015, 14:28
Thanks a lot for your quick answer.


QSerialPort *serial1,*serial2;
- Got it, thanks.

But it's unclear to me what the following code does.

QSerialPort *openSerial(const int comNr);

How would that enable me to call e.g:


openSerial(3);
openSerial(4);


To open COM3 and COM4?

Thanks for you patience...

Lesiok
31st August 2015, 15:26
Simply (without testing) :
void MainWindow::openSerial(const int comNr)
{
QSerialPort *serial = new QSerialPort();
serial->setPortName(QString("COM%1").arg(comNr));
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if (serial->open(QIODevice::ReadWrite))
{
ui->serialTxt->append("Connected");
}
else
{
QMessageBox::critical(this, tr("Error"), serial->errorString());
delete serial;
serial = 0;
}
return serial;
}
and in another place :
serial1 = openSerial(3);
serial2 = openSerial(4);

kfp
31st August 2015, 15:48
Thanks, that's great. - Got it up and running.

For completeness sake, I corrected your suggestion to:

QSerialPort *MainWindow::openSerial(const int comNr)

Lesiok
31st August 2015, 17:27
As I said : without testing :cool: