home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 330_02 / tsio.c < prev    next >
C/C++ Source or Header  |  1990-10-12  |  3KB  |  151 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. #include <ctype.h>
  9. #include <process.h>
  10.  
  11. #include "tsk.h"
  12. #include "sio.h"
  13. #include "tsksup.h"
  14.  
  15. #define PORT   0x80     /* COM1, relative */
  16. #define BAUD   9600L    /* Baudrate */
  17.  
  18.  
  19. #define STACKSIZE 2048
  20.  
  21. unsigned int _stklen = 4 * STACKSIZE;  /* For Turbo C: Two tasks + main Task Stack */
  22.  
  23. tcb tcb1, tcb2, tcb3;
  24. flag halt;
  25. sioptr siop;
  26.  
  27. word rcvbuf [10000];
  28. byte xmtbuf [100];
  29.  
  30. int endrun, err;
  31.  
  32. /*
  33.    Task 1 reads characters from the serial line and displays them on
  34.    the screen. While the halt flag is set, characters are not read,
  35.    so the XON/XOFF and RTS/CTS protocol can be tested for the receiving 
  36.    side.
  37. */
  38.  
  39. void far task1 (void)
  40. {
  41.    word ch;
  42.  
  43.    printf ("Task 1 started\n");
  44.    while (!endrun)
  45.       {
  46.       wait_flag_clear (&halt, 0L);
  47.       if (endrun)
  48.          return;
  49.       ch = v24_receive (siop, 0L);
  50.       putch (ch);
  51.       if (ch & 0xff00)
  52.          {
  53.          err = 1;
  54.          printf ("\n%c*%02x*", ch, ch >> 8);
  55.          }
  56.       }
  57. }
  58.  
  59.  
  60. /*
  61.    Task 2 reads characters from the keyboard and sends them to the
  62.    serial port. If 'h' is entered, the halt flag is set, so task1
  63.    stops reading. If 'c' is entered, the halt flag is cleared.
  64.    Entering 'e' stops the program.
  65.    'd' outputs snapshot dump.
  66. */
  67.  
  68. void far task2 (void)
  69. {
  70.    int ch;
  71.  
  72.    printf ("Task 2 started\n");
  73.    while (!endrun)
  74.       {
  75.       ch = t_read_key () & 0xff;
  76.       switch (tolower (ch))
  77.          {
  78.          case 'h':   set_flag (&halt);
  79.                      puts ("-halt-");
  80.                      break;
  81.  
  82.          case 'c':   clear_flag (&halt);
  83.                      err = 0;
  84.                      puts ("-continue-");
  85.                      break;
  86.  
  87.          case 'e':   puts ("-end-");
  88.                      endrun = 1;
  89.                      clear_flag (&halt);
  90.                      wake_task (NULL);
  91.                      break;
  92.  
  93. #if (TSK_NAMED)
  94.          case 'd':   snapshot (stdout);
  95.                      break;
  96. #endif
  97.  
  98.          default:    /* putch (ch); */
  99.                      v24_send (siop, (byte)ch, 0L);
  100.                      break;
  101.          }
  102.       }
  103. }
  104.  
  105.  
  106. int main (void)
  107. {
  108.    char stack1 [STACKSIZE];
  109.    char stack2 [STACKSIZE];
  110.  
  111.    endrun = 0;
  112.  
  113.    install_tasker (0, 0, IFL_STD, "TSIO");
  114.    siop = v24_install (PORT, 1, rcvbuf, sizeof (rcvbuf), xmtbuf, sizeof (xmtbuf));
  115.  
  116.    if (siop == NULL)
  117.       {
  118.       remove_tasker ();
  119.       printf ("Couldn't install COM-Port\n");
  120.       exit (1);
  121.       }
  122.    v24_change_baud (siop, BAUD);
  123.    v24_protocol (siop, XONXOFF | RTSCTS, 40, 60);
  124.  
  125.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL TN("TASK1"));
  126.    create_task (&tcb2, task2, stack2, STACKSIZE, PRI_STD, NULL TN("TASK2"));
  127.  
  128.    create_flag (&halt TN("Halt"));
  129.  
  130.    start_task (&tcb1);
  131.    start_task (&tcb2);
  132.  
  133.    preempt_on ();
  134.    t_delay (0L);
  135.  
  136.    endrun = 1;
  137.    puts ("******** Main Task *********");
  138.  
  139.    set_priority (NULL, 10);
  140.    schedule ();
  141.    delete_flag (&halt);
  142.  
  143.    preempt_off ();
  144.    v24_remove (siop, 1);
  145.    remove_tasker ();
  146.  
  147.    puts ("******** End Run *********");
  148.    return 0;
  149. }
  150.  
  151.