home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / sys / amiga / programm / 12339 < prev    next >
Encoding:
Text File  |  1992-08-14  |  2.3 KB  |  86 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: sparky!uunet!mcsun!news.funet.fi!ousrvr.oulu.fi!ousrvr!nix
  3. From: nix@zombie.oulu.fi (Tero Manninen)
  4. Subject: Re: console device raw mode
  5. In-Reply-To: jdickson@jato.jpl.nasa.gov's message of Fri, 14 Aug 1992 18:02:24 GMT
  6. Message-ID: <NIX.92Aug15110928@zombie.oulu.fi>
  7. Lines: 69
  8. Sender: news@ousrvr.oulu.fi
  9. Reply-To: nix@stekt.oulu.fi
  10. Organization: University of Oulu, Finland
  11. References: <DEREKN.92Aug12113036@vw.ece.cmu.edu> <NIX.92Aug14014127@zombie.oulu.fi>
  12.     <1992Aug14.114648.9446@syma.sussex.ac.uk>
  13.     <1992Aug14.180224.2182@jato.jpl.nasa.gov>
  14. Distribution: comp
  15. Date: Sat, 15 Aug 1992 09:09:28 GMT
  16.  
  17.  
  18. On Fri, 14 Aug 1992 18:02:24 GMT,
  19. jdickson@jato.jpl.nasa.gov (Jeff Dickson) said:
  20.  
  21. Jeff> Yes, the original question was about how to read from a RAW with dos.library.
  22.  
  23. Jeff> The reason Read() hangs, is because it will block until the number of char-
  24. Jeff> acters you specified to be read are read! There's no way to judge how many
  25. Jeff> characters may have been input, because unlike some of the other devices (i.e.
  26. Jeff> serial device) a kind of QUERY command (i.e. SCMD_QUERY) is not supported.
  27.  
  28. Ok, how about this then:
  29. #include <exec/types.h>
  30. #include <dos/dos.h>
  31. #include <signal.h>
  32.  
  33. BPTR con = 0;
  34.  
  35. void sigint_handler() {
  36.   if (con) {
  37.     SetMode(con, 0);
  38.     Close(con);
  39.   }
  40.   exit(0);
  41. }
  42.  
  43. int main(void)
  44. {
  45.   signal(SIGINT, sigint_handler);
  46.  
  47.   con = Open("CONSOLE:", MODE_OLDFILE);
  48.   if (con) {
  49.     SetMode(con, 1);
  50.  
  51.     for (;;) {
  52.       char buf[5], numbuf[10];
  53.       long n;
  54.       n = Read(con, buf, sizeof(buf));
  55.       if (n) {
  56.         int i;
  57.         for (i = 0; i < n; i++) {
  58.           sprintf(numbuf, "%02x,", buf[i]);
  59.           Write(con, numbuf, 3);
  60.         }
  61.       }
  62.       if (buf[0] == 'q') break;
  63.     }
  64.  
  65.     SetMode(con, 0);
  66.     Close(con);
  67.   }
  68.   return (0);
  69. }
  70.  
  71. This works quite well, producing for example following output
  72. (one hex number after each key press):
  73. SRC:test> raw
  74. 61,62,63,64,65,66,67,71,SRC:test>
  75. SRC:test>
  76.  
  77. If you dont want the behaviour that Read does not return until
  78. there is at least one character available you can check the
  79. file handler by calling WaitForChar(fh, delay_time_in_usec).
  80. In case there won't be character available in check time,
  81. WaitForChar returns 0.  I guess you could use this even in
  82. normal line buffered mode ?  but Read() would then hang for
  83. waithing the linefeed :-/
  84.  
  85. ++Tero
  86.