AbstractSerialEngine::AbstractSerialEngine(QObject *parent) : QObject(parent), d_ptr(new AbstractSerialEnginePrivate){}
AbstractSerialEngine::AbstractSerialEngine(QObject *parent) : QObject(parent), d_ptr(new AbstractSerialEnginePrivate){}NativeSerialEngine::NativeSerialEngine(QObject *parent) : AbstractSerialEngine(*new NativeSerialEnginePrivate(), parent){}
/*** KFooBase ***/KFooBase::KFooBase(QObject *parent) : QObject(parent), d_ptr(new KFooBasePrivate){} /*** KFooDerived ***/KFooDerived::KFooDerived(QObject *parent) : KFooBase(*new KFooDerivedPrivate, parent){}
C++ (Qt)while (!isEnlightened()) { read(http://techbase.kde.org/Policies/Library_Code_Policy/Shared_D-Pointer_Example);}
...MyDevice->open(QIODevice::ReadOnly | QIODevice::Unbuffered);...
...MyDevice->setParity(AbstractSerial::ParityEven);...
class Reader : public QObject{ Q_OBJECTpublic: Reader(QObject *parent = 0) { MyDevice = new AbstractSerial(); connect(MyDevice, SIGNAL(readyRead()), this, SLOT(slotReadData())); if (!MyDevice->open(QIODevice::ReadOnly | QIODevice::Unbuffered)) { qDebug() << "Open fail"; return; } if (!MyDevice->setParity(AbstractSerial::ParityEven)) { qDebug() << "Set parity fail"; return; } qDebug() << "= Parameters ="; qDebug() << "Device name : " << MyDevice->deviceName(); qDebug() << "Baud rate : " << MyDevice->baudRate(); qDebug() << "Data bits : " << MyDevice->dataBits(); qDebug() << "Parity : " << MyDevice->parity(); qDebug() << "Stop bits : " << MyDevice->stopBits(); qDebug() << "Flow : " << MyDevice->flowControl(); qDebug() << "Char timeout, msec : " << MyDevice->charIntervalTimeout(); } ~Reader() { MyDevice->close(); delete MyDevice; MyDevice = 0; }private slots: void slotReadData() { QByteArray ba = MyDevice->read(10); qDebug() << "Readed " << ba.size() << "bytes"; }private: AbstractSerial *MyDevice;};
C++ (Qt)QString MainWindow::transformInpData(const unsigned char *data, int size){ const QString tab = " "; QString res;for (int i = 0;i < size;i++) res = res+QString("%1").arg((int)data[i],0,16)+tab; return res;}
C++ (Qt)#ifndef MAINWINDOW_H#define MAINWINDOW_H #include <QtGui>#include <QtCore>#include <abstractserial.h>#include "ui_mainwindow.h"#include <iostream> namespace Ui{ class MainWindow;} class MainWindow : public QMainWindow{ Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; AbstractSerial *MyDevice; QTextEdit *log; private slots: void readmsg();}; #endif // MAINWINDOW_H
C++ (Qt)#include "mainwindow.h"#include "ui_mainwindow.h"#include <QtGui>#include <QtDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); log = new QTextEdit(this); log->setReadOnly(true); log->setGeometry(10,10,800,500); log->setStyleSheet( "font-size: 12px;" ); log->show(); MyDevice = new AbstractSerial(); connect(MyDevice, SIGNAL(readyRead()), this, SLOT(readmsg())); MyDevice->setDeviceName("com4"); if (!MyDevice->open(QIODevice::ReadOnly | QIODevice::Unbuffered)) { qDebug() << "Open fail"; return; }if (!MyDevice->setParity(AbstractSerial::ParityEven)) { qDebug() << "Set parity fail"; return; } qDebug() << "= Defaults parameters ="; qDebug() << "Device name : " << MyDevice->deviceName(); qDebug() << "Baud rate : " << MyDevice->baudRate(); qDebug() << "Data bits : " << MyDevice->dataBits(); qDebug() << "Parity : " << MyDevice->parity(); qDebug() << "Stop bits : " << MyDevice->stopBits(); qDebug() << "Flow : " << MyDevice->flowControl(); qDebug() << "Char timeout, msec : " << MyDevice->charIntervalTimeout(); } MainWindow::~MainWindow(){ delete ui;} void MainWindow::readmsg() { qDebug() << "test1"; QString test;QByteArray ba = MyDevice->read(10); qDebug() << "Readed: " << test; log->append("test");}