home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_08 / 2n08038a < prev    next >
Text File  |  1991-05-29  |  4KB  |  154 lines

  1. /*
  2.     Comm.c - Generalized Communication functions
  3.  
  4.     Author - 2/16/91  David T. Lowerre  ;{
  5.  
  6.     2/16/91  Windows version
  7. */
  8.  
  9. #include <stdlib.h>
  10. #include <windows.h>
  11.  
  12. static int comhandle[5]= {-1,-1,-1,-1,-1};    /* support up to com4 */
  13.  
  14. /*
  15.     Open a communication port
  16. */
  17. ComOpen( int port,                  /* Port number */
  18.          int inqsize,               /* input FIFO size */
  19.          int outqsize )             /* output FIFO size */
  20. {
  21.     char comstr[5];
  22.     int handle;
  23.  
  24.     sprintf( comstr, "COM%d", port );
  25.     if( (handle = OpenComm( comstr, inqsize, outqsize )) < 0 )
  26.     {
  27.         error( "Failed to open %s: code %d", comstr, handle );
  28.         return 0;           /* indicate failure */
  29.     }
  30.     comhandle[port] = handle;  /* store the new handle */
  31.     return 1;               /* indicate success */
  32. }
  33.  
  34. /*
  35.     Close a communication port
  36. */
  37. void ComClose( int port )
  38. {
  39.     /* Make sure that we have opened the port */
  40.     if( comhandle[port] >= 0 )
  41.     {
  42.         CloseComm( comhandle[port] );
  43.         comhandle[port] = -1;       /* mark the port as closed */
  44.     }
  45. }
  46.  
  47. static char ptyval[] = { NOPARITY, EVENPARITY, ODDPARITY };
  48. static char stpval[] = { ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS };
  49.  
  50. /*
  51.     ComSet - Set Communication Parameters
  52. */
  53. void ComSet( int port,      /* port number */
  54.              int baud,      /* actual baud rate */
  55.              int parity,    /* 0,1,2 for NONE,EVEN,ODD */
  56.              int bits,      /* actual number of data bits */
  57.              int stop )     /* 0,1,2 for 1, 1.5, 2 */
  58. {
  59.     DCB dcb;
  60.  
  61.     if( comhandle[port] >= 0 )
  62.     {
  63.         /* Get the DCB from Windows */
  64.         GetCommState( comhandle[port], (DCB FAR *)&dcb );
  65.         dcb.BaudRate = baud;
  66.         dcb.ByteSize = bits;
  67.         dcb.Parity = ptyval[parity];
  68.         dcb.StopBits = stpval[stop];
  69.  
  70.         /* Use the new DCB to set up the port */
  71.         SetCommState( (DCB FAR *)&dcb );
  72.     }
  73. }
  74.  
  75. /*
  76.     ComRead - Read from the input FIFO of a port
  77. */
  78. ComRead( int port,      /* port number */
  79.          char *buf,     /* place to put the data */
  80.          int bytes )    /* number of bytes to read */
  81. {
  82.     int read;   /* number of bytes actually read */
  83.     int err;
  84.  
  85.     if( comhandle[port] >= 0 )
  86.     {
  87.         /* if the return value is negative, clear the error */
  88.         if( (read = ReadComm( comhandle[port], buf, bytes )) <= 0 )
  89.             err = GetCommError( comhandle[port], NULL );
  90.         return abs(read);       /* return number of bytes actually read */
  91.     }
  92.     else
  93.         return 0;       /* port not open, nothing read */
  94. }
  95.  
  96. /*
  97.     ComWrite - Write to the output FIFO of a port
  98. */
  99. ComWrite( int port,     /* port number */
  100.           char *buf,    /* source of data to move into FIFO */
  101.           int bytes )   /* number of bytes to write */
  102. {
  103.     int wrote;  /* actual number of bytes written */
  104.     int err;
  105.  
  106.     if( comhandle[port] >= 0 )
  107.     {
  108.         /* if the return value is negative, clear the error */
  109.         if( (wrote = WriteComm( comhandle[port], buf, bytes )) <= 0 )
  110.             err = GetCommError( comhandle[port], NULL );
  111.         return abs(wrote);      /* return actual number of bytes written */
  112.     }
  113.     else
  114.         return 0;       /* port not open, nothing written */
  115. }
  116.  
  117. /*
  118.     ComInQSize - Determine the number of bytes waiting in the input FIFO
  119. */
  120. ComInQSize( int port )
  121. {
  122.     COMSTAT cstat;
  123.     int err;
  124.  
  125.     if( comhandle[port] >= 0 )
  126.     {
  127.         /* catch the COMSTAT information in cstat */
  128.         err = GetCommError( comhandle[port], &cstat );
  129.         return cstat.cbInQue;   /* number of bytes in the FIFO */
  130.     }
  131.     else
  132.         return 0;       /* port not open, no bytes in FIFO! */
  133. }
  134.  
  135. /*
  136.     ComOutQSize - Determine the number of bytes remaining in the
  137.                   Output FIFO.
  138. */
  139. ComOutQSize( int port )
  140. {
  141.     COMSTAT cstat;
  142.     int err;
  143.  
  144.     if( comhandle[port] >= 0 )
  145.     {
  146.         /* Catch the COMSTAT information in cstat */
  147.         err = GetCommError( comhandle[port], &cstat );
  148.         return cstat.cbOutQue;      /* number of bytes in the FIFO */
  149.     }
  150.     else
  151.         return 0;       /* port not open, no bytes in FIFO! */
  152. }
  153.  
  154.