home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MODEMIO.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  5KB  |  244 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** This source is released to the Public
  5. ** domain on December 16, 1992.
  6. **
  7. ** Curtis Paris
  8. ** Internet: cparis@comtch.spk.wa.usa
  9. **
  10. ** modified 12-Mar-94 by Bob Stout - Added pc-port.h from SNIPPETS
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <dos.h>
  15. #include <ctype.h>
  16. #include "extkword.h"
  17.  
  18. #define MODEMIO_INIT
  19. #include "modemio.h"
  20.  
  21. void (INTERRUPT FAR *old_modem_isr)(void);
  22.  
  23. /*********************************************************************/
  24. void FAR interrupt modem_isr(void)
  25. {
  26.   unsigned char c;
  27.  
  28.   enable();
  29.  
  30.   if (modem_buffer_count<1024) {
  31.     c=inp(modem_base);
  32.     if (((c==XON) || (c==XOFF)) && (modem_xon_xoff)) {
  33.       switch(c) {
  34.         case XON :modem_pause=0; break;
  35.         case XOFF:modem_pause=1; break;
  36.       }
  37.     } else {
  38.       modem_pause=0;
  39.       modem_buffer[modem_buffer_head++]=c;
  40.       if (modem_buffer_head>=MAX_BUFFER) modem_buffer_head=0;
  41.       modem_buffer_count++;
  42.     }
  43.     modem_overflow=0;
  44.   } else {
  45.     modem_overflow=1;
  46.   }
  47.   disable();
  48.   outp(0x20,0x20);
  49. }
  50.  
  51. int com_carrier(void)
  52. {
  53.   int x;
  54.  
  55.   if (!modem_open) return(0);
  56.   if ((inp(modem_base+6) & 0x80)==128) return(1);
  57.  
  58.   for (x=0; x<500; x++) {
  59.     if ((inp(modem_base+6) & 0x80)==128) return(1);
  60.   }
  61.   return(0);
  62. }
  63.  
  64. int com_ch_ready(void)
  65. {
  66.   if (!modem_open) return(0);
  67.   if (modem_buffer_count!=0) return(1);
  68.   return(0);
  69. }
  70.  
  71. int com_read_ch(void)
  72. /* This returns EOF if the port hasn't been opened, or if no character
  73.  * is waiting.  Otherwise it returns the next character from the port.
  74.  */
  75. {
  76.   unsigned char ch;
  77.  
  78.   if (!modem_open) return(EOF);
  79.   if (!com_ch_ready()) return(EOF);
  80.   ch=modem_buffer[modem_buffer_tail];
  81.   modem_buffer[modem_buffer_tail]=0;
  82.   modem_buffer_count--;
  83.   if (++modem_buffer_tail>=MAX_BUFFER) modem_buffer_tail=0;
  84.   return(ch);
  85. }
  86.  
  87. void com_send_ch(unsigned char ch)
  88. {
  89.   if (!modem_open) return;
  90.   outp(modem_base+4,0x0B);
  91.   if (modem_rts_cts) {
  92.     while((inp(modem_base+6) & 0x10)!=0x10) ; /* Wait for Clear to Send */
  93.   }
  94.   while((inp(modem_base+5) & 0x20)!=0x20) ;
  95.   if (modem_xon_xoff) {
  96.     while((modem_pause) && (com_carrier())) ;
  97.   }
  98.   outp(modem_base,ch);
  99. }
  100.  
  101.  
  102. void com_parity(char p)
  103. {
  104.   int x, newb=0;
  105.  
  106.   if (!modem_open) return;
  107.   x=inp(modem_base+3);
  108.  
  109.   newb=((x>>6)<<6)+((x<<5)>>5); /* Get rid of old parity */
  110.  
  111.   switch(toupper(p)) {
  112.     case 'N':newb+=0x00; break; /* None  */
  113.     case 'O':newb+=0x08;break;  /* Odd   */
  114.     case 'E':newb+=0x18; break; /* Even  */
  115.     case 'M':newb+=0x28;break;  /* Mark  */
  116.     case 'S':newb+=0x38;break;  /* Space */
  117.   }
  118.  
  119.   outp(modem_base+3, newb);
  120. }
  121.  
  122. void com_data_bits(unsigned char bits)
  123. {
  124.   int x, newb=0;
  125.  
  126.   if (!modem_open) return;
  127.   x=inp(modem_base+3);
  128.  
  129.     newb=((x>>2)<<2); /* Get rid of the old Data Bits */
  130.  
  131.   switch(bits) {
  132.     case 5 :newb+=0x00; break;
  133.     case 6 :newb+=0x01; break;
  134.     case 7 :newb+=0x02; break;
  135.     default:newb+=0x03; break;
  136.   }
  137.  
  138.   outp(modem_base+3,newb);
  139. }
  140.  
  141. void com_stop_bits(unsigned char bits)
  142. {
  143.   int x, newb=0;
  144.  
  145.   if (!modem_open) return;
  146.   x=inp(modem_base+3);
  147.  
  148.   newb=((x<<6)>>6)+((x>>5)<<5); /* Kill the old Stop Bits */
  149.  
  150.   if (bits==2) newb+=0x04; /* Only check for 2, assume 1 otherwise */
  151.  
  152.   outp(modem_base+3,newb);
  153. }
  154.  
  155. void com_speed(long speed)
  156. {
  157.   int x;
  158.     char l, m;
  159.     int d;
  160.  
  161.   if (!modem_open) return;
  162.  
  163.   x=inp(modem_base+3); /* Read In Old Stats */
  164.  
  165.   if ((x & 0x80)!=0x80) outp(modem_base+3,x+0x80); /* Set DLab On */
  166.  
  167.   d=(int)(115200L/speed);
  168.   l=d & 0xFF;
  169.   m=(d >> 8) & 0xFF;
  170.  
  171.   outp(modem_base+0,l);
  172.   outp(modem_base+1,m);
  173.  
  174.   outp(modem_base+3,x); /* Restore the DLAB bit */
  175. }
  176.  
  177. int com_open(int comport, long speed, int data_bit, unsigned char parity,
  178.       unsigned char stop_bit)
  179. {
  180.   int x;
  181.  
  182.   disable();
  183.   if (modem_open) com_close();
  184.   modem_port=comport;
  185.  
  186.   switch(modem_port) {
  187.     case 1:modem_base=0x3F8; modem_irq=4; modem_vect=0x0C; break;
  188.     case 2:modem_base=0x2F8; modem_irq=3; modem_vect=0x0B; break;
  189.     case 3:modem_base=0x3E8; modem_irq=4; modem_vect=0x0B; break;
  190.     case 4:modem_base=0x2E8; modem_irq=3; modem_vect=0x0C; break;
  191.     case 5:break;
  192.     default:modem_base=0x3F8; modem_irq=4; modem_vect=0x0C; break;
  193.   }
  194.  
  195.   outp(modem_base+1,0x00);
  196.   if (inp(modem_base+1)!=0) {
  197.     enable();
  198.     return(0);
  199.   }
  200.  
  201.   /* Set up the Interrupt Info */
  202.   old_modem_ier=inp(modem_base+1);
  203.   outp(modem_base+1,0x01);
  204.  
  205.   old_modem_isr=(void (INTERRUPT FAR *)(void))getvect(modem_vect);
  206.   setvect(modem_vect,modem_isr);
  207.  
  208.   if (modem_rts_cts) {
  209.     outp(modem_base+4,0x0B);
  210.   } else {
  211.     outp(modem_base+4,0x09);
  212.   }
  213.  
  214.   old_modem_imr=inp(I8088_IMR);
  215.   outp(I8088_IMR,old_modem_imr & ((1 << modem_irq) ^ 0x00FF));
  216.  
  217.   for (x=1; x<=5; x++) inp(modem_base+x);
  218.  
  219.   modem_open=1;
  220.  
  221.   modem_buffer_count=0;
  222.   modem_buffer_head=0;
  223.   modem_buffer_tail=0;
  224.  
  225.   com_speed(speed);
  226.   com_data_bits((unsigned char)data_bit);
  227.   com_parity(parity);
  228.   com_stop_bits(stop_bit);
  229.   enable();
  230.   return(1);
  231. }
  232.  
  233. void com_close(void)
  234. {
  235.   if (!modem_open) return;
  236.  
  237.   outp(modem_base+1,old_modem_ier);
  238.   outp(I8088_IMR, old_modem_imr);
  239.  
  240.   setvect(modem_vect, old_modem_isr);
  241.   outp(0x20,0x20);
  242.   modem_open=0;
  243. }
  244.