Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
270 johboh 1
#include "SerialReader.h"
2
 
3
#include <assert.h>
4
 
5
//void arrayCopy(unsigned char *src,unsigned int srcIndex,unsigned char *dst,unsigned int dstIndex,unsigned int length);
6
 
7
//bool queueEnqueue(CanProtoMessage cpm);
8
//bool queueDequeue(CanProtoMessage *cpm);
9
/*
10
CanProtoMessage cpmQueue[QUEUE_PLACES];
11
int queueReadPointer=0;
12
int queueWritePointer=0;
13
int queueLength=0;
14
 
15
static CSerial * serial;
16
 
17
static SerialConnection * sc;
18
 
19
static HANDLE  hRunMutex;                   // "Keep Running" mutex
20
*/
21
void SerRead(void *pMyID)
22
{
23
 
24
    unsigned char iBuff[SERIAL_READ_BUFFER_SIZE];
25
    unsigned int iBuffPointer = 0;
26
 
27
 
28
    long lLastError = 0;
29
 
30
    /*
31
    // Register only for the receive event
32
    lLastError = serial->SetMask(CSerial::EEventRecv);
33
    if (lLastError != ERROR_SUCCESS)
34
    {
35
        printf("Err! Unable to set COM-port event mask.\n");
36
        return;
37
    }
38
 
39
    // Use 'non-blocking' reads, because we don't know how many bytes
40
    // will be received. This is normally the most convenient mode
41
    // (and also the default mode for reading data).
42
    lLastError = serial->SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);
43
    if (lLastError != ERROR_SUCCESS)
44
    {
45
        printf("Err! Unable to set COM-port read timeout.\n");
46
        return;
47
    }
48
*/
49
    /*
50
    if (SetCommMask(sc->hCom,EV_RXCHAR))
51
    {
52
        // Handle the error.
53
        printf("SetCommMask failed with error %d.\n", GetLastError());
54
        return;
55
    }
56
 
57
        // Create an event object for use by WaitCommEvent.
58
 
59
    OVERLAPPED o;
60
    DWORD dwEvtMask;
61
 
62
 
63
 
64
    o.hEvent = CreateEvent(
65
        NULL,   // default security attributes
66
        TRUE,   // manual-reset event
67
        FALSE,  // not signaled
68
        NULL    // no name
69
        );
70
 
71
 
72
    // Intialize the rest of the OVERLAPPED structure to zero.
73
    o.Internal = 0;
74
    o.InternalHigh = 0;
75
    o.Offset = 0;
76
    o.OffsetHigh = 0;
77
 
78
    */
79
 
80
        DWORD dwCommEvent;
81
        DWORD dwRead;
82
        char  chRead;
83
/*
84
        if (sc->getHandle() ==0)
85
        {
86
            printf("Serial port closed.");
87
            return;
88
        }
89
*/
90
        /*
91
        if (!SetCommMask(sc->getHandle(), EV_RXCHAR))
92
        {
93
            // Handle the error.
94
            printf("SetCommMask failed with error %d.\n", GetLastError());
95
            return;
96
        }
97
 
98
 
99
    while ( WaitForSingleObject( hRunMutex, 75L ) == WAIT_TIMEOUT )
100
    {
101
 
102
        if (WaitCommEvent(sc->getHandle(), &dwCommEvent, NULL))
103
        {
104
            if (ReadFile(sc->getHandle(), &chRead, 1, &dwRead, NULL))
105
            {
106
                printf("byte receiced\n");
107
            } else {printf("Error1\n"); break;}
108
        } else {printf("Error2\n"); break;}
109
 
110
 
111
 
112
        /*
113
        if (WaitCommEvent(sc->hCom, &dwEvtMask, &o))
114
        {
115
            if (dwEvtMask & EV_RXCHAR)
116
            {
117
                printf("RX\n");
118
            }
119
        }
120
        else
121
        {
122
            DWORD dwRet = GetLastError();
123
            if( ERROR_IO_PENDING == dwRet)
124
            {
125
                printf("I/O is pending...\n");
126
 
127
                // To do.
128
            }
129
            //else printf("Wait failed with error %d.\n", GetLastError());
130
        }*/
131
    //}
132
 
133
 
134
    return;
135
/*
136
    while(1)
137
    {
138
 
139
        // Wait for an event
140
        lLastError = serial->WaitEvent();
141
        if (lLastError != ERROR_SUCCESS)
142
        {
143
            printf("Err! Unable to wait for a COM-port event.\n");
144
            break;
145
        }
146
 
147
        // Save event
148
        const CSerial::EEvent eEvent = serial->GetEventType();
149
 
150
 
151
        // Handle data receive event
152
        if (eEvent & CSerial::EEventRecv)
153
        {
154
 
155
            // Read data, until there is nothing left
156
            DWORD dwBytesRead = 0;
157
            unsigned char szBuffer[SERIAL_INPUT_BUFFER_SIZE];
158
            do
159
            {
160
                // Read data from the COM-port
161
                lLastError = serial->Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead);
162
                if (lLastError != ERROR_SUCCESS)
163
                {
164
                    printf("Unable to read from COM-port.");
165
                    return;
166
                }
167
 
168
                if (dwBytesRead > 0)
169
                {
170
 
171
 
172
 
173
                    // Spara i array, sist.
174
                    arrayCopy(szBuffer, 0, iBuff, iBuffPointer, dwBytesRead);
175
                    iBuffPointer += dwBytesRead;
176
 
177
                    // Följ parserutin.
178
                    // Köa.
179
                    // flytta.
180
 
181
                     // När array större eller lika med PACKET_LENGTH...
182
                    while (iBuffPointer >= PACKET_LENGTH)
183
                    {
184
                        int startIndex = 0;
185
 
186
                        // Sök igenom efter start byte från början.
187
                        for (unsigned int i = 0; i < iBuffPointer; i++)
188
                        {
189
                            // Poppa alla bytes som inte är startbyte.
190
                            if (iBuff[i] != UART_START_BYTE) startIndex++;
191
                            else
192
                            {
193
                                // När startbyte hittas, kolla om återstående längd är större eller lika med PACKET_LENGTH (inkl startbyte)
194
                                if ((iBuffPointer - startIndex) >= PACKET_LENGTH)
195
                                {
196
                                    //om så, kolla PACKET_LENGTH-1 byte fram.
197
                                    if (iBuff[startIndex + PACKET_LENGTH - 1] == UART_END_BYTE)
198
                                    {
199
                                        // Om byte PACKET_LENGTH-1 är slutbyte så extraktas startIndex till slutbyteindex.
200
                                        CanProtoMessage cmp(iBuff,startIndex + 1);
201
                                        queueEnqueue(cmp);
202
 
203
 
204
                                        // Sätt ny startindex och avsluta loop.
205
                                        startIndex += PACKET_LENGTH;
206
                                        break;
207
                                    }
208
                                }
209
                            }
210
                        }
211
                        // och i slutet göra en array copy.
212
                        // Flytta ner allt efter slutbyte till index 0 i array.
213
                        arrayCopy(iBuff, startIndex, iBuff, 0, iBuffPointer - PACKET_LENGTH);
214
                        iBuffPointer -= startIndex;
215
                    }
216
 
217
                }
218
            }while (dwBytesRead == sizeof(szBuffer)-1);
219
        }
220
 
221
    }
222
*/
223
    return;
224
}
225
 
