READER.C
4.87 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
/*-----------------------------------------------------------------------------
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: Reader.c
PURPOSE: Read from comm port
FUNCTIONS:
OutputABufferToWindow - 传入的数据显示到窗口
OutputABufferToFile - 传入的数据保存到文件
OutputABuffer - 从端口读取数据时调用
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <commctrl.h>
#include "MTTTY.h"
/*
Prototypes for functions call only within this file
*/
void OutputABufferToFile( HANDLE, char *, DWORD );
/*-----------------------------------------------------------------------------
FUNCTION: OutputABufferToWindow(HWND, char *, DWORD)
PURPOSE: 用刚刚收到的字符更新TTY缓冲区。
PARAMETERS:
hTTY - handle to the TTY child window
lpBuf - address of data buffer
dwBufLen - size of data buffer
HISTORY: Date Author Comment
5/ 8/91 BryanW Wrote it
10/27/95 AllenD Modified for MTTTY Sample
-----------------------------------------------------------------------------*/
void OutputABufferToWindow(HWND hTTY, char * lpBuf, DWORD dwBufLen)
{
RECT rect;
//更新屏幕缓冲区与新的缓冲区 需要一个字符一个字符地检查 为特殊字符
int i;
for (i = 0; i < dwBufLen; i++)
{
switch (lpBuf[i])
{
case ASCII_BEL: // BELL CHAR
MessageBeep(0);
break;
case ASCII_BS: // Backspace CHAR
if (g_appdata.nColumn > 0)
g_appdata.nColumn--;
break;
case ASCII_CR: // Carriage Return
g_appdata.nColumn = 0;
if (!g_appdata.fNewLine)
break;
//
// FALL THROUGH
//
case ASCII_LF: // Line Feed
if (g_appdata.nRow++ == MAXROWS - 1)
{
MoveMemory((LPSTR) (g_appdata.Screen),
(LPSTR) (g_appdata.Screen + MAXCOLS),
(MAXROWS - 1) *MAXCOLS);
FillMemory((LPSTR) (g_appdata.Screen + (MAXROWS - 1) *MAXCOLS),
MAXCOLS, ' ');
InvalidateRect(hTTY, NULL, FALSE);
g_appdata.nRow--;
}
break;
default: // standard character
g_appdata.Screen[g_appdata.nRow * MAXCOLS + g_appdata.nColumn] = lpBuf[i];
rect.left = (g_appdata.nColumn * g_appdata.xChar) -g_appdata.xOffset;
rect.right = rect.left + g_appdata.xChar;
rect.top = (g_appdata.nRow * g_appdata.yChar) -g_appdata.yOffset;
rect.bottom = rect.top + g_appdata.yChar;
InvalidateRect(hTTY, &rect, FALSE);
// Line wrap
if (g_appdata.nColumn < MAXCOLS - 1)
g_appdata.nColumn++;
else if (g_appdata.fAutowrap)
OutputABufferToWindow(hTTY, "\r\n", 2);
break;
}
}
MoveTTYCursor(hTTY);
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: OutputABufferToFile(HANDLE, char *, DWORD)
PURPOSE: 将已读取的缓冲区输出到文件中
PARAMETERS:
hFile - 文件的句柄保存数据到
lpBuf - address of data buffer
dwBufLen - size of data buffer
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void OutputABufferToFile(HANDLE hFile, char * lpBuf, DWORD dwBufLen)
{
DWORD dwWritten;
//
// place buffer into file, report any errors
//
if (!WriteFile(hFile, lpBuf, dwBufLen, &dwWritten, NULL))
ErrorReporter("WriteFile in file capture");
if (dwBufLen != dwWritten)
ErrorReporter("WriteFile");
//
// update transfer progress bar
//
PostMessage(GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS), PBM_STEPIT, 0, 0);
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: OutputABuffer(HWND, char *, DWORD)
PURPOSE: Send a rec'd buffer to the approprate location
PARAMETERS:
hTTY - handle to the TTY child window
lpBuf - address of data buffer
dwBufLen - size of data buffer
COMMENTS: If buffer is 0 length, then do nothing.
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void OutputABuffer(HWND hTTY, char * lpBuf)
{
DWORD dwBufLen =strlen(lpBuf);
if (dwBufLen == 0) {
OutputDebugString("NULL Buffer in OutputABuffer\n\r");
return;
}
switch(gdwReceiveState)
{
case RECEIVE_TTY:
OutputABufferToWindow(hTTY, lpBuf, dwBufLen);
break;
case RECEIVE_CAPTURED:
OutputABufferToFile(ghFileCapture, lpBuf, dwBufLen);
break;
default:
OutputDebugString("Unknown receive state!\n\r");
}
return;
}