OpenCores
URL https://opencores.org/ocsvn/bw_tiff_compression/bw_tiff_compression/trunk

Subversion Repositories bw_tiff_compression

[/] [bw_tiff_compression/] [trunk/] [client_application/] [src/] [Kernel/] [CSerialport.cpp] - Blame information for rev 16

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 amulder
/*
2
 * @file     Serialport.cpp
3
 * @date     May 14, 2012
4
 * @author   Aart Mulder
5
 */
6
 
7
#include <QDir>
8
#include <QDateTime>
9
#include <QImage>
10
#include <QDebug>
11
 
12
#include "CSerialport.h"
13
 
14
#define NEW_FRAME_CHAR    char('S')
15
 
16
CSerialport::CSerialport() :
17
    CSerialportDevice(10, "SerialportDevice")
18
{
19
    connect(this, SIGNAL(send(QByteArray)), this, SLOT(onSend(QByteArray)));
20
    connect(this, SIGNAL(send(unsigned char)), this, SLOT(onSend(unsigned char)));
21
 
22
    m_nBytesExpected = 0;
23
    m_nBytesReceived = 0;
24
 
25
    m_bTimeoutTimer = false;
26
    m_nTimeoutTime = 10000;
27
    m_sFilename = "";
28
    m_sDir = "";
29
    m_oRxQMutex.unlock();
30
    m_aRxQForGUI.clear();
31
 
32
    this->Start();
33
}
34
 
35
void CSerialport::BeforeClientLoop()
36
{
37
 
38
}
39
 
40
void CSerialport::ClientLoop()
41
{
42
    if(m_bTimeoutTimer)
43
    {
44
        if(m_oRxTimeoutTimer.elapsed() >= m_nTimeoutTime)
45
        {
46
            /* Communication timeout occured */
47
            if(this->m_oCommState == S_WaitForData)
48
            {
49
                handleStreamComplete("", m_sDir);
50
            }
51
 
52
            m_bTimeoutTimer = false;
53
        }
54
    }
55
}
56
 
57
void CSerialport::AfterClientLoop()
58
{
59
 
60
}
61
 
62
void CSerialport::OnDataReceived(quint8 cData)
63
{
64
    handleRxByte(cData);
65
}
66
 
67
void CSerialport::OnDataReceived(quint8 *pData, int nSize)
68
{
69
    for(int i = 0; i < nSize; i++)
70
    {
71
        handleRxByte(pData[i]);
72
    }
73
}
74
 
75
void CSerialport::Send(unsigned char cData)
76
{
77
    emit send(cData);
78
}
79
 
80
void CSerialport::Send(QByteArray aData)
81
{
82
    emit send(aData);
83
}
84
 
85
void CSerialport::onSend(unsigned char cData)
86
{
87
    CSerialportDevice::Send(cData);
88
}
89
 
90
void CSerialport::onSend(QByteArray aData)
91
{
92
    CSerialportDevice::Send((unsigned char*)aData.data(), (unsigned int)aData.size());
93
}
94
 
95
bool CSerialport::Connect(char *sName, quint32 nBaudrate)
96
{
97
    m_oCommState = S_StandBy;
98
    m_bTimeoutTimer = false;
99
 
100
    return CSerialportDevice::Connect(sName, nBaudrate);
101
}
102
 
103
bool CSerialport::Disconnect()
104
{
105
    m_oCommState = S_StandBy;
106
    m_bTimeoutTimer = false;
107
 
108
    return CSerialportDevice::Disconnect();
109
}
110
 
111
bool CSerialport::IsStateStandby()
112
{
113
    return (m_oCommState == S_StandBy ? true : false);
114
}
115
 
116
bool CSerialport::IsStateWaitForData()
117
{
118
    return (m_oCommState == S_WaitForData ? true : false);
119
}
120
 
121
int CSerialport::GetBytesExpected()
122
{
123
    return this->m_nBytesExpected;
124
}
125
 
126
/**
127
 * @brief Reads the number of bytes received.
128
 * @return int with the number of bytes received.
129
 */
130
int CSerialport::GetBytesReceived()
131
{
132
    return this->m_nBytesReceived;
133
}
134
 
135
/**
136
 * @brief Writes the data that has been received since the last call
137
 * of this function into the QByteArray where @e pRxData is pointing to.
138
 * @param pRxData is the pointer to a QByteArray where the received data
139
 * will be stored in.
140
 */
141
void CSerialport::GetNewBytes(QByteArray *pRxData)
142
{
143
    m_oRxQMutex.lock();
144
    pRxData->reserve(m_aRxQForGUI.size());
145
    while(!m_aRxQForGUI.empty())
146
    {
147
        pRxData->append(m_aRxQForGUI.dequeue());
148
    }
149
    m_oRxQMutex.unlock();
150
}
151
 
152
void CSerialport::CancelRequest()
153
{
154
    m_bTimeoutTimer = false;
155
    m_oCommState = S_StandBy;
156
 
157
    m_nBytesReceived = 0;
158
    m_nBytesExpected = 0;
159
 
160
    this->Flush();
161
}
162
 
