home *** CD-ROM | disk | FTP | other *** search
/ Boldly Go Collection / version40.iso / TS / 17A / DRWIN101.ZIP / SERUTIL.H < prev    next >
Text File  |  1990-12-12  |  14KB  |  254 lines

  1. #ifndef __SERUTIL
  2. #define __SERUTIL
  3. /****************************************************************************/
  4. /*  HEADER serutil.h                                                        */
  5. /****************************************************************************/
  6. /*-----------------------------Description----------------------------------*/
  7. /*  This file contains constants, data definitions and function prototypes  */
  8. /*  for the serial communications functions in serutil.c.                   */
  9. /*  A sample program is located at the end of this file.                    */
  10. /*-----------------------------Constraints/Gotchas--------------------------*/
  11. /*  This code requires another utility module, bufutil.c (.h), in order to  */
  12. /*  run.                                                                    */
  13. /*--Date--------Programmer----------Comments--------------------------------*/
  14. /*  1990.07.12  D. Rogers           initial release, 1.00                   */
  15. /****************************************************************************/
  16. #define SER_VERSION  "1.00"
  17.  
  18. #include "bufutil.h"
  19.  
  20.  
  21. #ifndef WORD
  22.   #define WORD unsigned int
  23. #endif
  24.  
  25. #ifndef BYTE
  26.   #define BYTE unsigned char
  27. #endif
  28.  
  29.  
  30. /****************************************************************************/
  31. /*  default constants, high-level constants..                               */
  32. /****************************************************************************/
  33. #define MAXPORTS       4
  34.  
  35. enum COMPORTS { COM1=0,COM2,COM3,COM4 };
  36.  
  37. enum SERERRORS {
  38.   SER_NONE=0,                          /*no serial errors*/
  39.   SER_BADPORT,                         /*port pointer was NULL*/
  40.   SER_UNINIT,                          /*port is not yet initialized*/
  41.   SER_BADCOM,                          /*port pointer way off, com field bad*/
  42.   SER_ILLEGALSPEC,                     /*bad cfg specifier*/
  43.   SER_ILLEGALCOM,                      /*bad cfg COM value, c*/
  44.   SER_ILLEGALBAUD,                     /*bad cfg baud rate, b*/
  45.   SER_ILLEGALPARY,                     /*bad cfg parity, p*/
  46.   SER_ILLEGALDATA,                     /*bad cfg data bits, d*/
  47.   SER_ILLEGALSTOP,                     /*bad cfg stop bits, s*/
  48.   SER_ILLEGALRSIZE,                    /*bad cfg rx buffer size, r*/
  49.   SER_ILLEGALTSIZE,                    /*bad cfg tx buffer size, t*/
  50.   SER_ILLEGALDTR,                      /*bad cfg DTR value specified*/
  51.   SER_ILLEGALRTS,                      /*bad cfg RTS value specified*/
  52.   SER_ILLEGALBRK,                      /*bad cfg Break value specified*/
  53.   SER_NOAVAILABLE,                     /*com port not available*/
  54.   SER_NOBUFMEM,                        /*could not allocate serial buffers*/
  55.   SER_RXOVER,                          /*receive buffer overflow*/
  56.   SER_TXFULL,                          /*transmit buffer full*/
  57.   SER_RXEMPTY,                         /*receive buffer empty*/
  58.   SER_NOCOMSWITCH,                     /*switched COM ports in sersetcfg()*/
  59.   SER_NOBUFCHANGE,                     /*buffer size not changed in ^^^^^^*/
  60.   SER_LASTERR
  61. };
  62.  
  63. #define SER_DEF_COM    COM1
  64. #define SER_DEF_BAUD   9600
  65. #define SER_DEF_PARY   'N'
  66. #define SER_DEF_DATA   '8'
  67. #define SER_DEF_STOP   '1'
  68. #define SER_DEF_RSIZ   256
  69. #define SER_DEF_TSIZ   256
  70. #define SER_DEF_DTR    '1'
  71. #define SER_DEF_RTS    0
  72. #define SER_DEF_BRK    '0'
  73.  
  74.  
  75. /****************************************************************************/
  76. /*  low-level constants..                                                   */
  77. /****************************************************************************/
  78. #define MSKVAL(a,m,v)  ( ((a)&(~(m))) | ((v)&(m)) )
  79.  
  80. #define SER_COM1INT    0x0C
  81. #define SER_COM2INT    0x0B
  82. #define SER_COM3INT    0x0D
  83. #define SER_COM4INT    0x0D
  84.  
  85. #define SER_COM1IMR    0x10            /*IMR bit for intr 0x0C, above*/
  86. #define SER_COM2IMR    0x08
  87. #define SER_COM3IMR    0x20
  88. #define SER_COM4IMR    0x20
  89.  
  90. #define SER_BAUD_MSK   0x01FF
  91. #define SER_BAUD_300   0x0180
  92. #define SER_BAUD_600   0x00C0
  93. #define SER_BAUD_1200  0x0060
  94. #define SER_BAUD_2400  0x0030
  95. #define SER_BAUD_4800  0x0018
  96. #define SER_BAUD_9600  0x000C
  97. #define SER_BAUD_19200 0x0006
  98.  
  99. #define IOP_PIC        0x0020          /*programmable interrupt controller*/
  100. #define IOP_IMR        0x0021          /*interrupt mask register I/O port*/
  101.  
  102. #define SIO_RBR        0x00            /*receiver buffer register, DLAB=0*/
  103. #define SIO_THR        0x00            /*transmit hold register, DLAB=0*/
  104. #define SIO_IER        0x01            /*interrupt enable register*/
  105. #define   SIO_ERBFI    0x01            /*enable received data available int*/
  106. #define   SIO_ETBEI    0x02            /*enable transmitter hold register empty int*/
  107. #define   SIO_ELSI     0x04            /*enable receiver line status int*/
  108. #define   SIO_EDSSI    0x08            /*enable modem status interrupt*/
  109. #define SIO_IIR        0x02            /*interrupt identification register*/
  110. #define   SIO_INTPEND  0x01            /*0 if interrupt pending*/
  111. #define   SIO_INTID    0x06            /*interrupt ID bits*/
  112. #define   SIO_INTRX    0x02            /*receive interrupt*/
  113. #define   SIO_INTTX    0x04            /*transmit hold register empty int*/
  114. #define SIO_LCR        0x03            /*line control register*/
  115. #define   SIO_WLS      0x03            /*word length select bits: */
  116. #define   SIO_WLS0     0x01            /*WLS bit 0       00-5  01-6  */
  117. #define   SIO_WLS1     0x02            /*WLS bit 1       10-7  11-8  */
  118. #define   SIO_DAT      0x03            /*more convenient names..*/
  119. #define   SIO_DAT5     0x00
  120. #define   SIO_DAT6     0x01
  121. #define   SIO_DAT7     0x02
  122. #define   SIO_DAT8     0x03
  123. #define   SIO_STB      0x04            /*stop bits: 0-1  1-2*/
  124. #define   SIO_STB1     0x00
  125. #define   SIO_STB2     0x04
  126. #define   SIO_PEN      0x08            /*parity enable:  0-off  1-on*/
  127. #define   SIO_EPS      0x10            /*even parity select: 0-odd  1-even*/
  128. #define   SIO_PAR      0x18
  129. #define   SIO_PARN     0x00
  130. #define   SIO_PARO     0x10
  131. #define   SIO_PARE     0x18
  132. #define   SIO_SPAR     0x20            /*stick parity*/
  133. #define   SIO_SBRK     0x40            /*set break:  0-normal  1-set break*/
  134. #define   SIO_DLAB     0x80            /*divisor latch access bit*/
  135. #define SIO_MCR        0x04            /*modem control register*/
  136. #define   SIO_DTR      0x01            /*data terminal ready, inverted*/
  137. #define   SIO_RTS      0x02            /*request to send, inverted*/
  138. #define   SIO_OUT1     0x04            /*user output 1, inverted*/
  139. #define   SIO_OUT2     0x08            /*user output 2, inverted*/
  140. #define   SIO_EINTS    0x08            /*OUT2=enable interrupts*/
  141. #define   SIO_LPBK     0x10            /*loopback of TX to RX*/
  142. #define SIO_LSR        0x05            /*line status register*/
  143. #define   SIO_DR       0x01            /*data ready when 1*/
  144. #define   SIO_OR       0x02            /*overrun error, bits 1-4 can gen ints*/
  145. #define   SIO_PE       0x04            /*parity error*/
  146. #define   SIO_FE       0x08            /*framing error (RX stop bit bad)*/
  147. #define   SIO_BI       0x10            /*break interrupt (RX was BREAK)*/
  148. #define   SIO_THRE     0x20            /*transmitter hold register empty*/
  149. #define   SIO_TSRE     0x40            /*transmitter shift register empty*/
  150. #define SIO_MSR        0x06            /*modem status register*/
  151. #define   SIO_DCTS     0x01            /*delta clear to send, CTS changed*/
  152. #define   SIO_DDSR     0x02            /*delta data set ready*/
  153. #define   SIO_TERI     0x04            /*trailing edge ring indicator*/
  154. #define   SIO_DRLSD    0x08            /*delta receive line signal detected*/
  155. #define   SIO_CTS      0x10            /*clear to send*/
  156. #define   SIO_DSR      0x20            /*data set ready*/
  157. #define   SIO_RI       0x40            /*ring indicator*/
  158. #define   SIO_RLSD     0x80            /*received line signal detected*/
  159. #define SIO_DLL        0x00            /*divisor latch LSB, when DLAB=1*/
  160. #define SIO_DLM        0x01            /*divisor latch MSB, when DLAB=1*/
  161.  
  162.  
  163. /****************************************************************************/
  164. /*  PORT structure definition...                                            */
  165. /****************************************************************************/
  166. typedef struct {
  167.   WORD com;                            /*COM port being used*/
  168.   BUFF *rx;                            /*receive buffer*/
  169.   BUFF *tx;                            /*transmit buffer*/
  170.   void interrupt (*serint)(void);      /*receive interrupt vector*/
  171.   void interrupt (*serold)(void);      /*old vector, replaced by rxint*/
  172.   WORD intnum;                         /*receive interrupt number*/
  173.   WORD intimr;                         /*IMR bit to disable ints*/
  174. } PORT;
  175.  
  176.  
  177. /****************************************************************************/
  178. /*  function prototypes..                                                   */
  179. /****************************************************************************/
  180. int   serbad(PORT *p);                   /*returns 1 if pointer is not good*/
  181. int   sersetdtr(PORT *p,int dtr);        /*sets DTR (Data Terminal Ready)*/
  182. int   sersetrts(PORT *p,int rts);        /*sets RTS (Request To Send)*/
  183. int   sersetbrk(PORT *p,int brk);        /*sets the Break control line*/
  184. int   sergetdtr(PORT *p);                /*gets DTR setting*/
  185. int   sergetrts(PORT *p);                /*gets RTS setting*/
  186. int   sergetbrk(PORT *p);                /*gets Break setting*/
  187. int   sergetcts(PORT *p);                /*gets CTS (Clear To Send) input*/
  188. int   sergetdsr(PORT *p);                /*gets DSR (Data Set Ready) input*/
  189. int   sersetbaud(PORT *p,int b);         /*sets baud rate*/
  190. int   sersetpary(PORT *p,char val);      /*sets parity, 'N', 'O', or 'E'*/
  191. int   sersetdata(PORT *p,char val);      /*sets data bits, '5' - '8'*/
  192. int   sersetstop(PORT *p,char val);      /*sets stop bits, '1' or '2'*/
  193. int   sergetbaud(PORT *p);               /*gets baud rate*/
  194. char  sergetpary(PORT *p);               /*gets parity*/
  195. char  sergetdata(PORT *p);               /*gets data bits*/
  196. char  sergetstop(PORT *p);               /*gets stop bits*/
  197.  
  198. PORT *seropen(char *cfg,...);            /*opens serial port, returns handle*/
  199. int   serclose(PORT *p);                 /*closes serial port*/
  200. int   serputc(PORT *p,int c);            /*puts out single character*/
  201. int   serputs(PORT *p,char *s);          /*puts out NUL-terminated string*/
  202. int   serputns(PORT *p,char *s,int n);   /*puts out string of n characters*/
  203. int   serprintf(PORT *p,char *fmt,...);  /*does printf() to transmit buffer*/
  204. int   sergetc(PORT *p);                  /*gets one character from port*/
  205. int   sergets(PORT *p,char *s,int n);    /*gets '\n'-terminated string*/
  206. int   sersetcfg(PORT *p,char *cfg,...);  /*sets configuration like seropen()*/
  207. char *sergetcfg(PORT *p,char *s,int n);  /*gets readable config string, ret s*/
  208. char *sererrmsg(int ec);                 /*error message for sererr code*/
  209.  
  210. extern volatile PORT serp[MAXPORTS];   /*serial port structures*/
  211. extern int  sererr;                    /*error condition indicator*/
  212. extern char *sercomstr[8];             /*names of the COM ports*/
  213.  
  214.  
  215. /*=== Sample progrm ========================================================*/
  216. /*                                                                          */
  217. /*  #include <stdio.h>                                                      */
  218. /*  #include "serutil.h"                                                    */
  219. /*                                                                          */
  220. /*  #define BUFFSIZE 1024                                                   */
  221. /*                                                                          */
  222. /*  PORT *p;                           /.handle, pointer to serial port./   */
  223. /*                                                                          */
  224. /*  void main(void)                    /.loopback test program./            */
  225. /*  {                                                                       */
  226. /*    int c;                           /.keyboard character./               */
  227. /*                                                                          */
  228. /*    p=seropen("c%d b2400 pNd8s1 r%d t%d",COM1,BUFFSIZE,BUFFSIZE);         */
  229. /*    if (p==NULL) {                   /.check that port initialized ok./   */
  230. /*      printf("Error on open: %s\n",sererrmsg(sererr));                    */
  231. /*      return;                                                             */
  232. /*    }   /.if error./                                                      */
  233. /*                                                                          */
  234. /*    printf("Connect lines 2 and 3 of COM1 for a loopback test.\n");       */
  235. /*    printf("Press <ESC> to quit.\n");                                     */
  236. /*    while (1) {                                                           */
  237. /*      while (kbhit()) {              /.while keyboard input.../           */
  238. /*        c=getch();                   /...get input./                      */
  239. /*        if (c==0x1B) {               /...if <ESC>.../                     */
  240. /*          serclose(p);               /.....close port./                   */
  241. /*          return;                    /.....quit./                         */
  242. /*        }   /.if <ESC>./                                                  */
  243. /*        serputc(p,c);                /...else put char out serial port./  */
  244. /*      }   /.while kb input./                                              */
  245. /*      while (!bufempty(p->rx))       /.if char received./                 */
  246. /*        putchar(sergetc(p));         /...echo it to screen./              */
  247. /*    }   /.while forever./                                                 */
  248. /*  }   /.main./                                                            */
  249. /*                                                                          */
  250. /*==========================================================================*/
  251.  
  252.  
  253. #endif                                 /*if not already included*/
  254.