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;
}
{
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
#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
To copy to clipboard, switch view to plain text mode
mainwindow.c
#include "mainwindow.h"
#include "ui_mainwindow.h"
#define nrBytes 8
MainWindow
::MainWindow(QWidget *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)
{
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);
{
ui->serialTxt->append("Connected");
}
else
{
QMessageBox::critical(this, tr
("Error"), serial
->errorString
());
}
}
#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());
}
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks