home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctask.zip / TSIO.C < prev    next >
C/C++ Source or Header  |  1988-03-01  |  3KB  |  127 lines

  1.  
  2. /*
  3.    Test program for checking the CTask serial I/O interface.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <conio.h>
  8.  
  9. #include "tsk.h"
  10. #include "sio.h"
  11.  
  12. #define PORT   0        /* COM1 */
  13. #define BAUD   9600L    /* Baudrate */
  14.  
  15.  
  16. #define STACKSIZE 2048
  17.  
  18. word _stklen = 3 * STACKSIZE;  /* For Turbo C: Two tasks + main Task Stack */
  19.  
  20. tcb tcb1, tcb2;
  21. flag halt;
  22.  
  23. word rcvbuf [100];
  24. byte xmtbuf [100];
  25.  
  26. int endrun;
  27.  
  28. /*
  29.    Task 1 reads characters from the serial line and displays them on
  30.    the screen. While the halt flag is set, characters are not read,
  31.    so the XON/XOFF and RTS/CTS protocol can be tested for the receiving 
  32.    side.
  33. */
  34.  
  35. void far task1 (void)
  36. {
  37.    word ch;
  38.  
  39.    printf ("Task 1 started\n");
  40.    while (!endrun)
  41.       {
  42.       wait_flag_clear (&halt, 0L);
  43.       if (endrun)
  44.          return;
  45.       ch = v24_receive (0, 0L);
  46.       putch (ch);
  47.       }
  48. }
  49.  
  50.  
  51. /*
  52.    Task 2 reads characters from the keyboard and sends them to the
  53.    serial port. If 'h' is entered, the halt flag is set, so task1
  54.    stops reading. If 'c' is entered, the halt flag is cleared.
  55.    Entering 'e' stops the program.
  56. */
  57.  
  58. void far task2 (void)
  59. {
  60.    int ch;
  61.  
  62.    printf ("Task 2 started\n");
  63.    while (!endrun)
  64.       {
  65.       ch = t_read_key () & 0xff;
  66.       switch (ch)
  67.          {
  68.          case 'h':   set_flag (&halt);
  69.                      puts ("-halt-");
  70.                      break;
  71.  
  72.          case 'c':   clear_flag (&halt);
  73.                      puts ("-continue-");
  74.                      break;
  75.  
  76.          case 'e':   puts ("-end-");
  77.                      endrun = 1;
  78.                      clear_flag (&halt);
  79.                      wake_task (NULL);
  80.                      break;
  81.  
  82.          default:    putch (ch);
  83.                      v24_send (0, ch, 0L);
  84.                      break;
  85.          }
  86.       }
  87. }
  88.  
  89.  
  90.  
  91. main ()
  92. {
  93.    char stack1 [STACKSIZE];
  94.    char stack2 [STACKSIZE];
  95.  
  96.    endrun = 0;
  97.  
  98.    install_tasker (0, 0);
  99.    v24_install (PORT, rcvbuf, sizeof (rcvbuf), xmtbuf, sizeof (xmtbuf));
  100.    v24_change_baud (PORT, BAUD);
  101.    v24_protocol (PORT, XONXOFF | RTSCTS, 40, 60);
  102.  
  103.    create_task (&tcb1, task1, stack1, STACKSIZE, 100, NULL);
  104.    create_task (&tcb2, task2, stack2, STACKSIZE, 100, NULL);
  105.  
  106.    create_flag (&halt);
  107.    start_task (&tcb1);
  108.    start_task (&tcb2);
  109.  
  110.    preempt_on ();
  111.    t_delay (0L);
  112.  
  113.    endrun = 1;
  114.    puts ("******** Main Task *********");
  115.  
  116.    set_priority (NULL, 10);
  117.    schedule ();
  118.    delete_flag (&halt);
  119.  
  120.    preempt_off ();
  121.    v24_remove (PORT);
  122.    remove_tasker ();
  123.  
  124.    puts ("******** End Run *********");
  125. }
  126.  
  127.