READER.C 4.87 KB
/*-----------------------------------------------------------------------------

    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;
}