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