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

  1.  
  2. // TERM1.C - A simple serial communications 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.  
  31. int done    = 0,  // global exit flag
  32.     sel,          // user menu input
  33.     com_port;     // the com port to open
  34.  
  35. printf("\nSerial Communications Program Version 1.0\n\n");
  36.  
  37. // ask the user for the com port
  38.  
  39. printf("\nWhich COM port is your modem attached to 1 or 2?");
  40.  
  41. scanf("%d",&com_port);
  42.  
  43. // depending on users selection open COM 1 or 2
  44.  
  45. if (com_port==1)
  46.     Serial_Open(COM_1,SERIAL_BAUD_2400,SERIAL_PARITY_NONE |
  47.                 SERIAL_BITS_8 | SERIAL_STOP_1);
  48. else
  49.     Serial_Open(COM_2,SERIAL_BAUD_2400,SERIAL_PARITY_NONE |
  50.                 SERIAL_BITS_8 | SERIAL_STOP_1);
  51.  
  52. // main loop
  53.  
  54. // flush serial port
  55.  
  56. Serial_Flush();
  57.  
  58. printf("\n");
  59.  
  60. // enter event loop
  61.  
  62. while(!done)
  63.      {
  64.      // try and get a character from local machine
  65.  
  66.      if (kbhit())
  67.         {
  68.         // get the character from keyboard
  69.  
  70.         ch = getch();
  71.  
  72.         printf("%c",ch);
  73.  
  74.         // send the character to other machine
  75.  
  76.         Serial_Write(ch);
  77.  
  78.         // has user pressed ESC ? if so bail.
  79.  
  80.         if (ch==27)
  81.            {
  82.            Serial_Flush();
  83.            done=1;
  84.            } // end if esc
  85.  
  86.         // test for CR, if so add an line feed
  87.  
  88.         if (ch==13)
  89.             printf("\n");
  90.  
  91.         } // end if kbhit
  92.  
  93.      // try and get a character from remote
  94.  
  95.      if (ch = Serial_Read())
  96.         {
  97.  
  98.         // print the character to the screen
  99.  
  100.         printf("%c",ch);
  101.  
  102.         // if it's a carriage return add a line feed
  103.  
  104.         if (ch==13)
  105.             printf("\n");
  106.  
  107.         // if a esc character is sent from remote then close down
  108.  
  109.         if (ch==27)
  110.            {
  111.            printf("\nRemote Machine Closing Connection.");
  112.            Serial_Flush();
  113.            done=1;
  114.  
  115.            } // end if remote close
  116.  
  117.         } // end if serial read
  118.  
  119.      } // end if not done
  120.  
  121. // close the connection and blaze
  122.  
  123. Serial_Close();
  124.  
  125. } // end main
  126.  
  127.