# -------------------------------------------------
# Project created by QtCreator 2011-08-29T12:45:21
# -------------------------------------------------
QT += core gui
TARGET = SERIAL
TEMPLATE = app
unix:include(qserialdevice/src/unix/ttylocker.pri)
include(qserialdevice/src/qserialdeviceenumerator/qserialdeviceenumerator.pri)
include(qserialdevice/src/qserialdevice/qserialdevice.pri)
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32 {
LIBS += -lsetupapi -luuid -ladvapi32
}
unix:!macx {
LIBS += -ludev
}
# -------------------------------------------------
# Project created by QtCreator 2011-08-29T12:45:21
# -------------------------------------------------
QT += core gui
TARGET = SERIAL
TEMPLATE = app
unix:include(qserialdevice/src/unix/ttylocker.pri)
include(qserialdevice/src/qserialdeviceenumerator/qserialdeviceenumerator.pri)
include(qserialdevice/src/qserialdevice/qserialdevice.pri)
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32 {
LIBS += -lsetupapi -luuid -ladvapi32
}
unix:!macx {
LIBS += -ludev
}
To copy to clipboard, switch view to plain text mode
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QApplication>
#include <QHBoxLayout>
#include <QPushButton>
#include <QtGui>
#include <QDialog>
#include <QtCore/QObject>
#include <abstractserial.h>
#include <serialdeviceenumerator.h>
namespace Ui {
class MainWindow;
}
class SerialDeviceEnumerator;
class AbstractSerial;
Q_OBJECT
signals:
public:
~MainWindow();
//void setTitle(const QString);
void start(bool enable);
public slots:
void pushButton_Clicked();
void pushButton2_Clicked();
void pushButton3_Clicked();
void printTrace();
protected:
private:
AbstractSerial *port;
Ui::MainWindow *ui;
int responseTimeout;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QApplication>
#include <QHBoxLayout>
#include <QPushButton>
#include <QtGui>
#include <QDialog>
#include <QtCore/QObject>
#include <abstractserial.h>
#include <serialdeviceenumerator.h>
namespace Ui {
class MainWindow;
}
class SerialDeviceEnumerator;
class AbstractSerial;
class MainWindow : public QMainWindow {
Q_OBJECT
signals:
void sendSerialData(const QByteArray &data);
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
//void setTitle(const QString);
void start(bool enable);
public slots:
void pushButton_Clicked();
void pushButton2_Clicked();
void pushButton3_Clicked();
void printTrace();
protected:
void changeEvent(QEvent *e);
private:
AbstractSerial *port;
QTimer *timer;
Ui::MainWindow *ui;
int responseTimeout;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QPushButton>
#include <QtGui>
#include <QDialog>
#include <QMessageBox>
#include <QtCore/QCoreApplication>
#include <abstractserial.h>
#include <serialdeviceenumerator.h>
MainWindow
::MainWindow(QWidget *parent
) :ui(new Ui::MainWindow),responseTimeout(200)//, m_queryLen(MinQueryLen)
{
ui->setupUi(this);
port = new AbstractSerial(this);
connect ( ui->pushButton, SIGNAL( clicked() ), this, SLOT( pushButton_Clicked() ) );
connect ( ui->pushButton_2, SIGNAL( clicked() ), this, SLOT( pushButton2_Clicked() ) );
connect ( ui->pushButton_3, SIGNAL( clicked() ), this, SLOT( pushButton3_Clicked() ) );
this
->ui
->comboBox
->addItem
("/dev/ttyS0",
QVariant::Char);
this
->ui
->comboBox
->addItem
("/dev/ttyS1",
QVariant::Char);
this
->ui
->comboBox
->addItem
("/dev/ttyUSB0",
QVariant::Char);
this
->ui
->comboBox
->addItem
("/dev/ttyACM0",
QVariant::Char);
this->ui->comboBox->setCurrentIndex(0);
timer->setInterval(20); //polling interval
connect(timer, SIGNAL(timeout()), this, SLOT(printTrace()));
}
MainWindow::~MainWindow()
{
delete ui;
port->close();
start(false);
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::pushButton_Clicked()
{ //QMessageBox::information( this, "Information", ui->comboBox->currentText() +" Selected" );
port->setDeviceName(ui->comboBox->currentText());
port->setBaudRate(AbstractSerial::BaudRate115200);
port->setDataBits(AbstractSerial::DataBits8);
port->setParity(AbstractSerial::ParityNone);
port->setStopBits(AbstractSerial::StopBits1);
port->setFlowControl(AbstractSerial::FlowControlOff);
if ( !port->open(AbstractSerial::ReadWrite | AbstractSerial::Unbuffered) )
{
QMessageBox::information( this,
"Information", port
->deviceName
() +" open fail." );
return;
}
else
{
QMessageBox::information( this,
"Information", port
->deviceName
() +" open Successful" );
}
}
void MainWindow::pushButton2_Clicked()
{
data.append(ui->lineEdit->text());
if (data.size() > 0)
{
port->flush();
port->write(data);
this->printTrace();
}
}
void MainWindow::printTrace()
{
port->flush();
if ((port->bytesAvailable() > 0) || port->waitForReadyRead(responseTimeout))
{
s = port->readAll();
ui->textEdit->insertPlainText(s);
}
start(true);
}
void MainWindow::pushButton3_Clicked()
{
port->close();
start(false);
}
void MainWindow::start(bool enable)
{
if (enable)
timer->start();
else
timer->stop();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QPushButton>
#include <QtGui>
#include <QDialog>
#include <QMessageBox>
#include <QtCore/QCoreApplication>
#include <abstractserial.h>
#include <serialdeviceenumerator.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),responseTimeout(200)//, m_queryLen(MinQueryLen)
{
ui->setupUi(this);
port = new AbstractSerial(this);
connect ( ui->pushButton, SIGNAL( clicked() ), this, SLOT( pushButton_Clicked() ) );
connect ( ui->pushButton_2, SIGNAL( clicked() ), this, SLOT( pushButton2_Clicked() ) );
connect ( ui->pushButton_3, SIGNAL( clicked() ), this, SLOT( pushButton3_Clicked() ) );
this->ui->comboBox->addItem("/dev/ttyS0",QVariant::Char);
this->ui->comboBox->addItem("/dev/ttyS1",QVariant::Char);
this->ui->comboBox->addItem("/dev/ttyUSB0",QVariant::Char);
this->ui->comboBox->addItem("/dev/ttyACM0",QVariant::Char);
this->ui->comboBox->setCurrentIndex(0);
timer = new QTimer(this);
timer->setInterval(20); //polling interval
connect(timer, SIGNAL(timeout()), this, SLOT(printTrace()));
}
MainWindow::~MainWindow()
{
delete ui;
port->close();
start(false);
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::pushButton_Clicked()
{ //QMessageBox::information( this, "Information", ui->comboBox->currentText() +" Selected" );
port->setDeviceName(ui->comboBox->currentText());
port->setBaudRate(AbstractSerial::BaudRate115200);
port->setDataBits(AbstractSerial::DataBits8);
port->setParity(AbstractSerial::ParityNone);
port->setStopBits(AbstractSerial::StopBits1);
port->setFlowControl(AbstractSerial::FlowControlOff);
if ( !port->open(AbstractSerial::ReadWrite | AbstractSerial::Unbuffered) )
{
QMessageBox::information( this, "Information", port->deviceName() +" open fail." );
return;
}
else
{
QMessageBox::information( this, "Information", port->deviceName() +" open Successful" );
}
}
void MainWindow::pushButton2_Clicked()
{
QByteArray data;
data.append(ui->lineEdit->text());
if (data.size() > 0)
{
port->flush();
port->write(data);
this->printTrace();
}
}
void MainWindow::printTrace()
{
port->flush();
QByteArray data1;
QString s;
if ((port->bytesAvailable() > 0) || port->waitForReadyRead(responseTimeout))
{
s = port->readAll();
ui->textEdit->insertPlainText(s);
}
start(true);
}
void MainWindow::pushButton3_Clicked()
{
port->close();
start(false);
}
void MainWindow::start(bool enable)
{
if (enable)
timer->start();
else
timer->stop();
}
To copy to clipboard, switch view to plain text mode
Bookmarks