home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR23 / TOUCH2.ZIP / COMM.C < prev    next >
C/C++ Source or Header  |  1993-08-14  |  5KB  |  289 lines

  1. /*
  2.  * comm.c - COM[12] interface
  3.  *
  4.  * V. Abell
  5.  */
  6.  
  7. /*
  8.  * Copyright 1993 Victor A. Abell, Lafayette, Indiana  47906.  All rights
  9.  * reserved.
  10.  *
  11.  * Written by Victor A. Abell.
  12.  *
  13.  * Permission is granted to anyone to use this software for any purpose on
  14.  * any computer system, and to alter it and redistribute it freely, subject
  15.  * to the following restrictions:
  16.  *
  17.  * 1. Victor A. Abell is not responsible for any consequences of the use of
  18.  * this software.
  19.  *
  20.  * 2. The origin of this software must not be misrepresented, either by
  21.  *    explicit claim or by omission.  Credit to Victor A. Abell must
  22.  *    appear in documentation and sources.
  23.  *
  24.  * 3. Altered versions must be plainly marked as such, and must not be
  25.  *    misrepresented as being the original software.
  26.  *
  27.  * 4. This notice may not be removed or altered.
  28.  */
  29. #ifndef lint
  30. static char copyright[] =
  31. "@(#) Copyright 1993 Victor A. Abell.\nAll rights reserved.\n";
  32. #endif
  33.  
  34. #include "touch2.h"
  35.  
  36. extern struct videoconfig Vc;
  37.  
  38. #define    BAUDLN    8
  39. #define COL    20
  40. #define EXITLN    18
  41. #define    FRAMELN    12
  42. #define PARLN    14
  43. #define PORTLN    10
  44. #define    STSTLN    16
  45.  
  46. short Baud[] = { 9600, 2400, 1200, 300, 0 };
  47.  
  48. struct parity Par[] = {
  49.     { 0, "none" },
  50.     { 1, "even" },
  51.     { 2, "odd"  },
  52.     { 0, NULL },
  53. };
  54.  
  55. short Baudx = 0;
  56. short Comopen = 0;
  57. short Frame = 8;
  58. short Parx = 0;
  59. short Port = 0;
  60. short StSt = 1;
  61.  
  62.  
  63. struct menu ComPar[] = {
  64.     {  2, 20, "Communications parameters:" },
  65.     {  0,  0, NULL },
  66. };
  67.  
  68.  
  69. static void DisBaud();
  70. static void DisFrame();
  71. static void DisPar();
  72. static void DisPort();
  73. static void DisStst();
  74.  
  75.  
  76. /*
  77.  * CloseCom() - close COM[12] port
  78.  */
  79.  
  80. void
  81. CloseCom()
  82. {
  83.     if (Comopen) {
  84.         (void) close_com();
  85.         Comopen = 0;
  86.     }
  87. }
  88.  
  89.  
  90. /*
  91.  * DefCom() - define COM[12] parameters
  92.  */
  93.  
  94. void
  95. DefCom()
  96. {
  97.     int ch;
  98.  
  99.     DispMenu(ComPar, NULL);
  100.     DisBaud();
  101.     DisFrame();
  102.     DisPar();
  103.     DisPort();
  104.     DisStst();
  105.     _settextposition(EXITLN, COL);
  106.     _outtext("X - eXit");
  107.     for (;;) {
  108.         if ( ! kbhit()) {
  109.             reset_buffer();
  110.             continue;
  111.         }
  112.         ch = getch();
  113.         switch(ch) {
  114.  
  115.         case ESC:
  116.         case 'x':
  117.         case 'X':
  118.             return;
  119.  
  120.         case 'b':
  121.         case 'B':
  122.             Baudx++;
  123.             if (Baud[Baudx] == 0)
  124.                 Baudx = 0;
  125.             DisBaud();
  126.             break;
  127.  
  128.         case 'c':
  129.         case 'C':
  130.             Port = (Port == 0) ? 1 : 0;
  131.             DisPort();
  132.             break;
  133.  
  134.         case 'f':
  135.         case 'F':
  136.             Frame = (Frame == 8) ? 7 : 8;
  137.             DisFrame();
  138.             break;
  139.  
  140.         case 'p':
  141.         case 'P':
  142.             Parx++;
  143.             if (Parx > 2)
  144.                 Parx = 0;
  145.             DisPar();
  146.             break;
  147.  
  148.         case 's':
  149.         case 'S':
  150.             StSt = (StSt == 1) ? 2 : 1;
  151.             DisStst();
  152.             break;
  153.  
  154.         case 0:
  155.             ch = getch();
  156.             /* fall through */
  157.         default:
  158.             putch(BELL);
  159.         }
  160.     }
  161. }
  162.  
  163.  
  164. /*
  165.  * DisBaud() - display baud rate
  166.  */
  167.  
  168. static void
  169. DisBaud()
  170. {
  171.     char b[64];
  172.  
  173.     (void) sprintf(b, "B - baud rate: %d", Baud[Baudx]);
  174.     ClearRow(BAUDLN, COL);
  175.     _settextposition(BAUDLN, COL);
  176.     _outtext(b);
  177. }
  178.  
  179.  
  180. /*
  181.  * DisFrame() - display frame size
  182.  */
  183.  
  184. static void
  185. DisFrame()
  186. {
  187.     char b[64];
  188.  
  189.     (void) sprintf(b, "F - frame size : %d", Frame);
  190.     ClearRow(FRAMELN, COL);
  191.     _settextposition(FRAMELN, COL);
  192.     _outtext(b);
  193. }
  194.  
  195.  
  196. /*
  197.  * DisPar() - display parity
  198.  */
  199.  
  200. static void
  201. DisPar()
  202. {
  203.     char b[64];
  204.  
  205.     (void) sprintf(b, "P - parity: %s", Par[Parx].nm);
  206.     ClearRow(PARLN, COL);
  207.     _settextposition(PARLN, COL);
  208.     _outtext(b);
  209. }
  210.  
  211.  
  212. /*
  213.  * DisPort() - display COM port
  214.  */
  215.  
  216. static void
  217. DisPort()
  218. {
  219.     char b[64];
  220.  
  221.     (void) sprintf(b, "C - COM port: %d", Port + 1);
  222.     ClearRow(PORTLN, COL);
  223.     _settextposition(PORTLN, COL);
  224.     _outtext(b);
  225. }
  226.  
  227.  
  228. /*
  229.  * DisStst() - display start/stop bit count
  230.  */
  231.  
  232. static void
  233. DisStst()
  234. {
  235.     char b[64];
  236.  
  237.     (void) sprintf(b, "S - start/stop bits: %d", StSt);
  238.     ClearRow(STSTLN, COL);
  239.     _settextposition(STSTLN, COL);
  240.     _outtext(b);
  241. }
  242.  
  243.  
  244. /*
  245.  * OpenCom() - open the COM[12] port
  246.  */
  247.  
  248. void
  249. OpenCom()
  250. {
  251.     int err;
  252.     char msg[128];
  253.  
  254.     if (Comopen)
  255.         return;
  256.     open_com(Port, Baud[Baudx], Par[Parx].val, StSt, Frame, &err);
  257.     if (err) {
  258.         (void) sprintf(msg, "Can't open COM%d port: error %d",
  259.             Port + 1, err);
  260.         _clearscreen(_GCLEARSCREEN);
  261.         _settextposition(12, 25);
  262.         _outtext(msg);
  263.         PromptMsg("Press any key to exit.");
  264.         (void) WaitAnyKey();
  265.         TouchExit(1);
  266.     }
  267.     Comopen = 1;
  268. }
  269.  
  270.  
  271. /*
  272.  * WaitAnyKey() - wait for any key press
  273.  */
  274.  
  275. int
  276. WaitAnyKey()
  277. {
  278.     int ch;
  279.  
  280.     for (;;) {
  281.         if (kbhit()) {
  282.             if ((ch = getch()) == 0)
  283.                 ch = getch();
  284.             return(ch);
  285.         }
  286.         reset_buffer();
  287.     }
  288. }
  289.