home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_9 / term2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-20  |  5.9 KB  |  241 lines

  1.  
  2. // TERM2.C - A menu driven modem terminal program.
  3.  
  4. // I N C L U D E S ///////////////////////////////////////////////////////////
  5.  
  6. #include <io.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11. #include <bios.h>
  12. #include <fcntl.h>
  13. #include <memory.h>
  14. #include <malloc.h>
  15. #include <math.h>
  16. #include <string.h>
  17.  
  18. #include "black9.h"
  19. #include "black3.h"
  20. #include "black5.h"
  21.  
  22. // G L O B A L S ////////////////////////////////////////////////////////////
  23.  
  24. // M A I N ////////////////////////////////////////////////////////////////////
  25.  
  26. void main(void)
  27. {
  28.  
  29. char ch,          // input character
  30.      number[64];  // the phone number that is dialed
  31.  
  32. int done    = 0,  // global exit flag
  33.     sel,          // user menu input
  34.     linked  = 0,  // has a connection been made
  35.     result,       // result of modem commands
  36.     com_port;     // the com port to open
  37.  
  38. printf("\nModem Terminal Communications Program Version 2.0\n\n");
  39.  
  40. // ask the user for the com port
  41.  
  42. printf("\nWhich COM port is your modem attached to 1 or 2?");
  43.  
  44. scanf("%d",&com_port);
  45.  
  46. // depending on users selection open COM 1 or 2
  47.  
  48. if (com_port==1)
  49.     Serial_Open(COM_1,SERIAL_BAUD_2400,SERIAL_PARITY_NONE |
  50.                 SERIAL_BITS_8 | SERIAL_STOP_1);
  51. else
  52.     Serial_Open(COM_2,SERIAL_BAUD_2400,SERIAL_PARITY_NONE |
  53.                 SERIAL_BITS_8 | SERIAL_STOP_1);
  54.  
  55. // main loop
  56.  
  57. // flush modem and serial port
  58.  
  59. Serial_Flush();
  60.  
  61. Initialize_Modem(NULL);
  62.  
  63. printf("\n");
  64.  
  65. // enter event loop
  66.  
  67. while(!done)
  68.      {
  69.  
  70.      // if the a connection hasn't been made then enter into menu
  71.  
  72.      if (!linked)
  73.         {
  74.         printf("\nMain Menu\n");
  75.         printf("\n1 - Make a Call");
  76.         printf("\n2 - Wait for Call");
  77.         printf("\n3 - Hang Up");
  78.         printf("\n4 - Exit");
  79.         printf("\n\nSelect ?");
  80.  
  81.         // get input from user
  82.  
  83.         scanf("%d",&sel);
  84.  
  85.         // what is user trying to do
  86.  
  87.         switch(sel)
  88.               {
  89.  
  90.               case 1:  // make a call
  91.                    {
  92.                    printf("\nNumber to dial?");
  93.                    scanf("%s",number);
  94.  
  95.                    // try and make the connection
  96.  
  97.                    result = Make_Connection(number);
  98.  
  99.                    // was the response code a connect message
  100.  
  101.                    if (result==MODEM_CONNECT_1200 || result==MODEM_CONNECT_2400 ||
  102.                       result==MODEM_CONNECT || result==MODEM_CARRIER_2400)
  103.                       {
  104.                       linked=1;
  105.                       printf("\nEntering Terminal Mode...\n");
  106.                       }
  107.                    else
  108.                    if (result==MODEM_USER_ABORT)
  109.                       {
  110.                       printf("\nUser Aborted!\n");
  111.                       Hang_Up();
  112.                       Serial_Flush();
  113.                       } // end else
  114.  
  115.                    } break;
  116.  
  117.               case 2:  // wait for call
  118.                    {
  119.                    printf("\nWaiting...");
  120.  
  121.                    // wait for a call
  122.  
  123.                    result = Wait_For_Connection();
  124.  
  125.                    // was the response code a connection
  126.  
  127.                    if (result==MODEM_CONNECT_1200 || result==MODEM_CONNECT_2400 ||
  128.                       result==MODEM_CONNECT || result==MODEM_CARRIER_2400)
  129.                       {
  130.                       linked=1;
  131.                       printf("\nEntering Terminal Mode...\n");
  132.                       }
  133.                    else
  134.                    if (result==MODEM_USER_ABORT)
  135.                       {
  136.                       printf("\nUser Aborted!\n");
  137.                       Hang_Up();
  138.                       Serial_Flush();
  139.                       } // end else
  140.  
  141.                    } break;
  142.  
  143.               case 3: // hang up
  144.                    {
  145.                    // drop DTR and flush serial buffer
  146.  
  147.                    Hang_Up();
  148.                    Serial_Flush();
  149.  
  150.                    } break;
  151.  
  152.               case 4: // exit
  153.                    {
  154.                    // set global exit flag
  155.                    done=1;
  156.  
  157.                    } break;
  158.  
  159.               default:break;
  160.  
  161.               } // end switch
  162.  
  163.         }  // end if not linked
  164.  
  165.  
  166.         // once machines are linked, allow bi-directional communication
  167.  
  168.         while(linked==1)
  169.              {
  170.              // try and get a character from local machine
  171.  
  172.              if (kbhit())
  173.                 {
  174.                 // get the character from keyboard
  175.  
  176.                 ch = getch();
  177.  
  178.                 printf("%c",ch);
  179.  
  180.                 // send the character to other machine
  181.  
  182.                 Serial_Write(ch);
  183.  
  184.                 // has user pressed ESC ? if so bail.
  185.  
  186.                 if (ch==27)
  187.                    {
  188.                    linked=0;
  189.                    Hang_Up();
  190.                    Serial_Flush();
  191.                    } // end if esc
  192.  
  193.                 // test for CR, if so add an line feed
  194.  
  195.                 if (ch==13)
  196.                     printf("\n");
  197.  
  198.                 } // end if kbhit
  199.  
  200.              // try and get a character from remote
  201.  
  202.              if (ch = Serial_Read())
  203.                 {
  204.  
  205.                 // print the character to the screen
  206.  
  207.                 printf("%c",ch);
  208.  
  209.                 // if it's a carriage return add a line feed
  210.  
  211.                 if (ch==13)
  212.                     printf("\n");
  213.  
  214.                 // if a esc character is sent from remote then close down
  215.  
  216.                 if (ch==27)
  217.                    {
  218.                    printf("\nRemote Machine Closing Connection.");
  219.                    linked=0;
  220.                    Hang_Up();
  221.                    Serial_Flush();
  222.  
  223.                    } // end if remote close
  224.  
  225.                 } // end if serial read
  226.  
  227.              } // end while linked
  228.  
  229.      } // end if not done
  230.  
  231. // close the connection and blaze
  232.  
  233. // break connection just in case
  234.  
  235. Hang_Up();
  236.  
  237. Serial_Close();
  238.  
  239. } // end main
  240.  
  241.