READSTAT.C
11 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*-----------------------------------------------------------------------------
This is a part of the Microsoft Source Code Samples.
Copyright (C) 1995 Microsoft Corporation.
All rights reserved.
This source code is only intended as a supplement to
Microsoft Development Tools and/or WinHelp documentation.
See these sources for detailed information regarding the
Microsoft samples programs.
MODULE: ReadStat.c
PURPOSE: Thread procedure responsible for reading comm port,
reporting status events, report status messages,
and periodically updating status controls
FUNCTIONS:
ReaderAndStatusProc - Thread procedure does the work here
-----------------------------------------------------------------------------*/
#include <windows.h>
#include "mttty.h"
#define AMOUNT_TO_READ 512
#define NUM_READSTAT_HANDLES 4
/*-----------------------------------------------------------------------------
FUNCTION: ReaderAndStatusProc(LPVOID)
PURPOSE: Thread function controls comm port reading, comm port status
checking, and status messages.
PARMATERS:
lpV - 4 byte value contains the tty child window handle
RETURN: always 1
COMMENTS: Waits on various events in the applications to handle
port reading, status checking, status messages and modem status.
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
11/21/95 AllenD Incorporated status message heap
-----------------------------------------------------------------------------*/
DWORD WINAPI ReaderAndStatusProc(LPVOID lpV)
{
OVERLAPPED osReader = {0}; // overlapped structure for read operations
OVERLAPPED osStatus = {0}; // overlapped structure for status operations
HANDLE hArray[NUM_READSTAT_HANDLES];
DWORD dwStoredFlags = 0xFFFFFFFF; // local copy of event flags
DWORD dwCommEvent; // result from WaitCommEvent
DWORD dwOvRes; // result from GetOverlappedResult
DWORD dwRead; // bytes actually read
DWORD dwRes; // result from WaitForSingleObject
BOOL fWaitingOnRead = FALSE;
BOOL fWaitingOnStat = FALSE;
BOOL fThreadDone = FALSE;
char lpBuf[AMOUNT_TO_READ];
#ifdef __TOPWELL_EDIT__
char lpBuf_first[AMOUNT_TO_READ];
char Flashed_percent;
#endif
HWND hTTY;
hTTY = (HANDLE) lpV;
//
// create two overlapped structures, one for read events
// and another for status events
//
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osReader.hEvent == NULL)
ErrorInComm("CreateEvent (Reader Event)");
osStatus.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osStatus.hEvent == NULL)
ErrorInComm("CreateEvent (Status Event)");
//
// We want to detect the following events:
// Read events (from ReadFile)
// Status events (from WaitCommEvent)
// Status message events (from our UpdateStatus)
// Thread exit evetns (from our shutdown functions)
//
hArray[0] = osReader.hEvent;
hArray[1] = osStatus.hEvent;
hArray[2] = ghStatusMessageEvent;
hArray[3] = ghThreadExitEvent;
//
// initial check, forces updates
//
CheckModemStatus(TRUE);
CheckComStat(TRUE);
while ( !fThreadDone ) {
//
// If no reading is allowed, then set flag to
// make it look like a read is already outstanding.
//
if (NOREADING( TTYInfo ))
fWaitingOnRead = TRUE;
//
// if no read is outstanding, then issue another one
//
if (!fWaitingOnRead) {
#ifdef __TOPWELL_EDIT__
if (!ReadFile(COMDEV(TTYInfo), lpBuf_first, AMOUNT_TO_READ, &dwRead, &osReader))
#else
if (!ReadFile(COMDEV(TTYInfo), lpBuf, AMOUNT_TO_READ, &dwRead, &osReader))
#endif
{
if (GetLastError() != ERROR_IO_PENDING) // read not delayed?
ErrorInComm("ReadFile in ReaderAndStatusProc");
fWaitingOnRead = TRUE;
}
else { // read completed immediately
if ((dwRead != MAX_READ_BUFFER) && SHOWTIMEOUTS(TTYInfo))
UpdateStatus("Read timed out immediately.\r\n");
if (dwRead)
OutputABuffer(hTTY, lpBuf);
}
}
//
// If status flags have changed, then reset comm mask.
// This will cause a pending WaitCommEvent to complete
// and the resultant event flag will be NULL.
//
if (dwStoredFlags != EVENTFLAGS(TTYInfo)) {
dwStoredFlags = EVENTFLAGS(TTYInfo);
if (!SetCommMask(COMDEV(TTYInfo), dwStoredFlags))
ErrorReporter("SetCommMask");
}
//
// If event checks are not allowed, then make it look
// like an event check operation is outstanding
//
if (NOEVENTS(TTYInfo))
fWaitingOnStat = TRUE;
//
// if no status check is outstanding, then issue another one
//
if (!fWaitingOnStat) {
if (NOEVENTS(TTYInfo))
fWaitingOnStat = TRUE;
else {
if (!WaitCommEvent(COMDEV(TTYInfo), &dwCommEvent, &osStatus)) {
if (GetLastError() != ERROR_IO_PENDING) // Wait not delayed?
ErrorReporter("WaitCommEvent");
else
fWaitingOnStat = TRUE;
}
else
// WaitCommEvent returned immediately
ReportStatusEvent(dwCommEvent);
}
}
//
// wait for pending operations to complete
//
if ( fWaitingOnStat && fWaitingOnRead ) {
dwRes = WaitForMultipleObjects(NUM_READSTAT_HANDLES, hArray, FALSE, STATUS_CHECK_TIMEOUT);
switch(dwRes)
{
//
// read completed
//
case WAIT_OBJECT_0:
if (!GetOverlappedResult(COMDEV(TTYInfo), &osReader, &dwRead, FALSE))
{
if (GetLastError() == ERROR_OPERATION_ABORTED)
UpdateStatus("Read aborted\r\n");
else
ErrorInComm("GetOverlappedResult (in Reader)");
}
else { // read completed successfully
if ((dwRead != MAX_READ_BUFFER) && SHOWTIMEOUTS(TTYInfo))
UpdateStatus("Read timed out overlapped.\r\n");
#ifdef __TOPWELL_EDIT__
if (dwRead)
{
int loop1, loop2;
loop2 = 0;
for (loop1 = 0; loop1 < AMOUNT_TO_READ; loop1++)
{
if (lpBuf_first[loop1] < 0x0A || lpBuf_first[loop1] >= 0x7f)
continue;
else
lpBuf[loop2++] = lpBuf_first[loop1];
}
lpBuf[loop2++] = '\r';
lpBuf[loop2++] = '\n';
lpBuf[loop2] = 0;
if (strstr(lpBuf, "ROM boot"))
{
if (GetTickCount() -TTYInfo.tickcountFinish < 2000)
{ // 在很短的时间内收到PCB板的下载信号, 视为PCB板自动复位, 而不是合上夹具
return 0;
}
TTYInfo.rgbBGColor = RGB(255, 255, 255);
OutputABuffer(hTTY, "进入下载模式\r\n");
TTYInfo.DownloadReday = TRUE;
if (strlen(TTYInfo.szFileName) != 0)
TransferFileTextStart(TTYInfo.szFileName);
else
{
TTYInfo.rgbBGColor = RGB(255, 201, 14);
OutputABuffer(hTTY, "还未选择软件\r\n");
return 0;
}
}
else if (strstr(lpBuf, "Verify successful"))
{
TTYInfo.tickcountFinish = GetTickCount();
TTYInfo.rgbBGColor = RGB(50, 255, 50);
OutputABuffer(hTTY, "下载完成\r\n");
TTYInfo.DownloadReday = FALSE;
}
else if (strstr(lpBuf, "Programming:"))
{
char * pchar_percent = strstr(lpBuf, "Programming:");
char ascbufpercent[64];
if (pchar_percent != NULL)
{
TTYInfo.tickcountFinish = GetTickCount();
TTYInfo.rgbBGColor = RGB(255, 255, 255);
memset(ascbufpercent, 0, 64);
strncpy(ascbufpercent, pchar_percent + 12, 2);
Flashed_percent = atoi(ascbufpercent);
sprintf(ascbufpercent, "下载已完成%d\r\n", Flashed_percent);
OutputABuffer(hTTY, ascbufpercent);
TTYInfo.DownloadReday = FALSE;
}
}
else if (strstr(lpBuf, "Verify fail"))
{
//TTYInfo.tickcountFinish = GetTickCount();
TTYInfo.rgbBGColor = RGB(255, 50, 50);
OutputABuffer(hTTY, "下载失败\r\n");
TTYInfo.DownloadReday = FALSE;
}
else
{
TTYInfo.rgbBGColor = RGB(255, 255, 255);
OutputABuffer(hTTY, lpBuf);
TTYInfo.DownloadReday = FALSE;
}
}
#else
if (dwRead)
OutputABuffer(hTTY, lpBuf);
#endif
}
fWaitingOnRead = FALSE;
break;
//
// status completed
//
case WAIT_OBJECT_0 + 1:
if (!GetOverlappedResult(COMDEV(TTYInfo), &osStatus, &dwOvRes, FALSE))
{
if (GetLastError() == ERROR_OPERATION_ABORTED)
UpdateStatus("WaitCommEvent aborted\r\n");
else
ErrorInComm("GetOverlappedResult (in Reader)");
}
else // status check completed successfully
ReportStatusEvent(dwCommEvent);
fWaitingOnStat = FALSE;
break;
//
// status message event
//
case WAIT_OBJECT_0 + 2:
StatusMessage();
break;
//
// thread exit event
//
case WAIT_OBJECT_0 + 3:
fThreadDone = TRUE;
break;
case WAIT_TIMEOUT:
//
// timeouts are not reported because they happen too often
// OutputDebugString("Timeout in Reader & Status checking\n\r");
//
//
// if status checks are not allowed, then don't issue the
// modem status check nor the com stat check
//
if (!NOSTATUS(TTYInfo)) {
CheckModemStatus(FALSE); // take this opportunity to do
CheckComStat(FALSE); // a modem status check and
// a comm status check
}
break;
default:
ErrorReporter("WaitForMultipleObjects(Reader & Status handles)");
break;
}
}
}
//
// close event handles
//
CloseHandle(osReader.hEvent);
CloseHandle(osStatus.hEvent);
return 1;
}