226
/*
227
void serialReaderStart(CSerial * ser)
228
{
229
    serial = ser;
230
    hRunMutex = CreateMutex( NULL, TRUE, NULL );      // Set
231
    _beginthread( SerRead, 0, 0 );
232
}
233
 
234
void serialReaderStartSerCon(SerialConnection * ser)
235
{
236
    sc = ser;
237
    hRunMutex = CreateMutex( NULL, TRUE, NULL );      // Set
238
    _beginthread( SerRead, 0, 0 );
239
}
240
 
241
void serialReaderStop( void )
242
{
243
    ReleaseMutex( hRunMutex );
244
    CloseHandle( hRunMutex );
245
}
246
*/
247
 
248
 
249
 
250
//bool getMessage(CanProtoMessage *cpm)
251
//{
252
//  return queueDequeue(cpm);
253
//}
254
/*
255
void arrayCopy(unsigned char *src,unsigned int srcIndex,unsigned char *dst,unsigned int dstIndex,unsigned int length)
256
{
257
    // Copy an aray to another array (of unsigned chars == bytes)
258
    for(unsigned int i=0;i<length;i++) dst[dstIndex+i]=src[srcIndex+i];
259
}
260
*/
261
/*
262
bool queueEnqueue(CanProtoMessage cpm)
263
{
264
    // Enqueue reveiced message to queue
265
    if (queueLength>=QUEUE_PLACES) return false;
266
 
267
    cpmQueue[queueWritePointer]=cpm;
268
 
269
    queueLength++;
270
    queueWritePointer++;
271
    if (queueWritePointer>=QUEUE_PLACES) queueWritePointer=0;
272
 
273
    printf("[%ld] Queue message, length: %d\n",GetTickCount(),queueLength);
274
 
275
    return true;
276
}
277
bool queueDequeue(CanProtoMessage *cpm)
278
{
279
    // Dequeue can message.
280
    if (queueLength==0) return false;
281
 
282
    cpm = &cpmQueue[queueReadPointer];
283
 
284
    queueLength--;
285
    queueReadPointer++;
286
    if (queueReadPointer>=QUEUE_PLACES) queueReadPointer=0;
287
 
288
    return true;
289
}*/