usenet@cps3xx.UUCP (Usenet file owner) (02/01/89)
/*
* Sample code for a simple terminal emulation under Windows.
* This is from my first Windows program--let me know if there's something
* poor in here! (Complete source to entire program not included
* due to size--available upon request.)
* Mark Riordan riordanmr@clvax1.cl.msu.edu 31 Jan 1989
*/
/* This first code fragment sets up the comm port */
int CommDevice;
if(retcode = BuildCommDCB("COM1:2400,n,8",(DCB FAR *) &DCBCom1)) {
sprintf(mesbuf,"BuildComm returned %d",retcode);
MessageBox(hWndConf,mesbuf,"Error building COM1 DCB",MB_OK|MB_ICONEXCLAMATION);
} else {
CommDevice = OpenComm("COM1",1024,1024);
if(CommDevice < 0) {
sprintf(mesbuf,"OpenComm returned %d",CommDevice);
MessageBox(hWndConf,mesbuf,"Error opening COM1",MB_OK|MB_ICONEXCLAMATION);
} else {
SetCommState((DCB FAR *) &DCBCom1);
}
}
/* ------------------------------------------------------------ */
/* This routine manages a window that acts as a terminal screen */
#include "windows.h"
#include "mrr1.h"
#include "newmemos.h"
#include "winundoc.h"
#include "mrr1glob.h"
/****************************************************************************
FUNCTION: Mrr1TermWndProc(HWND, unsigned, WORD, LONG)
****************************************************************************/
long FAR PASCAL Mrr1TermWndProc(hWnd, message, wParam, lParam)
HWND hWnd;
unsigned message;
WORD wParam;
LONG lParam;
{
FARPROC lpProcAbout;
HMENU hMenu;
PAINTSTRUCT ps; /* paint structure */
HDC hDC; /* handle to display context */
char ch, kch;
int ChCount;
char mesbuf[60];
switch (message) {
case WM_SYSCOMMAND:
return (DefWindowProc(hWnd, message, wParam, lParam));
break;
case WM_CREATE:
/* Set the timer for 0.1-second intervals */
idTimer = SetTimer(hWnd, NULL, 100, (FARPROC) NULL);
xPos = SideSpace;
yPos = TopSpace;
break;
case WM_CHAR:
/* Just send any keyboard characters out the Comm port. */
kch = wParam & 0x7f;
WriteComm(CommDevice,(LPSTR) &kch, 1);
break;
case WM_KEYDOWN:
break;
case WM_TIMER:
/* Each timer tick, check to see if any characters have arrived. */
hDC = GetDC(hWnd);
while(ChCount = ReadComm(CommDevice,(LPSTR) &ch,1)) {
ch &= 0x7f;
InterpHostChar(ch);
}
ReleaseDC(hWnd,hDC);
break;
case WM_COMMAND:
switch(wParam) {
case IDM_EXIT:
DestroyWindow(hWnd);
break;
}
break;
case WM_PAINT:
/* Repaint the window from the characters we have saved */
/* in the Lines array. */
hDC = BeginPaint (hWnd, &ps);
X = SideSpace;
Y = TopSpace;
for(il=0; il<MAXLINES; il++) {
TextOut(hDC,X,Y,&(Lines[il][0]),strlen(&(Lines[il][0])));
Y += LineHeight;
}
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
KillTimer(hWnd, idTimer); /* Stops the timer */
NTerminals--;
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (NULL);
}
/*--- Function InterpHostChar ---------------------------------------
* Process a character just received on the Comm port.
* Interpret CR, LF, and NULL, and display all other characters
* as printing characters on the screen.
* Save the character in an array in case we have to repaint.
*/
InterpHostChar(ch)
char ch;
{
if(ch == '\r') {
xPos = SideSpace;
TermCol = 0;
} else if(ch == '\n') {
yPos += LineHeight;
if(++TermRow >= MAXLINES) {
TermRow = 0;
yPos = 0;
}
} else if(ch == '\0') {
/* do nothing */
} else {
Lines[TermRow][TermCol] = ch;
TextOut(hDC,xPos,yPos,&ch,1);
xPos += CharWidth;
TermCol++;
}
}
/* ----------------------------------------------------- */
/*
* Finally, when the application is being terminated, we close
* the comm port.
* Failure to do so will leave it open and unavailable for other apps.
*/
case WM_DESTROY:
CloseComm(CommDevice);