summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDenis Shienkov <scapig2@yandex.ru>2011-09-21 20:12:53 +0400
committerDenis Shienkov <scapig2@yandex.ru>2011-09-21 20:12:53 +0400
commit70d2cc3157302591c3b5fca14c227bd759abdda3 (patch)
tree2a41dc261a288c87442e9d7033cd9903ca99ec14 /tests
parent30d75a0b90dd9512b4885548b0bac53415f4e26b (diff)
Added some test console applications.
Diffstat (limited to 'tests')
-rw-r--r--tests/consolewaitreader/consolewaitreader.pro25
-rw-r--r--tests/consolewaitreader/main.cpp89
-rw-r--r--tests/consolewriter/consolewriter.pro25
-rw-r--r--tests/consolewriter/main.cpp81
4 files changed, 220 insertions, 0 deletions
diff --git a/tests/consolewaitreader/consolewaitreader.pro b/tests/consolewaitreader/consolewaitreader.pro
new file mode 100644
index 00000000..44182165
--- /dev/null
+++ b/tests/consolewaitreader/consolewaitreader.pro
@@ -0,0 +1,25 @@
+TEMPLATE = app
+CONFIG += console
+QT -= gui
+OBJECTS_DIR = obj
+MOC_DIR = moc
+
+INCLUDEPATH += \
+ ../../include \
+ ../../src
+
+HEADERS += \
+ ../../include/serialport.h \
+ ../../include/serialportinfo.h
+
+include(../../src/src.pri)
+
+SOURCES += main.cpp
+
+CONFIG(debug, debug|release) {
+ DESTDIR = debug
+ TARGET = consolewaitreaderd
+} else {
+ DESTDIR = release
+ TARGET = consolewaitreader
+}
diff --git a/tests/consolewaitreader/main.cpp b/tests/consolewaitreader/main.cpp
new file mode 100644
index 00000000..a06fa3e9
--- /dev/null
+++ b/tests/consolewaitreader/main.cpp
@@ -0,0 +1,89 @@
+/*
+* ConsoleWaitReader
+*
+* This application is part of the examples on the use of the library QSerialDevice.
+*
+* ConsoleWaitReader - a test console application to read data from the port using the method
+* of expectations waitForReadyRead().
+*
+* Copyright (C) 2011 Denis Shienkov
+*
+* Contact Denis Shienkov:
+* e-mail: <scapig2@yandex.ru>
+* ICQ: 321789831
+*/
+
+#include <iostream>
+#include <QtCore/QCoreApplication>
+
+#include "serialport.h"
+
+int main(int argc, char *argv[])
+{
+ QCoreApplication app(argc, argv);
+
+ // 1. First - create an instance of an object.
+ SerialPort port;
+
+ char inbuf[30];
+
+ std::cout << "Please enter serial device name,\n"
+ "specific by OS, example\n"
+ "- in Windows: COMn\n"
+ "- in GNU/Linux: ttyXYZn\n"
+ ":";
+ std::cin >> inbuf;
+
+ // 2. Second - set the device name.
+ port.setPort(QString(inbuf));
+
+ std::cout << "The port will be opened in read-only mode (QIODevice::ReadOnly).\n"
+ "But you can choose to buffered or not (QIODevice::Unbuffered).\n"
+ "To understand what is the difference - try to change these modes!\n"
+ "Disable buffering [y/N] ?:";
+ std::cin >> inbuf;
+
+ QIODevice::OpenMode mode = QIODevice::ReadOnly;
+ if (inbuf[0] == 'y')
+ mode |= QIODevice::Unbuffered;
+
+ // 3. Third - open the device.
+ if (port.open(mode)) {
+
+ // 4. Fourth - now you can configure it (only after successfully opened!).
+ if (port.setRate(115200) && port.setDataBits(SerialPort::Data8)
+ && port.setParity(SerialPort::NoParity) && port.setStopBits(SerialPort::OneStop)
+ && port.setFlowControl(SerialPort::NoFlowControl)) {
+
+ int msecs = 0;
+ std::cout << "Please enter wait timeout for ready read, msec: ";
+ std::cin >> msecs;
+
+ int len = 0;
+ std::cout << "Please enter len data for read, bytes: ";
+ std::cin >> len;
+
+ // 5. Fifth - you can now read/write device, or further modify its settings, etc.
+ while (1) {
+
+ std::cout << "Now starting wait data ..." << std::endl;
+
+ if ((port.bytesAvailable() > 0) || port.waitForReadyRead(msecs)) {
+
+ QByteArray data = port.read(len);
+
+ std::cout << "Readed " << data.size() << " bytes" << std::endl;
+ } else {
+ std::cout << "Wait timeout expired." << std::endl;
+ }
+ }
+ } else {
+ std::cout << "Configure " << port.portName().toLocal8Bit().constData() << " fail.";
+ port.close();
+ }
+ } else {
+ std::cout << "Open " << port.portName().toLocal8Bit().constData() << " fail.";
+ }
+
+ return app.exec();
+}
diff --git a/tests/consolewriter/consolewriter.pro b/tests/consolewriter/consolewriter.pro
new file mode 100644
index 00000000..335d92c7
--- /dev/null
+++ b/tests/consolewriter/consolewriter.pro
@@ -0,0 +1,25 @@
+TEMPLATE = app
+CONFIG += console
+QT -= gui
+OBJECTS_DIR = obj
+MOC_DIR = moc
+
+INCLUDEPATH += \
+ ../../include \
+ ../../src
+
+HEADERS += \
+ ../../include/serialport.h \
+ ../../include/serialportinfo.h
+
+include(../../src/src.pri)
+
+SOURCES += main.cpp
+
+CONFIG(debug, debug|release) {
+ DESTDIR = debug
+ TARGET = consolewriterd
+} else {
+ DESTDIR = release
+ TARGET = consolewriter
+}
diff --git a/tests/consolewriter/main.cpp b/tests/consolewriter/main.cpp
new file mode 100644
index 00000000..e2e47d15
--- /dev/null
+++ b/tests/consolewriter/main.cpp
@@ -0,0 +1,81 @@
+/*
+* ConsoleWriter
+*
+* This application is part of the examples on the use of the library QSerialDevice.
+*
+* ConsoleWriter - a test console application to write data to the port.
+*
+* Copyright (C) 2011 Denis Shienkov
+*
+* Contact Denis Shienkov:
+* e-mail: <scapig2@yandex.ru>
+* ICQ: 321789831
+*/
+
+#include <iostream>
+#include <QtCore/QCoreApplication>
+
+#include "serialport.h"
+
+int main(int argc, char *argv[])
+{
+ QCoreApplication app(argc, argv);
+
+ // 1. First - create an instance of an object.
+ SerialPort port;
+
+ char inbuf[30];
+
+ std::cout << "Please enter serial device name,\n"
+ "specific by OS, example\n"
+ "- in Windows: COMn\n"
+ "- in GNU/Linux: ttyXYZn\n"
+ ":";
+ std::cin >> inbuf;
+
+ // 2. Second - set the device name.
+ port.setPort(QString(inbuf));
+
+ std::cout << "The port will be opened in write-only mode (QIODevice::WriteOnly).\n"
+ "But you can choose to buffered or not (QIODevice::Unbuffered).\n"
+ "To understand what is the difference - try to change these modes!\n"
+ "Disable buffering [y/N] ?:";
+ std::cin >> inbuf;
+
+ QIODevice::OpenMode mode = QIODevice::WriteOnly;
+ if (inbuf[0] == 'y')
+ mode |= QIODevice::Unbuffered;
+
+ // 3. Third - open the device.
+ if (port.open(mode)) {
+
+ // 4. Fourth - now you can configure it (only after successfully opened!).
+ if (port.setRate(115200) && port.setDataBits(SerialPort::Data8)
+ && port.setParity(SerialPort::NoParity) && port.setStopBits(SerialPort::OneStop)
+ && port.setFlowControl(SerialPort::NoFlowControl)) {
+
+ // 5. Fifth - you can now read/write device, or further modify its settings, etc.
+ while (1) {
+
+ int len = 0;
+ std::cout << "Please enter len data for write, bytes: ";
+ std::cin >> len;
+
+ QByteArray data(len, 0);
+
+ if (port.write(data)) {
+ std::cout << "Writed " << data.size() << " bytes" << std::endl;
+ } else {
+ std::cout << "Write fail." << std::endl;
+ }
+ }
+ } else {
+ std::cout << "Configure " << port.portName().toLocal8Bit().constData() << " fail.";
+ port.close();
+ }
+ } else {
+ std::cout << "Open " << port.portName().toLocal8Bit().constData() << " fail.";
+ }
+
+ return app.exec();
+}