home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 3 Comm
/
03-Comm.zip
/
STERM.ZIP
/
STERMC.C
< prev
next >
Wrap
Text File
|
1991-01-14
|
6KB
|
219 lines
/* STERMC.C
Handle COM port access, open the device and
set parms to currently stored values */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <process.h>
#define INCL_SUB
#define INCL_DOSDEVICES
#include <os2kernl.h>
#define STERMC
#include "sterm.h"
int setport( PortDef *pd )
{
USHORT iostat; /* status from kernl call */
USHORT action; /* Open retcode */
USHORT openflag = 0x01; /* fail if dvc not avail */
USHORT openmode = 0x12; /* read/write, deny others */
/* open device for access */
if ( DosOpen( pd->cport, (PUSHORT) &pd->chandle,
(PUSHORT) &action,
0L, 0, openflag, openmode, 0L ) )
errexit( "Can't Open COM device." );
/* get serial port DCB */
iostat = DosDevIOCtl( (PVOID) &pd->ldcb, NULL, 0x73, 1, pd->chandle );
if ( iostat != 0 )
errexit( "Can't get port DCB." );
/* request DTR control mode */
pd->ldcb.enflag = enable_DTR;
/* set serial port DCB */
iostat = DosDevIOCtl( NULL, (PVOID) &pd->ldcb, 0x53, 1, pd->chandle );
if ( iostat != 0 )
errexit( "Can't set port DCB." );
/* set baud rate */
if ( DosDevIOCtl( NULL, (PVOID) &pd->lspeed, 0x41, 1, pd->chandle ) )
errexit( "Can't set baud rate." );
/* init serial port */
if ( DosDevIOCtl( NULL, (PVOID) &pd->ltype, 0x42, 1, pd->chandle ) )
errexit( "Can't configure COM port." );
return ( 0 );
} /* end setport */
#define NUL '\x00' /* null byte */
#define ESC '\x1B' /* leadin to control seq. */
#define LEADIN ";01H\x1B[0m"
#define LEADL 8 /* length of leadin */
static USHORT sqp = 0; /* index to escseq */
static char escsqc[10];
static BOOL havleadin = FALSE;
static USHORT bcount = 0;
static FILE *plog = NULL; /* port log file */
/* Read from the serial port
Block on input from the serial port and a terminate/semaphore. */
void comin( void )
{
/* characters which IBM wraps its lines in */
#define DC1 0x11
#define DC3 0x13
#define CDEL 0x7f
USHORT rlen;
BYTE inchar;
MsgH outw;
USHORT cip; /* store ptr */
outw.mtype = immed; /* set for terminal mode */
outw.mlen = 1; /* char at a time */
cip = 0;
memset( (void *) compbuf, 0, WSLEN );
DosSemWait( (HSEM) &readsem, -1L );
while ( TRUE )
{
/* block on read semaphore */
DosRead( CurP->chandle, (PVOID) &inchar, 1, (PUSHORT) &rlen );
if ( rlen == 0)
{ /* timeout, i guess */
continue;
}
/* log the character */
if ( ComLog )
{
if ( !plog )
plog = fopen( "STERM.LOG", "w" );
fputc( inchar, plog );
}
/* filter out unwanted chars */
switch ( inchar )
{
case DC1:
case DC3:
case CDEL: if ( keymode == tty )
break;
default:
{ /* always save up to WSLEN chars */
if ( cip >= WSLEN )
{
memmove( compbuf, &compbuf[1], WSLEN-1 );
compbuf[WSLEN-1] = inchar;
}
else
{
compbuf[cip] = inchar; cip++;
}
/* if mainline is waiting for a string
and this char == last char in string and
we have buffered enough chars and
the string is in the buffer, wheww.. */
if ( waitlen )
{
if ( waitstr[0] == '\0' )
{ /* wait for ANY */
waitlen = 0; waitrc = AWAIT_OK;
DosSemClear( (HSEM) &waitsem );
}
else
if ( ( inchar == waitstr[waitlen-1] ) &&
( cip >= waitlen ) &&
( strncmp( waitstr, &compbuf[cip-waitlen], waitlen ) == 0 ) )
{ /* found awaited string */
waitlen = 0; waitrc = AWAIT_OK;
DosSemClear( (HSEM) &waitsem );
}
}
/* send char to screen */
outw.imdat = inchar;
outw.doecho = FALSE; /* so we know it came from outer space */
DosWrite( vworkW, (PVOID) &outw, sizeof(MsgH), (PUSHORT) &rlen );
}
} /* endswitch */
} /* endwhile */
}
/* Thread to write to the serial port
Blocks on reading 'comoR' pipe */
void comout( void )
{
USHORT wlen;
USHORT mct;
MsgH comsg;
char cline[WSLEN];
BOOL tell_read = TRUE;
while ( TRUE )
{
DosRead( comoR, (PVOID) &comsg, sizeof(MsgH), (PUSHORT) &wlen );
if ( comsg.mtype == termin )
_dosendthread();
/* immediate data is written */
if ( comsg.mtype == immed )
{
DosWrite( CurP->chandle, (PVOID) &comsg.imdat, 1, (PUSHORT) &wlen );
/* log the character */
if ( ComLog )
{
if ( !plog )
plog = fopen( "STERM.LOG", "w" );
fputc( comsg.imdat, plog );
}
}
else
/* MsgH is skipped and data string is written */
if ( comsg.mtype == dhead )
{
DosRead( comoR, (PVOID) &cline, comsg.mlen, (PUSHORT) &wlen );
DosWrite( CurP->chandle, (PVOID) &cline, comsg.mlen, (PUSHORT) &wlen );
if ( ComLog )
{
if ( !plog )
plog = fopen( "STERM.LOG", "w" );
for ( mct = 0; mct < comsg.mlen; mct++ )
fputc( cline[mct], plog );
}
}
/* OK to allow port-read now that we have a port handle */
if ( tell_read )
{
tell_read = FALSE;
DosSemClear( (HSEM) &readsem );
}
/* if echo mode, send chars to 'vidout' */
if ( ( CurP->halfplex ) && ( comsg.doecho ) )
{
DosWrite( vworkW, (PVOID) &comsg, sizeof(MsgH), (PUSHORT) &wlen );
if ( comsg.mtype == dhead )
DosWrite( vworkW, (PVOID) &cline, comsg.mlen, (PUSHORT) &wlen );
}
}
} /* end comout */