PDA

View Full Version : Setting up and reading from a ComPort with QExtSerialPort



jde08
30th May 2013, 19:06
Hi,

I am wondering how to use QExtSerialPort to declare and read from a Comport.

I am trying to read in a sting of data from an instrument and extract 4 points of data, 2 bytes each, and display them on my gui.

I have three main questions:


How do I make my Comport hook up with a specific comport? (COM38 in my case)
How do I read in the data into a string or char array?
How do I get that data from mainwindow to main?

Here is what I have so far:
Mainwindow.cpp;

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qextserialport.h>
#include <QTextBrowser>

QextSerialPort *port;

char buff[1024];


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
port=new QextSerialPort();
ui->setupUi(this);
port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);

port->setBaudRate(BAUD38400);

port->setFlowControl(FLOW_OFF);

port->setParity(PAR_NONE);

port->setDataBits(DATA_8);

port->setStopBits(STOP_1);

port->setQueryMode(QextSerialPort::EventDriven);


}

MainWindow::~MainWindow()
{
//for reading purpose

char buff[219];

if(port->bytesAvailable())

{

int i = port->read(buff, 1);//read 1 byte at a time

buff[i] = '\0';



}
delete ui;
}



Main.cpp;

#include "qextserialport.h"
#include "qextserialport_p.h"
#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QString>
#include <stdio.h>
#include <QtCore/QDebug>
#include <QtCore/QReadLocker>
#include <QtCore/QWriteLocker>



int main(int argc, char *argv[])
{


QApplication a(argc, argv);
QWidget window;

float beam1,beam2,beam3,beam4,average;


QString qstr1 = QString::number(beam1);
QString qstr2 = QString::number(beam2);
QString qstr3 = QString::number(beam3);
QString qstr4 = QString::number(beam4);
QString qstr5 = QString::number(average);

QGridLayout* mainLayout = new QGridLayout(&window);
QLabel* bl1 = new QLabel("Beam One");
QLabel* br1 = new QLabel(qstr1);
QLabel* bl2 = new QLabel("Beam Two");
QLabel* br2 = new QLabel(qstr2);
QLabel* bl3 = new QLabel("Beam Three");
QLabel* br3 = new QLabel(qstr3);
QLabel* bl4 = new QLabel("Beam Four");
QLabel* br4 = new QLabel(qstr4);
QLabel* al = new QLabel("Average");
QLabel* ar = new QLabel(qstr5);

mainLayout->addWidget(bl1,0,0);
mainLayout->addWidget(br1,0,1);
mainLayout->addWidget(bl2,1,0);
mainLayout->addWidget(br2,1,1);
mainLayout->addWidget(bl3,2,0);
mainLayout->addWidget(br3,2,1);
mainLayout->addWidget(bl4,3,0);
mainLayout->addWidget(br4,3,1);
mainLayout->addWidget(al,4,0);
mainLayout->addWidget(ar,4,1);

window.show();

return a.exec();
}