home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / STERM.ZIP / STERMC.C < prev    next >
Text File  |  1991-01-14  |  6KB  |  219 lines

  1.   /* STERMC.C
  2.      Handle COM port access, open the device and
  3.      set parms to currently stored values */
  4.  
  5.   #include <stdlib.h>
  6.   #include <stdio.h>
  7.   #include <string.h>
  8.   #include <process.h>
  9.  
  10.   #define  INCL_SUB
  11.   #define  INCL_DOSDEVICES
  12.   #include <os2kernl.h>
  13.   #define  STERMC
  14.   #include "sterm.h"
  15.  
  16.  
  17.   int setport( PortDef *pd )
  18.      {
  19.       USHORT iostat;           /* status from kernl call */
  20.       USHORT action;           /* Open retcode */
  21.       USHORT   openflag = 0x01;  /* fail if dvc not avail */
  22.       USHORT   openmode = 0x12;  /* read/write, deny others */
  23.  
  24.      /* open device for access */
  25.      if ( DosOpen( pd->cport, (PUSHORT) &pd->chandle,
  26.                (PUSHORT) &action,
  27.                0L, 0, openflag, openmode, 0L ) )
  28.                errexit( "Can't Open COM device." );
  29.  
  30.      /* get serial port DCB  */
  31.      iostat = DosDevIOCtl( (PVOID) &pd->ldcb, NULL, 0x73, 1, pd->chandle );
  32.      if ( iostat != 0 )
  33.           errexit( "Can't get port DCB." );
  34.  
  35.      /* request DTR control mode */
  36.      pd->ldcb.enflag = enable_DTR;
  37.  
  38.      /* set serial port DCB  */
  39.      iostat = DosDevIOCtl( NULL, (PVOID) &pd->ldcb, 0x53, 1, pd->chandle );
  40.      if ( iostat != 0 )
  41.           errexit( "Can't set port DCB." );
  42.  
  43.      /* set baud rate */
  44.      if ( DosDevIOCtl( NULL, (PVOID) &pd->lspeed, 0x41, 1, pd->chandle ) )
  45.           errexit( "Can't set baud rate." );
  46.  
  47.      /* init serial port */
  48.      if ( DosDevIOCtl( NULL, (PVOID) &pd->ltype, 0x42, 1, pd->chandle ) )
  49.           errexit( "Can't configure COM port." );
  50.  
  51.      return ( 0 );
  52.      }  /* end setport */
  53.  
  54. #define NUL '\x00'   /* null byte */
  55. #define ESC '\x1B'   /* leadin to control seq. */
  56.  
  57. #define LEADIN ";01H\x1B[0m"
  58. #define LEADL 8          /* length of leadin */
  59. static USHORT sqp = 0;   /* index to escseq */
  60. static char escsqc[10];
  61. static BOOL havleadin = FALSE;
  62. static USHORT bcount = 0;
  63.  
  64. static  FILE *plog = NULL;     /* port log file */
  65.  
  66. /* Read from the serial port
  67.    Block on input from the serial port and a terminate/semaphore. */
  68.  
  69. void  comin( void )
  70.   {
  71.   /* characters which IBM wraps its lines in */
  72.   #define DC1 0x11
  73.   #define DC3 0x13
  74.   #define CDEL 0x7f
  75.   USHORT rlen;
  76.   BYTE inchar;
  77.   MsgH outw;
  78.   USHORT cip;            /* store ptr */
  79.  
  80.   outw.mtype = immed;  /* set for terminal mode */
  81.   outw.mlen  = 1;      /* char at a time */
  82.   cip = 0;
  83.   memset( (void *) compbuf, 0, WSLEN );
  84.   DosSemWait( (HSEM) &readsem, -1L );
  85.  
  86.  
  87.   while ( TRUE )
  88.     {
  89.     /* block on read semaphore */
  90.     DosRead( CurP->chandle, (PVOID) &inchar, 1, (PUSHORT) &rlen );
  91.     if ( rlen == 0)
  92.        {  /* timeout, i guess */
  93.        continue;
  94.        }
  95.  
  96.     /* log the character */
  97.     if ( ComLog )
  98.        {
  99.        if ( !plog )
  100.           plog = fopen( "STERM.LOG", "w" );
  101.        fputc( inchar, plog );
  102.        }
  103.  
  104.  
  105.  
  106.     /*  filter out unwanted chars */
  107.     switch ( inchar )
  108.       {
  109.       case DC1:
  110.       case DC3:
  111.       case CDEL: if ( keymode == tty )
  112.                  break;
  113.       default:
  114.           {  /* always save up to WSLEN chars */
  115.            if ( cip >= WSLEN )
  116.               {
  117.                memmove( compbuf, &compbuf[1], WSLEN-1 );
  118.                compbuf[WSLEN-1] = inchar;
  119.               }
  120.            else
  121.               {
  122.                compbuf[cip] = inchar; cip++;
  123.               }
  124.  
  125.            /* if mainline is waiting for a string
  126.               and this char == last char in string and
  127.               we have buffered enough chars and
  128.               the string is in the buffer, wheww.. */
  129.            if ( waitlen )
  130.               {
  131.               if ( waitstr[0] == '\0' )
  132.                    {  /* wait for ANY */
  133.                    waitlen = 0; waitrc = AWAIT_OK;
  134.                    DosSemClear( (HSEM) &waitsem );
  135.                    }
  136.               else
  137.               if ( ( inchar == waitstr[waitlen-1] ) &&
  138.                    ( cip >= waitlen ) &&
  139.                    ( strncmp( waitstr, &compbuf[cip-waitlen], waitlen ) == 0 ) )
  140.                    {  /* found awaited string */
  141.                    waitlen = 0; waitrc = AWAIT_OK;
  142.                    DosSemClear( (HSEM) &waitsem );
  143.                    }
  144.               }
  145.  
  146.           /* send char to screen */
  147.           outw.imdat = inchar;
  148.           outw.doecho = FALSE;  /* so we know it came from outer space */
  149.           DosWrite( vworkW, (PVOID) &outw, sizeof(MsgH), (PUSHORT) &rlen );
  150.           }
  151.       }  /* endswitch */
  152.     } /* endwhile */
  153.   }
  154.  
  155. /* Thread to write to the serial port
  156.    Blocks on reading 'comoR' pipe */
  157.  
  158.  
  159. void comout( void )
  160.   {
  161.   USHORT wlen;
  162.   USHORT mct;
  163.   MsgH comsg;
  164.   char cline[WSLEN];
  165.   BOOL tell_read = TRUE;
  166.  
  167.   while ( TRUE )
  168.     {
  169.     DosRead( comoR, (PVOID) &comsg, sizeof(MsgH), (PUSHORT) &wlen );
  170.     if ( comsg.mtype == termin )
  171.        _dosendthread();
  172.  
  173.     /* immediate data is written */
  174.     if ( comsg.mtype == immed )
  175.        {
  176.        DosWrite( CurP->chandle, (PVOID) &comsg.imdat, 1, (PUSHORT) &wlen );
  177.        /* log the character */
  178.        if ( ComLog )
  179.           {
  180.           if ( !plog )
  181.              plog = fopen( "STERM.LOG", "w" );
  182.           fputc( comsg.imdat, plog );
  183.           }
  184.  
  185.        }
  186.     else
  187.     /* MsgH is skipped and data string is written */
  188.     if ( comsg.mtype == dhead )
  189.        {
  190.        DosRead( comoR, (PVOID) &cline, comsg.mlen, (PUSHORT) &wlen );
  191.        DosWrite( CurP->chandle, (PVOID) &cline, comsg.mlen, (PUSHORT) &wlen );
  192.        if ( ComLog )
  193.           {
  194.           if ( !plog )
  195.              plog = fopen( "STERM.LOG", "w" );
  196.           for ( mct = 0; mct < comsg.mlen; mct++ )
  197.               fputc( cline[mct], plog );
  198.           }
  199.  
  200.        }
  201.  
  202.     /* OK to allow port-read now that we have a port handle */
  203.     if ( tell_read )
  204.        {
  205.        tell_read = FALSE;
  206.        DosSemClear( (HSEM) &readsem );
  207.        }
  208.  
  209.     /* if echo mode, send chars to 'vidout' */
  210.     if ( ( CurP->halfplex ) && ( comsg.doecho ) )
  211.        {
  212.        DosWrite( vworkW, (PVOID) &comsg, sizeof(MsgH), (PUSHORT) &wlen );
  213.        if ( comsg.mtype == dhead )
  214.           DosWrite( vworkW, (PVOID) &cline, comsg.mlen, (PUSHORT) &wlen );
  215.        }
  216.     }
  217.   }  /* end comout */
  218.  
  219.