source: projects/punch-card-project/trunk/punch-card-editor/src/driver/documation-m200-client/client.cc @ 59

Last change on this file since 59 was 59, checked in by sven-win, 14 years ago

Umfangreiche Weiterentwicklung (letzte Entwicklung am Netbook). Driver fuer M200 erstmals testweise implementiert; funktioniert(e mit Netbook).

-- Sven @ netboo

File size: 5.0 KB
Line 
1#include "client.h"
2
3#include "qpunchcard/card.h"
4#include "qpunchcard/jones.h"
5
6#include <QDebug>
7#include <QByteArray>
8#include <QRegExp>
9#include <QMutexLocker>
10
11using namespace QPunchCard;
12using namespace Device;
13using namespace DocumationM200;
14
15ClientThread::ClientThread(QObject* parent) : QThread(parent) {
16        abort = false;
17}
18
19ClientThread::~ClientThread() {
20        abort = true; // Quick and dirty, no mutex.
21        qDebug("Waiting for thread to finish.");
22        wait();
23}
24
25void ClientThread::setAbort(bool value) {
26        this->abort = value;
27}
28
29
30void ClientThread::readLine() {
31        QMutexLocker locker(&important_reading);
32
33        // Zeile kriegen
34        char buf[200];
35
36        qint64 len = port.readLine(buf, sizeof(buf));
37        if(len <= 0)
38                return;
39
40        QString text(buf);
41        if(text.count() != 0) {
42                static QRegExp line_regex("^(\\d{3})\\s*([^\\n\\r]*)\\s*$",
43                                          Qt::CaseInsensitive, QRegExp::RegExp2); // greed
44                if(line_regex.indexIn(text) == -1) {
45                        qDebug() << "No match for" << text;
46                        return;
47                }
48                QStringList matches = line_regex.capturedTexts();
49                if(matches.count() != 3) {
50                        qDebug() << "Strange capturing count:" << text << "matched:" << line_regex.capturedTexts();
51                        return;
52                }
53
54                QString code = matches[1];
55                QString comment = matches[2];
56
57                emit recievedResponse(code, comment);
58
59                // schauen, ob eine Karte ankommt
60                if(M200_SERVER_BINARY == code) {
61                        qDebug("Processing card!");
62                        Card card = processCard();
63
64                        qDebug("Well... we got a card. Have fun!");
65                }
66        }
67}
68
69/**
70 * Call this function *exactly* after you recieved readLine() with code == M200_SERVER_BINARY.
71 **/
72Card ClientThread::processCard() {
73        static const int len = 120;
74        char binary[len];
75        int offset = 0;
76
77        // read exactly `len` bytes
78        do {
79                offset += port.read(binary + offset, len - offset);
80                qDebug() << "Read in" << offset << "bytes of" << len << "bytes";
81        } while( offset != len );
82
83        // we got it. No go on encoding, running the algorithm from the JonesFileFormat
84        qDebug() << "Translating data" << QByteArray(binary, len);
85        Card card = JonesFileFormat::jones_bytes_to_card(binary);
86        qDebug() << "Read in card:" << card;
87
88        // uh... thats it
89        return card;
90}
91
92void ClientThread::run() {
93        qDebug("Device::DocumationM200 ClientThread starting");
94
95        // Open Port:
96        port.setPortName("COM4");
97        port.setBaudRate(BAUD38400);
98        port.setFlowControl(FLOW_OFF);
99        port.setDataBits(DATA_8);
100        port.setStopBits(STOP_1);
101        port.setParity(PAR_NONE);
102
103        //port.setTextModeEnabled(true);
104        // => BUG!!!?! Scheint ein Bug in Qt zu sein:
105        //   QIODevice bla;
106        //   bla.setTextModeEnabled(true);
107        //   bla.open(QIODevice::ReadWrite);
108        //   qDebug() << bla.openMode() ===> "Text", should be "ReadOnly|Text|WriteOnly"
109        //   !!!
110
111        qDebug() << "Open port: " << port.open(QIODevice::ReadWrite | QIODevice::Text);
112
113        qDebug() << "Open mode:" << port.openMode();
114
115        port.putChar( M200_CLIENT_BINARY );
116        //qDebug("Printed a char");
117
118        char last_char;
119        forever {
120                // das mag Qt nicht...
121                //QByteArray data = port.readLine( Q_INT64_C(600) );
122                //QString text(data);
123                //qDebug() << "Read in [" << data.count()  <<  "]: " << data;
124
125                /*
126                last_char = (last_char == M200_CLIENT_START) ? M200_CLIENT_STOP : M200_CLIENT_START;
127                port.putChar(last_char);
128
129                port.waitForBytesWritten(1000); // mal lieber, bevor was kaputt geht
130                */
131
132                readLine();
133
134                usleep(50);
135
136                if(abort) {
137                        qDebug("Returning from thread main loop");
138                        return;
139                }
140
141                // noch eine Variante
142                /*
143                qDebug("Wait for answer");
144                while( port.waitForReadyRead(-1) ) {
145                //while( port.bytesAvailable()   ) {
146                        qDebug("GOT OUTPUT!!!");
147                        QString text(port.readAll());
148                        qDebug() << "DATA:" << text;
149                }
150                msleep(1000);
151                if(!port.errorString().isEmpty())
152                        qDebug() << "ERROR:" << port.errorString();
153                */
154
155                // also auf die harte Tour:
156                /*
157                char buf[200];
158                qint64 len = port.readLine(buf, sizeof(buf));
159                if(len != -1) {
160                        QString text(buf);
161                        if(text.count() != 0) {
162                                qDebug() << "Read in [" << text.count() << "]: " << text;
163                        }
164                }
165                */
166
167        } // forever
168}
169
170/**
171 * This can (and must, since there's no Event Loop in this Thread) be called by other
172 * threads directly (Qt::DirectConnection when using Signals and Slots).
173 **/
174void ClientThread::fire_start() {
175        qDebug() << "Firing Startup";
176        QMutexLocker just_a_short_printout(&important_reading);
177
178        port.putChar( M200_CLIENT_START );
179        port.waitForBytesWritten(1000);
180}
181
182void ClientThread::fire_stop() {
183        qDebug() << "Firing Stop";
184        QMutexLocker just_a_short_printout(&important_reading);
185
186        port.putChar( M200_CLIENT_STOP );
187        port.waitForBytesWritten(1000);
188}
189
190void ClientThread::fire_ping() {
191        qDebug() << "Firing Ping";
192        QMutexLocker just_a_short_printout(&important_reading);
193
194        port.putChar( M200_CLIENT_PING );
195        port.waitForBytesWritten(1000);
196}
197
198void ClientThread::fire_reset() {
199        qDebug() << "Firing reset";
200        QMutexLocker just_a_short_printout(&important_reading);
201
202        port.putChar(M200_CLIENT_RESET);
203        port.waitForBytesWritten(1000);
204        qDebug() << "Done.";
205}
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License