home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ISC366.ZIP / SERIAL / EXAMPLES / MODEM.CPP < prev    next >
Text File  |  1993-09-01  |  4KB  |  158 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. //                                                                       //
  3. //            File: modem.cpp                                            //
  4. //            started on: 24/2/92                                        //
  5. //                                                                       //
  6. ///////////////////////////////////////////////////////////////////////////
  7. //                                                                       //
  8. //                                                                       //                                                                     //
  9. ///////////////////////////////////////////////////////////////////////////
  10.  
  11. #include <bios.h>;   // biostime.
  12. #include <dos.h>;    // inportb, delay.
  13. #include "modem.h";  // MODEM.
  14.  
  15. // this class is serial port compatible submitting to DSRs and CTSs from
  16. // modems on receive.
  17.  
  18.  
  19. // extending the init.
  20. //
  21. void MODEM::extended_init(void)
  22. {
  23.     // turn on DTR, RTS.
  24.     //
  25.     set_controls(0x3);
  26. }
  27.  
  28. // destructor.
  29. //
  30. MODEM::~MODEM()
  31. {
  32.     // turn off DTR, RTS.
  33.     //
  34.     set_controls(0);
  35. }
  36.  
  37. void MODEM::flow_check(void)
  38. {
  39.     // check flow control.
  40.     // if both DSR && CTS are activated:- flow is enabled.
  41.     //
  42.     flow_enabled= ((get_MSR()& 0x30)== 0x30)?TRUE: FALSE;
  43. }
  44.  
  45. void MODEM::wait(unsigned miliseconds)
  46. {
  47.     unsigned long start= biostime(0, 0L);
  48.  
  49.     do {
  50.        if (!(get_MSR()& 0x80))
  51.           return;
  52.     } while ((biostime(0, 0L)- start* 55)< miliseconds);
  53. }
  54.  
  55. void MODEM::disconnect( BOOLEAN boot_enable,
  56.                         const char *escape_sequence,
  57.                         const char *disconnect_string,
  58.                         const char *disconnect_reply_string,
  59.                         const unsigned timeout)
  60. {
  61.     INDEX i;
  62.  
  63.     empty_io_buffers();
  64.  
  65.     for (i=0; escape_sequence[i]!='\x0'; i++) {
  66.         if (escape_sequence[i]!='~')
  67.            *this<< escape_sequence[i];
  68.         else
  69.             delay (500);
  70.     }
  71.     for (i=0; disconnect_string[i]!='\x0'; i++) {
  72.         if (disconnect_string[i]!='~')
  73.            *this<< disconnect_string[i];
  74.         else
  75.             delay(500);
  76.     }
  77.  
  78.     if ((get_MSR()& 0x80)) {
  79.        set_controls(0);
  80.        wait (2000);
  81.        set_controls(0x3);
  82.     }
  83.     else { // disconnected...
  84.          empty_io_buffers();
  85.          return;
  86.     }
  87.  
  88.  
  89.     // WAIT FOR OK_STRING.
  90.     long begin_clock, timeout_begin_clock;
  91.     BYTE temp;
  92.  
  93.     i= 0;
  94.     timeout_begin_clock= begin_clock= biostime(0, 0L);
  95.  
  96.     do {
  97.        if (!disconnect_reply_string[i]) // finished receiving OK string.
  98.           break;
  99.  
  100.        if (*this>> temp) { // if a byte is available check it.
  101.           if (temp== disconnect_reply_string[i])
  102.              i++;
  103.           else {
  104.                i= 0;
  105.                if (temp== disconnect_reply_string[i]) // could be that the char was the start of the string...
  106.                   i++;
  107.           }
  108.  
  109.           begin_clock= biostime(0,0L); // a byte was received (reset pend_ticks timeout).
  110.        }
  111.     } while (((biostime(0,0L)- begin_clock)< timeout)&&
  112.              ((biostime(0,0L)- timeout_begin_clock)< timeout));
  113.  
  114.     if (disconnect_reply_string[i]) { // did not receive OK string.
  115.        // now, if line is still up, boot!
  116.        //
  117.        if ((get_MSR()& 0x80))
  118.           if (boot_enable)
  119.              ; // boot();
  120.     }
  121. }
  122.  
  123. void MODEM::msr_int(const BYTE msr)
  124. {
  125.     if ((msr& 0x44)== 0x44) // ring is up and the interrupt is because of it.
  126.        ring_int (msr);
  127.  
  128.     if (msr& 0x3)   // DSR or CTS changed!
  129.        modem_status_change (msr);
  130.  
  131.     if ((msr& 0x8)) // DCD changed.
  132.        dcd_status_change (msr);
  133. }
  134.  
  135. void MODEM::dcd_status_change(BYTE /* msr */)
  136. {
  137.     // if DCD or CTS changed...
  138.  
  139.     // to be overloaded.
  140. }
  141.  
  142. void MODEM::modem_status_change(BYTE /* msr */)
  143. {
  144.     // if DSR or CTS changed...
  145.  
  146.     // to be overloaded.
  147. }
  148.  
  149. void MODEM::ring_int(BYTE /* msr */)
  150. {
  151.     // to be overloaded.
  152. }
  153.  
  154. BYTE MODEM::get_MSR(void)
  155. {
  156.     return ((BYTE) inportb(com_port+ MSR));
  157. }
  158.