163
void CSerialport::RequestNewFrame(QString sFilename, QString sDir)
164
{
165
    /* Request of a new frame is only allowed if no request is pending */
166
    if(m_oCommState != S_StandBy)
167
    {
168
        return;
169
    }
170
 
171
    m_sFilename = sFilename;
172
    if(sFilename.length() == 0)
173
    {
174
        m_sFilename = QString("image_") + QDateTime::currentDateTime().toString("yyyyMMdd_hh_mm_ss_zzz") + QString(".tif");
175
    }
176
 
177
    m_sDir = sDir;
178
    if(sDir.length() == 0)
179
    {
180
        m_sDir = QDir::currentPath();
181
    }
182
 
183
    m_nBytesReceived = 0;
184
    m_nBytesExpected = 0;
185
 
186
    this->Flush();
187
 
188
    m_oCommState = S_WaitForData;
189
 
190
    m_oRxTimeoutTimer.restart();
191
    m_bTimeoutTimer = true;
192
 
193
    this->Send(NEW_FRAME_CHAR);
194
}
195
 
196
void CSerialport::handleRxByte(quint8 cData)
197
{
198
    m_oRxTimeoutTimer.restart();
199
    m_bTimeoutTimer = true;
200
 
201
    if(this->m_oCommState != S_WaitForData)
202
    {
203
        /*
204
         * When no frame is requested: only display the received data
205
         * to the user. No processing or storage in the Queue is done.
206
         */
207
        m_oRxQMutex.lock();
208
        m_aRxQForGUI.append(cData);
209
        m_oRxQMutex.unlock();
210
 
211
        return;
212
    }
213
 
214
    switch(m_nBytesReceived)
215
    {
216
    case 0:
217
        m_nBytesExpected = (quint32)cData;
218
        m_aRxBuf.clear();
219
        break;
220
    case 1:
221
        m_nBytesExpected = m_nBytesExpected + ((quint32)cData << 8) + 2;
222
        m_aRxBuf.reserve(m_nBytesExpected);
223
        break;
224
    default:
225
        /* Add all following bytes to the buffer */
226
        m_aRxBuf.append(cData);
227
        break;
228
    }
229
    m_oRxQMutex.lock();
230
    m_aRxQForGUI.append(cData);
231
    m_oRxQMutex.unlock();
232
 
233
    m_nBytesReceived++;
234
 
235
    if(m_nBytesReceived == m_nBytesExpected)
236
    {
237
        handleStreamComplete(m_sFilename, m_sDir);
238
    }
239
}
240
 
