home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / communic / sercom / downl3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-09-04  |  2.4 KB  |  109 lines

  1. #include "stdio.h";
  2.  
  3. FILE *fstart();
  4.  
  5. #define _DATAREADY 1
  6. #define _OVERRUN 2
  7. #define _PARITY 4
  8. #define _FRAME 8
  9. #define _BREAK 16
  10. #define _DTR 1
  11. #define _RTS 2
  12. #define PORTBASE 0x02F8                          /* For COM1. 0x03F8 for CO*/
  13. #define BUFSIZE 512
  14.  
  15. main(argc, argv)
  16. int argc;
  17. char *argv[];
  18. {
  19. FILE *f;
  20. unsigned char parmbyte;
  21. int count;
  22. char buf[BUFSIZE];
  23.  
  24.    f = fstart(argc, argv);
  25.    if (f == NULL) 
  26.       exit();
  27.  
  28. /* Calculate and set the parameter byte */
  29.  
  30.    comparm(1200, 0, 1, 8);                       /* Program 16.6           */
  31.  
  32.    printf("%s \n", "Press any key to start download");
  33.    getch();
  34.  
  35.    printf("%s \n", "Press 'ESC' to stop download");
  36.  
  37.    while (count = readbuf(buf) == BUFSIZE) { 
  38.       if (count > 0)
  39.          fwrite(buf, count, 1, f);
  40.    }
  41.  
  42.    fclose(f);                                    /* Close the File         */
  43.  
  44.    printf("%s \n", "Finished");
  45.       
  46. }
  47.  
  48. readbuf(buf)
  49. char *buf;
  50. {
  51. int count = 0;
  52. int lstat;                                       /* Line Status            */
  53. int mstat;                                       /* Modem Status           */
  54.  
  55.    outp(PORTBASE + 4, _DTR | _RTS);              /* Handshaking on         */
  56.  
  57.    while (count <= BUFSIZE) {
  58.  
  59.       if (kbhit()) {
  60.          if (getch() == 27) 
  61.             return;                              /* Hit ESC                */
  62.       }
  63.  
  64.       lstat = inp(PORTBASE + 5);                 /* Read Line Status       */
  65.  
  66.       if (lstat & _OVERRUN) {
  67.          printf("%s \n", "overrun error");
  68.          return(-1);
  69.       }
  70.  
  71.       if (lstat & _PARITY) {
  72.          printf("%s \n", "parity error");
  73.          return(-1);
  74.       }
  75.  
  76.       if (lstat & _FRAME) {
  77.          printf("%s \n", "framing error");
  78.          return(-1);
  79.       }
  80.  
  81.       if (lstat & _BREAK) {
  82.          printf("%s \n", "break received");
  83.          return(-1);
  84.       }
  85.  
  86. /*
  87. * Read the Modem Status (not needed but here for completeness)
  88. * We can test for incoming handshaking if we wish.
  89. */
  90.       mstat = inp(PORTBASE + 6);
  91.  
  92.       if (!(lstat & _DATAREADY))                 /* Nothing There          */
  93.          continue;
  94.  
  95. /* So we have received a character */
  96.  
  97.       buf[count++] = inp(PORTBASE);              /* read the character     */
  98.    }
  99.  
  100. /* We have filled the buffer, or hit ESCAPE */
  101.  
  102.    outp(PORTBASE + 4, 0);                        /* Handshaking off        */
  103.  
  104.    return(count);
  105. }
  106.  
  107.  
  108. /* Figure 16.10: DOWNL3.C */
  109.