PDA

View Full Version : qserialdevice & gui



sos1
21st June 2013, 16:42
I want to use qtcreator and qserialdevice to allow my Beaglebone (branch 3.2 Angstrom) communicate with devices. I am developing on Ubuntu 13.10.

I downloaded the qserial device and it built OK using the cross compiler. I then compiled the examples and they work on the Beaglebone. Now I want to use the qserialdevice within the QtCreator GUI but I'm having trouble compiling. The error is "error: cannot find -lqserialdeviced". I created a new directory under qserialdevice/examples and copied the files from writer directory before modifying them.

I know it should be simple but I'm stuck and need help.

Regards,
James

Heres my .pro file:-


#-------------------------------------------------
#
# Project created by QtCreator 2013-06-21T16:11:25
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = writerw1
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui

OBJECTS_DIR = obj
MOC_DIR = moc
INCLUDEPATH += ../../src/qserialdevice


CONFIG(debug, debug|release) {
QMAKE_LIBDIR += ../../src/build/debug
LIBS += -lqserialdeviced
DESTDIR = debug
TARGET = writerw1d
} else {
QMAKE_LIBDIR += ../../src/build/release
LIBS += -lqserialdevice
DESTDIR = release
TARGET = writerw1
}


The following is the mainwindow.cpp code


The following is the mainwindow.cpp code

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

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <abstractserial.h>
#include <iostream>


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

/* Writer
*
* This application is part of the examples on the use of the library QSerialDevice.
*
* writer - a test console application to write data to the port
*
* Copyright (C) 2009 Denis Shienkov
*
* Contact Denis Shienkov:
* e-mail: <scapig2@yandex.ru>
* ICQ: 321789831
*/

using namespace std;

AbstractSerial *port = new AbstractSerial();


/* 1. First - create an instance of an object.
*/


char dn[50]; //device name
cout << "Please enter serial device name, specific by OS, \n example: in Windows -> COMn, in GNU/Linux -> /dev/ttyXYZn: ";
cin >> dn;

/* 2. Second - set the device name.
*/
port->setDeviceName(dn);

/* 3. Third - open the device.

Here, the port is opened with the flag "Unbuffered".
In this case (transfer data) can not use this flag.
*/
if (port->open(AbstractSerial::WriteOnly | AbstractSerial::Unbuffered)) {

qDebug() << "Serial device " << port->deviceName() << " open in " << port->openMode();

//Here, the default current parameters (for example)
qDebug() << "= Default parameters =";
qDebug() << "Device name : " << port->deviceName();
qDebug() << "Baud rate : " << port->baudRate();
qDebug() << "Data bits : " << port->dataBits();
qDebug() << "Parity : " << port->parity();
qDebug() << "Stop bits : " << port->stopBits();
qDebug() << "Flow : " << port->flowControl();

/* 4. Fourth - now you can set the parameters. (after successfully opened port)
*/

//Here example set baud rate 115200 bit/sec (baud)
if (!port->setBaudRate(AbstractSerial::BaudRate115200)) {
qDebug() << "Set baud rate " << AbstractSerial::BaudRate115200 << " error.";
goto label;
};

if (!port->setDataBits(AbstractSerial::DataBits8)) {
qDebug() << "Set data bits " << AbstractSerial::DataBits8 << " error.";
goto label;
}

if (!port->setParity(AbstractSerial::ParityNone)) {
qDebug() << "Set parity " << AbstractSerial::ParityNone << " error.";
goto label;
}

if (!port->setStopBits(AbstractSerial::StopBits1)) {
qDebug() << "Set stop bits " << AbstractSerial::StopBits1 << " error.";
goto label;
}

if (!port->setFlowControl(AbstractSerial::FlowControlOff)) {
qDebug() << "Set flow " << AbstractSerial::FlowControlOff << " error.";
goto label;
}

/*
...
here you can set other parameters.
...
*/

/*
Important Note:

1. For All OS:
If you use buffered mode (ie, at the opening did not put the flag AbstractSerial::Unbuffered),
there is no need to set timeouts reading (Ie they are to remain the default = 0)/
Any value other than 0 will only slow down data acquisition.

2. For Windows:
If you are using unbuffered mode, the timeouts have the effect of reading!
Necessary for the total timeout to set the value of reading At least 1 ms,
or (at 0) will not be read.

PS: I have not figured out yet what the reason.

3. For *.nix:
If you are using unbuffered mode, the timeouts have the effect of reading!
Necessary for the total timeout to set the value of reading At least 100 ms,
as if the value is 0 read will return immediately,
so you can not read the requested number of bytes (ie, reading function can return fewer bytes).

In any case, experiment with options for treatment with buffered/unbuffered,
as well as the timeout values from 0 to N and find the differences. :)
*/


//Here, the new set parameters (for example)
qDebug() << "= New parameters =";
qDebug() << "Device name : " << port->deviceName();
qDebug() << "Baud rate : " << port->baudRate();
qDebug() << "Data bits : " << port->dataBits();
qDebug() << "Parity : " << port->parity();
qDebug() << "Stop bits : " << port->stopBits();
qDebug() << "Flow : " << port->flowControl();

QByteArray ba; //data to send
qint64 bw = 0; //bytes really writed

/* 5. Fifth - you can now read / write device, or further modify its settings, etc.
*/
while (1) {
bw = 0;
cout << "Please enter count bytes for wtitten : ";
cin >> bw;

qDebug() << "Starting writting " << bw << " bytes in time : " << QTime::currentTime();

ba.clear();
ba.resize(bw);

while (bw--) //filling data array
ba[(int)bw] = bw;

bw = port->write(ba);
qDebug() << "Writed is : " << bw << " bytes";
}
}
else {
qDebug() << "Error opened serial device " << port->deviceName();
}

label:

port->close();
qDebug() << "Serial device " << port->deviceName() << " is closed";
delete port;
port = 0;

}

MainWindow::~MainWindow()
{
delete ui;


}

kuzulis
23rd June 2013, 14:29
Hi.

Instead of QSerialDevice please use QtSerialPort: http://qt-project.org/wiki/QtSerialPort,

also see online doc: http://doc-snapshot.qt-project.org/qt5-stable/qtserialport/qtserialport-index.html

sos1
24th June 2013, 09:27
Hi kuzulis,

I would love to but my toolchain will only work up to Qt 4.6, I think QtSerialPort requires Qt 4.8.

Regards,

kuzulis
24th June 2013, 16:05
Yes, 4.8. But it is possible to build with a lower version. You just need replace QElapsedTimer to QTime (or something like that), if I'm not mistaken. Maybe something else will have to fix... by little things.

liuyuejob
9th July 2013, 03:20
you can use qextserialport instead