summaryrefslogtreecommitdiffstats
path: root/tests/consolewaitreader/main.cpp
blob: a06fa3e9183b6d7e92809404db137e491914f275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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();
}