home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c222 / 1.ddi / SOURCE / IBMLIB / SI_HANDL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-10  |  2.4 KB  |  127 lines

  1. /*********************
  2.  *
  3.  *  si_handl.c - serial interface handler.
  4.  *
  5.  *  Purpose: This file contains handlers for the com1 and com2 devices.
  6.  *
  7.  *  Blackstar C Function Library
  8.  *  (c) Copyright 1985,1989 Sterling Castle Software
  9.  *
  10.  *******/
  11.  
  12. #include <dos.h>
  13. #include "blackstr.h"
  14. #include "si_head.h"
  15.  
  16. char si_types_[4];        /* types (line or modem) for port #'s */
  17.  
  18.  
  19. /********
  20.  *
  21.  *   si_init(port,parms) - initialize a serial port
  22.  *
  23.  **/
  24.  
  25. int si_init(int port, struct SI_PARMS *parms)
  26. {
  27.     union REGS inregs,outregs;
  28.     char sbyte;
  29.  
  30.     sbyte = parms->wbits - 5;       /* bits 0-1 are word length */
  31.     sbyte |= (parms->stop-1) << 2;  /* bit 2 is stop bits */
  32.     sbyte |= (parms->parity) <<  3; /* bits 3&4 are parity */
  33.     sbyte |= (parms->baud) << 5;    /* bits 5-7 are speed */
  34.  
  35.     inregs.x.dx = port;
  36.     inregs.h.ah = 0;                /* command to set port */
  37.     inregs.h.al = sbyte;            /* init byte for bios */
  38.     int86(0x14,&inregs,&outregs);
  39.     si_types_[port] = parms->type;  /* save type of port */
  40.     return(outregs.x.ax);
  41. }
  42.  
  43.  
  44. /********
  45.  *
  46.  *   si_stat(port) - get port status
  47.  *
  48.  **/
  49.  
  50. int si_stat(int port)
  51. {
  52.     union REGS inregs,outregs;
  53.  
  54.     inregs.x.dx = port;
  55.     inregs.h.ah = 3;                /* command to get status */
  56.     int86(0x14,&inregs,&outregs);
  57.     return(outregs.x.ax);
  58. }
  59.  
  60.  
  61. /********
  62.  *
  63.  *   si_out(port,c) - output byte to port
  64.  *
  65.  **/
  66.  
  67. int si_out(int port, char c)
  68. {
  69.     union REGS inregs,outregs;
  70.  
  71.     inregs.x.dx = port;
  72.     inregs.h.ah = 1;
  73.     inregs.h.al = c;                /* output byte */
  74.     int86( 0x14,&inregs,&outregs);
  75.     return(outregs.x.ax);
  76. }
  77.  
  78.  
  79. /********
  80.  *
  81.  *   si_inp(port) - input byte from port
  82.  *
  83.  **/
  84.  
  85. int si_inp(int port)
  86. {
  87.     union REGS inregs,outregs;
  88.  
  89.     inregs.x.dx = port;
  90.     inregs.h.ah = 2;
  91.     int86(0x14,&inregs,&outregs);
  92.     if(outregs.h.ah)
  93.     return(ERROR);
  94.     else
  95.     return((int)outregs.h.al);
  96. }
  97.  
  98.  
  99. /********
  100.  *
  101.  *   si_putc(port,c) - put a character to the serial port
  102.  *
  103.  **/
  104.  
  105. int si_putc(int port, char c)
  106. {
  107.     while(!(si_stat(port)& 0x60)) ;     /* wait for tx buff empty */
  108.     return(si_out(port,c));
  109. }
  110.  
  111.  
  112. /********
  113.  *
  114.  *   si_getc(port) - get character from a serial port
  115.  *
  116.  **/
  117.  
  118. int si_getc(int port)
  119. {
  120.     if (si_stat(port) & 0x0100)         /* wait for data ready */
  121.     return(si_inp(port));
  122.     else
  123.     return(0);
  124. }
  125.  
  126.  
  127.