home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / OS2UTIL.ZIP / OS2ASY.C < prev    next >
Text File  |  1990-05-21  |  19KB  |  470 lines

  1. /* os2asy.c */
  2. /* This file contains twelve routines for asynchrounous communication through*/
  3. /* com1 and com2. The routines: port_open(), port_close(), port_close_all(), */
  4. /* send_line(), send_asy(), asy_chr(), asyget(), asy_stream(), inqsize(),    */
  5. /* asy_get(), fasy_get(), rdch(), flushib() and flushob() are provided below.*/
  6. /* The file os2asy.h should be included at the top of your program */
  7.  
  8. #define OS2ASY
  9. #include "os2asy.h"
  10.  
  11. /*   port_open - open com port and return error message            */
  12. /*                                                                 */
  13. /*   CALLING SYNTAX:      port_open(port)                          */
  14. /*                                                                 */
  15. /*   int port_open();     open port port                           */
  16. /*                         0 - OK                                  */
  17. /*                        >0 - error                               */
  18. /*   int port;            com port number; range 1-2               */
  19. /*                                                                 */
  20. /*   MEG - 05/16/90                           */
  21.  
  22. port_open(port, baud, xon)
  23. int port, baud, xon;
  24. {
  25.     int  sts, res;
  26.     char portstr[5];
  27.  
  28.     if (port > MAX_PORTS)
  29.     return -1;
  30.  
  31.     sprintf(portstr, "com%d", port);
  32.     sts = DosOpen(portstr, &asy.porthf[port], &res, 0L, 0, 1, 0x12, 0L);
  33.  
  34.     if(sts)  {
  35.             /*printf("com%d port error sts = %d\n", port, sts);*/
  36.             return 1;
  37.     }                                                    /* puts("com opened");     */
  38.  
  39.     DosDevIOCtl(0L, &baud, 0x41, 0x01, asy.porthf[port]);    /* puts("buad rate set");   */
  40.     DosDevIOCtl(0L, &comm_ctl, 0x42, 0x01, asy.porthf[port]);/* puts("other param set"); */
  41.  
  42.     /* enable xon/xoff for xmit and receive */
  43.     DosDevIOCtl(&di, 0L, 0x73, 0x01, asy.porthf[port]);
  44.     di.f1 = 0;
  45.     di.f2 = (xon) ? (char) 0x03 : (char) 0x00;
  46.     DosDevIOCtl(0L, &di, 0x53, 0x01, asy.porthf[port]);
  47.  
  48.     asy.curptr[port] = asy.commbuff[port];
  49.     asy.bufptr[port] = asy.buff[port];
  50.  
  51.     return(0);
  52. }
  53.  
  54. /*   port_close - close com port and return error message          */
  55. /*                                                                 */
  56. /*   CALLING SYNTAX:      port_close(port)                         */
  57. /*                                                                 */
  58. /*   int port_close();    close port port                          */
  59. /*                         0 - OK                                  */
  60. /*                        >0 - error                               */
  61. /*   int port;            com port number; range 1-2               */
  62. /*                                                                 */
  63. /*   MEG - 12/22/88                                                */
  64.  
  65. port_close(port)
  66. int port;
  67. {
  68.  
  69.     return( DosClose(asy.porthf[port]) );
  70.                                   /* printf("com%d closed\n", port); */
  71. }
  72.  
  73. /*   port_close_all - close all com ports and return error message */
  74. /*                                                                 */
  75. /*   CALLING SYNTAX:      port_close_all()                         */
  76. /*                                                                 */
  77. /*   int port_close();    close all com ports                      */
  78. /*                         0 - OK                                  */
  79. /*                         2 - file not found COM1                 */
  80. /*                         5 - access  denied COM1                 */
  81. /*                         6 - invalid handle COM1                 */
  82. /*                        16 - file not found COM2                 */
  83. /*                        40 - access  denied COM2                 */
  84. /*                        48 - invalid handle COM2                 */
  85. /*                                                                 */
  86. /*   error codes for COM1 and COM2 are added together              */
  87. /*                                                                 */
  88. /*   MEG - 12/22/88                                                */
  89.  
  90. port_close_all()
  91. {
  92.  
  93.     return( DosClose(asy.porthf[COM1]) + 8*DosClose(asy.porthf[COM2]) );
  94.  
  95. }
  96.  
  97. /*   send_line - send a line of data to the com port               */
  98. /*                                                                 */
  99. /*   CALLING SYNTAX:      send_line(port, str)                     */
  100. /*                                                                 */
  101. /*   int send_line();     send string to a com port                */
  102. /*                         0 - OK                                  */
  103. /*                        >0 - error                               */
  104. /*   int port;            com port number; range 1-2               */
  105. /*   char *str          data to be send               */
  106. /*                                                                 */
  107. /*   MEG - 03/09/90                           */
  108.  
  109. send_line(port, str)
  110. char *str;
  111. {
  112. char cmd[256];
  113. int size = 1, cnt, tmp, retcode;
  114. QInfo qdata;
  115.                                                 /*printf("  port = %d  str='%s'\n", port, str);*/
  116.         strncpy(cmd, str, 256);
  117.         cmd[255] = NULL;
  118.         cnt = strlen(cmd);                      /* get string length       */
  119.                                                 /*printf("  cnt=%d  cmd='%s'", cnt, cmd);*/
  120.         retcode=DosWrite(asy.porthf[port], cmd, cnt, &tmp);
  121.                                                 /*printf("  write retcode=%d", retcode);*/
  122.         while(size)  {                          /* wait until sent   */
  123.                 DosDevIOCtl(&qdata, 0L, 0x69, 0x01, asy.porthf[port]);
  124.                 size = qdata.chrs_in_q;
  125.                                                 /*printf("   size = %d", size);*/
  126.         }
  127.         return(retcode);
  128. }
  129. /*   send_asy -- send a line of data to the com port w/CR       */
  130. /*                                                                 */
  131. /*   CALLING SYNTAX:      send_asy(port, str)               */
  132. /*                                                                 */
  133. /*   int send_asy();      send string to a com port           */
  134. /*                         0 - OK                                  */
  135. /*                        >0 - error                               */
  136. /*   int port;            com port number; range 1-2               */
  137. /*   char *str          data to be send  -- CR tacked on       */
  138. /*                                                                 */
  139. /*   MEG - 03/12/90                           */
  140.  
  141. send_asy(port, str)
  142. int port;
  143. char *str;
  144. {
  145.     send_line(port, str);
  146.     return asy_chr(port, '\r');
  147. }
  148.  
  149.  
  150. /*   asy_chr -   send a character to the com port                  */
  151. /*                                                                 */
  152. /*   CALLING SYNTAX:      asy_chr(port, chr)                       */
  153. /*                                                                 */
  154. /*   int asy_chr();       send string to a com port                */
  155. /*                         0 - OK                                  */
  156. /*                        >0 - error                               */
  157. /*   int port;            com port number; range 1-2               */
  158. /*   char *chr            data to be send                          */
  159. /*                                                                 */
  160. /*   MEG - 12/22/88                                                */
  161.  
  162. asy_chr(port, chr)
  163. int port;
  164. char chr;
  165. {
  166.     int size=1, tmp, retcode;
  167.     char str[2];
  168.     QInfo qdata;
  169.                                        /* printf("string='%s'\n", send);*/
  170.  
  171.     str[0]  = chr; str[1] = NULL;
  172.     retcode = DosWrite(asy.porthf[port], str, 1, &tmp);
  173.  
  174.     while (size) {
  175.           DosDevIOCtl(&qdata, 0L, 0x69, 0x01, asy.porthf[port]);
  176.           size = qdata.chrs_in_q;
  177.     }
  178.     return(retcode);
  179.  
  180. }
  181.  
  182. /*   asyget - receive data from com port port, if available        */
  183. /*                                                                 */
  184. /*   CALLING SYNTAX:      asyget(port)                             */
  185. /*                                                                 */
  186. /*   int asyget();        receive string from port com             */
  187. /*                        0 - indicates a record is ready to be    */
  188. /*                            parsed. data is stored in a global   */
  189. /*                            varaible named: commbuff[port]       */
  190. /*                        1 - no complete record send              */
  191. /*   int port             com port number; range: 1 - 2            */
  192. /*                                                                 */
  193. /*   MEG - 12/22/88                                                */
  194.  
  195. asyget(port)
  196. int port;
  197. {
  198.    int retcode = 1;
  199.    static int asyret[3] = { 0, 0, 0 }, bufflen[3] = { 0, 0, 0 };
  200.    int numread, qcnt;
  201.    char *ptr;
  202.  
  203.    /* move date from other buffer to tmpbuff */
  204.    if( *(asyret+port) )  {
  205.        *(asyret+port) = 0;
  206.        *(asy.curptr+port) = *(asy.commbuff+port);
  207.        while( (*(bufflen+port))--)    {
  208.            if(**(asy.bufptr+port) == LF)  {
  209.                *((*(asy.curptr+port))-1) = 0;
  210.                ++*(asy.bufptr+port);
  211.                *(asyret+port)         = 1;
  212.                            retcode                 = 0;
  213.                            break;
  214.                    }
  215.                    else
  216.                *(*(asy.curptr+port))++     = *(*(asy.bufptr+port))++;
  217.             }
  218.     }
  219.  
  220.     qcnt = inqsize(port);
  221.     *(iqsize+port) = qcnt;
  222.     if(qcnt > *(hi_q+port) )
  223.         *(hi_q+port) = qcnt;
  224.     if(qcnt > 800)
  225.             qcnt = 800;
  226.  
  227.  
  228.     if(qcnt && !*(asyret+port) )  {
  229.         DosRead(*(asy.porthf+port), *(asy.curptr+port), qcnt, &numread);
  230.  
  231.             while(numread--)  {
  232.             if(**(asy.curptr+port) == LF && !*(asyret+port) )  {
  233.  
  234.                  *((*(asy.curptr+port))-1) = 0;
  235.                  ptr             = *(asy.curptr+port) + 1;
  236.                  *(asy.curptr+port)        = *(asy.buff+port);
  237.                  *(bufflen+port)           = 0;
  238.                  *(asy.bufptr+port)        = *(asy.buff+port);
  239.                  *(asyret+port)           = 1;
  240.                              retcode                 = 0;
  241.                              continue;
  242.                     }
  243.             if( *(asyret+port) )  {
  244.                 **(asy.curptr+port)        = *ptr++;
  245.                 ++*(bufflen+port);
  246.                     }
  247.             ++*(asy.curptr+port);
  248.             }
  249.     }
  250.  
  251.    return(retcode);
  252. }
  253.  
  254. /*   asy_stream - receive chrs bytes of data from com port port    */
  255. /*                                                                 */
  256. /*   CALLING SYNTAX:      asy_stream(port, chrs, string, ltime)    */
  257. /*                                                                 */
  258. /*   int asy_stream();      receive string chrs chars long from comm */
  259. /*             -1 - insufficient chars in queue       */
  260. /*                        0 - indicates a record is ready to be    */
  261. /*                  parsed. data is stored in string       */
  262. /*                        1 - no complete record send              */
  263. /*   int port          com port number; range: 1 - 2        */
  264. /*   int chrs          number of character to be grabbed       */
  265. /*   char *string      return string                */
  266. /*   unsigned long ltime  time limit in ticks for error if input   */
  267. /*              queue does not receive chr charactes       */
  268. /*                                                                 */
  269. /*   MEG - 05/16/90                           */
  270.  
  271. asy_stream(port, chrs, string, ltime)
  272. int port, chrs;
  273. long ltime;
  274. char *string;
  275. {
  276.     int numread;
  277.     long ctime;
  278.  
  279.     ctime = dostime();
  280.  
  281.     /* wait until queue ready    */
  282.     while (inqsize(port) < chrs && (long)(dostime()-ctime) < ltime);
  283.  
  284.     if ((long)(dostime() - ctime) >= ltime)    /* timeout exceeded */
  285.     return -1;
  286.  
  287.     /* get chrs amount of characters */
  288.     DosRead(*(asy.porthf+port), string, chrs, &numread);
  289.  
  290.     if (numread == chrs)    /* correct flow */
  291.     {
  292.     /*strcpy(string, asy.curptr[port]);*/
  293.     return 0;
  294.     }
  295.     else            /* error flow                */
  296.     {
  297.     flushib(port);        /* reset for next call            */
  298.     return 1;       /* improper number of chars read       */
  299.     }
  300.  
  301. }
  302.  
  303. /*   inqsize - get queue size from com port                        */
  304. /*                                                                 */
  305. /*   CALLING SYNTAX:      inqsize(port)                            */
  306. /*                                                                 */
  307. /*   int inqsize();       return input queue size of com port      */
  308. /*   int port;            com port number; range: 1-2              */
  309. /*                                                                 */
  310. /*   MEG - 12/22/88                                                */
  311.  
  312. inqsize(port)
  313. int port;
  314. {
  315.         QInfo qdata;
  316.  
  317.         DosDevIOCtl(&qdata, 0L, 0x68, 0x01, asy.porthf[port]);
  318.         return(qdata.chrs_in_q);
  319. }
  320.  
  321. /*   asy_get - receive data from com port, if available            */
  322. /*                                                                 */
  323. /*   CALLING SYNTAX:      asy_get(port, string)                    */
  324. /*                                                                 */
  325. /*   int asy_get();       receive string from com port             */
  326. /*                                                                 */
  327. /*   int port;            com port number; range 1-2               */
  328. /*                                                                 */
  329. /*   char *string;        data from com port                       */
  330. /*                                                                 */
  331. /*   int retcode;         0 - indicates a record is ready to be    */
  332. /*                            parsed. data is stored in a variable */
  333. /*                            named: string                        */
  334. /*                        1 - no complete record send              */
  335. /*                                                                 */
  336. /*   MEG - 12/22/88                                                */
  337.  
  338. asy_get(port, string)
  339. int port;
  340. char *string;
  341. {
  342.      int retcode;
  343.  
  344.      DosSleep(250L);              /* wait 1/4 seconds */
  345.  
  346.      if ( (retcode = asyget(port)) == OK)
  347.         strcpy(string, asy.commbuff[port]);
  348.  
  349.      return(retcode);
  350. }
  351.  
  352. /*   fasy_get - receive data with no wait from com port, if avail  */
  353. /*                                                                 */
  354. /*   CALLING SYNTAX:      fasy_get(port, string)           */
  355. /*                                                                 */
  356. /*   int fasy_get();      receive string from com port           */
  357. /*                                                                 */
  358. /*   int port;            com port number; range 1-2               */
  359. /*                                                                 */
  360. /*   char *string;        data from com port                       */
  361. /*                                                                 */
  362. /*   int retcode;         0 - indicates a record is ready to be    */
  363. /*                            parsed. data is stored in a variable */
  364. /*                            named: string                        */
  365. /*                        1 - no complete record send              */
  366. /*                                                                 */
  367. /*   MEG - 03/14/90                           */
  368.  
  369. fasy_get(port, string)
  370. int port;
  371. char *string;
  372. {
  373.      int retcode;
  374.  
  375.      if ( (retcode = asyget(port)) == OK)
  376.         strcpy(string, asy.commbuff[port]);
  377.  
  378.      return(retcode);
  379. }
  380.  
  381. /*   rdch - read a character from com port, if available           */
  382. /*                                                                 */
  383. /*   CALLING SYNTAX:      rdch(port, ch, cnt, numread)             */
  384. /*                                                                 */
  385. /*   int rdch();          read a character from the com port       */
  386. /*                                                                 */
  387. /*   int port;            com port number; range 1-2               */
  388. /*                                                                 */
  389. /*   char *ch;            data from com port                       */
  390. /*                                                                 */
  391. /*   int cnt;             number of characters to be read          */
  392. /*                                                                 */
  393. /*   int *numread;        number of characters read from com port  */
  394. /*                                                                 */
  395. /*   int retcode;         0 - indicates a record is ready to be    */
  396. /*                            parsed. data is stored in a variable */
  397. /*                            named: string                        */
  398. /*                        1 - no complete record send              */
  399. /*                                                                 */
  400. /*   MEG - 04/11/89                                                */
  401.  
  402. rdch(port, ch, cnt, numread)
  403. int port, cnt, *numread;
  404. char *ch;
  405. {
  406.      int retcode;
  407.  
  408.      if (inqsize(port))
  409.         retcode = DosRead(asy.porthf[port], ch, cnt, numread);
  410.      else
  411.         return 2;
  412.  
  413.      return (retcode == 0 && *numread == cnt) ? 0 : 1;
  414. }
  415.  
  416. /*   flushib - flush the input buffer                   */
  417. /*                                                                 */
  418. /*   CALLING SYNTAX:      flush(port)                   */
  419. /*                                                                 */
  420. /*   int flushib();      flush the input buffer to com port       */
  421. /*   int port;            com port number; range: 1-2              */
  422. /*                                                                 */
  423. /*   MEG - 03/12/90                           */
  424. /*                                   */
  425. flushib(port)
  426. int port;
  427. {
  428.     /* FLUSH INPUT buffer */
  429.     return DosDevIOCtl(0L, 0, 0x0001, 0x000B, asy.porthf[port]);
  430. }
  431.  
  432. /*   flushob - flush the output buffer                   */
  433. /*                                                                 */
  434. /*   CALLING SYNTAX:      flushob(port)                */
  435. /*                                                                 */
  436. /*   int flushob();      flush the output buffer to com port       */
  437. /*   int port;            com port number; range: 1-2              */
  438. /*                                                                 */
  439. /*   MEG - 03/12/90                           */
  440. /*                                   */
  441. flushob(port)
  442. int port;
  443. {
  444.     /* FLUSH OUTPUT buffer */
  445.     return DosDevIOCtl(0L, 0, 0x0002, 0x000B, asy.porthf[port]);
  446. }
  447.  
  448. /* dostime.c */
  449.  
  450. unsigned long dostime()     /* ticks */
  451. {
  452.    GINFOSEG  FAR *info;
  453.    SEL gbl, local;
  454.    int hour, min, sec;
  455.    unsigned long gcurtime;
  456.     
  457.    DosGetInfoSeg(&gbl, &local);
  458.    info = MAKEPGINFOSEG(gbl);
  459.  
  460.    hour = info->hour;
  461.    min = info->minutes;
  462.    sec = info->seconds;
  463.    gcurtime = hour*3600L + min*60L + sec;
  464.    gcurtime = gcurtime * 18  +  gcurtime / 5;
  465.    gcurtime += info->hundredths / 5;
  466.  
  467.    return(gcurtime);
  468. }
  469. 
  470.