241
void CSerialport::handleStreamComplete(QString sFilename, QString sDir)
242
{
243
    QImage *pImage;
244
    QString sFilename2;
245
    QFile file;
246
    QDir oDir;
247
 
248
    if(sFilename.length() == 0)
249
    {
250
        sFilename = QString("image_") + QDateTime::currentDateTime().toString("yyyyMMdd_hh_mm_ss_zzz") + QString(".tif");
251
    }
252
 
253
    if(sDir.length() == 0)
254
    {
255
        sDir = QDir::currentPath();
256
    }
257
 
258
    oDir.setCurrent(sDir);
259
    sFilename2 = oDir.absoluteFilePath(sFilename);
260
 
261
    file.setFileName(sFilename2);
262
 
263
    /*
264
     * Don't create the file if no data has been received
265
     * and turn of the repeat function if enabled.
266
     */
267
    if(m_aRxBuf.size() < 1)
268
    {
269
        m_oCommState = S_StandBy;
270
        m_bTimeoutTimer = false;
271
 
272
        emit showErrorMessage("No data has been received.", true, false);
273
 
274
        return;
275
    }
276
 
277
    if (!file.open(QIODevice::WriteOnly))
278
        return;
279
 
280
    if(m_aRxBuf.size() & 0x1)
281
        m_aRxBuf.append('\0');
282
 
283
    QDataStream dStream(&file);
284
 
285
    m_nImageWidth = 752;
286
    m_nImageHeight = 480;
287
 
288
    /* Write tiff header */
289
    dStream << (quint8)0x4D << (quint8)0x4D << (quint8)0x00 << (quint8)0x2A << (quint8)0x00 << (quint8)0x00;
290
 
291
    /* Write header offset */
292
    dStream << (quint8)(((m_aRxBuf.size() + 8) & 0xFF00) >> 8) << (quint8)(((m_aRxBuf.size() + 8) & 0x00FF) >> 0);
293
 
294
    /* Write stream data */
295
    dStream.writeRawData(m_aRxBuf.data(), m_aRxBuf.size());
296
 
297
    /* Write number of directories */
298
    dStream << (quint8)0x00 << (quint8)0x08;
299
 
300
    /* Write image width */
301
    //Byte 0 to 7
302
    dStream << (quint8)0x01 << (quint8)0x00 << (quint8)0x00 << (quint8)0x03 << (quint8)0x00 << (quint8)0x00 << (quint8)0x00 << (quint8)0x01;
303
    //Byte 8 to 9
304
    dStream << (quint8)((m_nImageWidth & 0xFF00) >> 8) << (quint8)((m_nImageWidth & 0x00FF) >> 0);
305
    //Byte 10 to 11
306
    dStream << (quint8)0x00 << (quint8)0x00;
307
 
308
    /* Write image height */
309
    //Byte 0 to 7
310
    dStream << (quint8)0x01 << (quint8)0x01 << (quint8)0x00 << (quint8)0x03 << (quint8)0x00 << (quint8)0x00 << (quint8)0x00 << (quint8)0x01;
311
    //Byte 8 to 9
312
    dStream << (quint8)((m_nImageHeight & 0xFF00) >> 8) << (quint8)((m_nImageHeight & 0x00FF) >> 0);
313
    //Byte 10 to 11
314
    dStream << (quint8)0x00 << (quint8)0x00;
315
 
316
    /* Write bits per sample */
317
    //Byte 0 to 5
318
    dStream << (quint8)0x01 << (quint8)0x02 << (quint8)0x00 << (quint8)0x03 << (quint8)0x00 << (quint8)0x00;
319
    //Byte 6 to 11
320
    dStream << (quint8)0x00 << (quint8)0x01 << (quint8)0x00 << (quint8)0x01 << (quint8)0x00 << (quint8)0x00;
321
 
322
    /* Write compression */
323
    //Byte 0 to 5
324
    dStream << (quint8)0x01 << (quint8)0x03 << (quint8)0x00 << (quint8)0x03 << (quint8)0x00 << (quint8)0x00;
325
    //Byte 6 to 11
326
    dStream << (quint8)0x00 << (quint8)0x01 << (quint8)0x00 << (quint8)0x04 << (quint8)0x00 << (quint8)0x00;
327
 
328
    /* Write photometric interpretation */
329
    //Byte 0 to 5
330
    dStream << (quint8)0x01 << (quint8)0x06 << (quint8)0x00 << (quint8)0x03 << (quint8)0x00 << (quint8)0x00;
331
    //Byte 6 to 11
332
    dStream << (quint8)0x00 << (quint8)0x01 << (quint8)0x00 << (quint8)0x00 << (quint8)0x00 << (quint8)0x00;
333
 
334
    /* Write strip offset */
335
    //Byte 0 to 5
336
    dStream << (quint8)0x01 << (quint8)0x11 << (quint8)0x00 << (quint8)0x03 << (quint8)0x00 << (quint8)0x00;
337
    //Byte 6 to 11
338
    dStream << (quint8)0x00 << (quint8)0x01 << (quint8)0x00 << (quint8)0x08 << (quint8)0x00 << (quint8)0x08;
339
 
340
    /* Write rows per strip */
341
    //Byte 0 to 5
342
    dStream << (quint8)0x01 << (quint8)0x16 << (quint8)0x00 << (quint8)0x03 << (quint8)0x00 << (quint8)0x00;
343
    //Byte 6 to 7
344
    dStream << (quint8)0x00 << (quint8)0x01;
345
    //Byte 8 to 9
346
    dStream << (quint8)((m_nImageHeight & 0xFF00) >> 8) << (quint8)((m_nImageHeight & 0x00FF) >> 0);
347
    //Byte 10 to 11
348
    dStream << (quint8)0x00 << (quint8)0x00;
349
 
350
    /* Write strip byte count */
351
    //Byte 0 to 5
352
    dStream << (quint8)0x01 << (quint8)0x17 << (quint8)0x00 << (quint8)0x03 << (quint8)0x00 << (quint8)0x00;
353
    //Byte 6 to 7
354
    dStream << (quint8)0x00 << (quint8)0x01;
355
    //Byte 8 to 9
356
    dStream << (quint8)((m_aRxBuf.size() & 0xFF00) >> 8) << (quint8)((m_aRxBuf.size() & 0x00FF) >> 0);
357
    //Byte 10 to 11
358
    dStream << (quint8)0x00 << (quint8)0x00;
359
 
360
    /* Write EOB */
361
    dStream << (quint8)0x00 << (quint8)0x00 << (quint8)0x00 << (quint8)0x00;
362
 
363
    file.close();
364
 
365
    qDebug() << " ";
366
    qDebug() << "=========================================================";
367
    qDebug() << "========== Start decoding of new frame ==================";
368
    qDebug() << "=========================================================";
369
    qDebug() << " ";
370
 
371
    pImage = new QImage(sFilename2);
372
    if(pImage->isNull())
373
    {
374
        m_oCommState = S_StandBy;
375
        m_bTimeoutTimer = false;
376
 
377
        emit showErrorMessage(QString("Failed to open the file: %1").arg(sFilename2), true, false);
378
 
379
        return;
380
    }
381
    delete pImage;
382
 
383
    emit frameCompleted(sFilename2);
384
 
385
    m_oCommState = S_StandBy;
386
    m_bTimeoutTimer = false;
387
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.