home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 110_01 / yam5ii.c < prev    next >
Text File  |  1984-03-03  |  4KB  |  194 lines

  1. /*
  2. >>:yam5.c 9-03-81
  3.  * Modem related functions. If your modem supports baudrate setting,
  4.  * and hangup, write your own routines here.
  5.  * If your modem supports an autocall, put that routine here also
  6.  * define MODEMSTUFF to suppress default baud rate related functions.
  7.  * define AUTOCALL in your autocall routine to suppress the default
  8.  * which merely prints the phone number and recommended baud rate.
  9.  */
  10.  
  11. /*
  12.  * setbaud(nbaud) If legal rate, set modem registers and Baudrate
  13.  */
  14. #include "yam.h"
  15.  
  16. #ifdef TUART
  17. #define MODEMSTUFF
  18. /*
  19.  * set baud rate for modem driven by Cromenco TUART
  20.  */
  21. setbaud(nbaud)
  22. unsigned nbaud;
  23. {
  24.     char command, baudcmd;
  25.     command=0;
  26.     switch(nbaud) {
  27.         case 110: baudcmd=0001;break;
  28.         case 150: baudcmd=0202;break;
  29.         case 300: baudcmd=0204;break;
  30.         case 1200: baudcmd=0210;break;
  31.         case 2400: baudcmd=0220;break;
  32.         case 4800: baudcmd=0240;break;
  33.         case 9600: baudcmd=0300;break;
  34.         case 19200: baudcmd=0220;command=020;break;
  35.         case 38400: baudcmd=0240;command=020; break;
  36.     default:
  37.         return ERROR;
  38.     }
  39.     outp(Sport, baudcmd);
  40.     outp(Sport+2, command);
  41.     Baudrate=nbaud;
  42.     return 0;
  43. }
  44. readbaud()
  45. {
  46.     Baudrate=DEFBAUD;
  47. }
  48.  
  49. /* Bye disconnects the line and allows another call */
  50. bye()
  51. {
  52.     outp(Sport+2, 03);    /* turn on break */
  53.     sleep(40/CLKMHZ);    /* wait two seconds */
  54.     setbaud(Baudrate);
  55. }
  56.  
  57. /* onhook disconnects the line for good */
  58. onhook()
  59. {}    /* no way to go on hook with this setup */
  60. #endif
  61. #ifdef Z89
  62. #define MODEMSTUFF
  63. /*
  64.  * Routine to set baud rate for 8250 driven modem port
  65.  */
  66. setbaud(nbaud)
  67. unsigned nbaud;
  68. {
  69.     unsigned bcmd;
  70.  
  71.     if(nbaud==0)
  72.         return ERROR;
  73.     bcmd= 57600;        /* 1.8432 Mhz clock */
  74.     bcmd /= (nbaud/2);    /* this must be done unsigned! */
  75.     outp(Dport+3, 0203);    /* enable divisor latch */
  76.     outp(Dport, bcmd);
  77.     outp(Dport+1, (bcmd>>8));
  78.     /* 8 data bits, 1 stop bit (2 if 110 baud), disable divisor latch */
  79.     outp(Dport+3, nbaud==110? 07:03);
  80.     /* turn on dtr and rts, also output2 is baudrate is 1200 or more */
  81.     outp(Dport+4, nbaud>=1200?017:03);
  82.     Baudrate=nbaud;
  83. /*
  84.     printf("bcmd=%d bcmdms=0%o bcmdls=0%o\n",bcmd,(bcmd>>8),(bcmd&0377));
  85. */
  86.     return OK;
  87. }
  88. /* fetch the baudrate from the modem port */
  89. readbaud()
  90. {
  91.     char dp3;
  92.     unsigned div;
  93.  
  94.     dp3=inp(Dport+3);
  95.     outp(Dport+3, 0200|dp3);
  96.     div= inp(Dport) | (inp(Dport+1)<<8);    /* fetch divisor */
  97.     outp(Dport+3, dp3);    /* restore uart modes */
  98.     Baudrate=57600;        /* be sure all this remains unsigned! */
  99.     Baudrate /= div;
  100.     Baudrate <<= 1;
  101. }
  102.  
  103. /* Bye hangs up the line and then resets for another call */
  104. bye()
  105. {
  106.     onhook();
  107.     sleep(40/CLKMHZ);
  108.     setbaud(Baudrate);
  109. }
  110.  
  111. /* onhook goes off line for good */
  112. onhook()
  113. {
  114.     outp(Dport+4, 0);
  115. }
  116.  
  117. #endif
  118.  
  119. #ifndef MODEMSTUFF
  120. setbaud()
  121. {}    /* INSERT ROUTINE HERE IF AVAILABLE */
  122. onhook()
  123. {}    /* INSERT ROUTINE HERE IF AVAILABLE */
  124. bye()
  125. {}    /* INSERT ROUTINE HERE IF AVAILABLE */
  126. readbaud()
  127. {
  128.     Baudrate=DEFBAUD;
  129. }
  130. #endif
  131.  
  132. /*
  133.  * Readline from MODEM13.C rewritten to allow much higher
  134.  * baud rates.
  135.  * Timeout is in deciseconds (1/10th's)
  136.  * For top speed, character ready is checked in many places.
  137.  * returns TIMEOUT if kbd character is ready.
  138.  */
  139. readline(decisecs)
  140. {
  141.     if(MIREADY)
  142.         return MICHAR;
  143.     while(--decisecs>=0) {
  144. #ifdef CDO
  145.         if(MIREADY)
  146.             return MICHAR;
  147.         if(CDO)
  148.             return TIMEOUT;
  149. #endif
  150.         if(MIREADY)
  151.             return MICHAR;
  152.         if(CIREADY) {
  153.             CICHAR;        /* dismiss character */
  154.             return TIMEOUT;
  155.         }
  156.         if(MIREADY)
  157.             return MICHAR;
  158.         for(Timeout=T1pause; --Timeout; )
  159.             if(MIREADY)
  160.                 return MICHAR;
  161.     }
  162.     return TIMEOUT;
  163. }
  164.  
  165. sendline(data)
  166. char data;
  167. {
  168.     while(!MOREADY)
  169.         ;
  170.     outp(MODATA, data);
  171. }
  172. purgeline()
  173. {
  174.     while(MIREADY)
  175.         MICHAR;
  176. }
  177.  
  178. /* default "autodial" routine */
  179. #ifndef AUTODIAL
  180. dial(name)
  181. char *name;
  182. {
  183.     char *s;
  184. #ifdef CAFPERSONAL
  185.     Sport=(Dport=208)+(SPORT-DPORT);
  186. #endif
  187.     if((s=cisubstr(name, "\tb")))
  188.         if(!setbaud(atoi(s+2)))
  189.             printf("Baudrate set to %u: ", Baudrate);
  190.     printf("%s", name);
  191.     return OK;
  192. }
  193. #endif
  194.