READER.C 4.81 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 (TTYInfo.nColumn > 0)
					TTYInfo.nColumn--;
				break;
			case ASCII_CR: // Carriage Return
				TTYInfo.nColumn = 0;
				if (!TTYInfo.fNewLine)
					break;

				//
				// FALL THROUGH
				//
			case ASCII_LF: // Line Feed
				if (TTYInfo.nRow++ == MAXROWS - 1)
				{
					MoveMemory((LPSTR) (TTYInfo.Screen), 
						(LPSTR) (TTYInfo.Screen + MAXCOLS), 
						(MAXROWS - 1) *MAXCOLS);
					FillMemory((LPSTR) (TTYInfo.Screen + (MAXROWS - 1) *MAXCOLS), 
						MAXCOLS, ' ');
					InvalidateRect(hTTY, NULL, FALSE);
					TTYInfo.nRow--;
				}
				break;
			default: // standard character
				TTYInfo.Screen[TTYInfo.nRow * MAXCOLS + TTYInfo.nColumn] = lpBuf[i];
				rect.left = (TTYInfo.nColumn * TTYInfo.xChar) -TTYInfo.xOffset;
				rect.right = rect.left + TTYInfo.xChar;
				rect.top = (TTYInfo.nRow * TTYInfo.yChar) -TTYInfo.yOffset;
				rect.bottom = rect.top + TTYInfo.yChar;
				InvalidateRect(hTTY, &rect, FALSE);
 				// Line wrap
				if (TTYInfo.nColumn < MAXCOLS - 1)
					TTYInfo.nColumn++;
				else if (TTYInfo.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)
{
    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;
}