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