home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sys / next / programm / 7236 < prev    next >
Encoding:
Internet Message Format  |  1992-11-15  |  3.9 KB

  1. Path: sparky!uunet!ukma!wupost!cs.utexas.edu!ut-emx!ccwf.cc.utexas.edu!jdawson
  2. From: jdawson@ccwf.cc.utexas.edu (John Dawson)
  3. Newsgroups: comp.sys.next.programmer
  4. Subject: Re: Serial port communication difficulties
  5. Message-ID: <JDAWSON.92Nov15131012@donald.cc.utexas.edu>
  6. Date: 15 Nov 92 19:10:12 GMT
  7. References: <1992Nov10.115012.29368@doug.cae.wisc.edu>
  8.     <1992Nov11.091546.5251@urz.unibas.ch>
  9.     <1992Nov11.224715.12841@cs.tu-berlin.de>
  10. Sender: news@ut-emx.uucp
  11. Organization: The University of Texas at Austin
  12. Lines: 108
  13. In-reply-to: marcel@opal.cs.tu-berlin.de's message of 11 Nov 92 22:47:15 GMT
  14.  
  15. Victor White writes:
  16.  
  17. > But on the Next, it seems much harder.  I simply thought by
  18. > opening a file being /dev/cua, I could check the ports status
  19. > via feof().  But alas, it always says theres more data there,
  20. > and so it continially gets hung on reading after the lasst
  21. > charachter is read.  I tried all sorts of variations with the
  22. > cable, conecting various permutaions of RTS/CTS and so on, but
  23. > to no avail.
  24. >
  25. > In sum, the Next has no trouble reading the data from the port.
  26. > But its only problem is that it can not find a correct answer
  27. > for the status of the port.
  28. >
  29. > Is there a simple solution or a better way?
  30.  
  31. I believe that the problem you're having is simply that you're
  32. expecting to find an EOF from a device that is a non-closing stream.
  33. feof() works "funny" on sockets and certain devices.  You will never
  34. get an EOF unless the stream gets broken.  I don't know much about
  35. your problem here, but I suspect that a fix would be to code your
  36. loops differently.  Instead of having a loop that looks like this ...
  37.  
  38.     FILE *fp;
  39.  
  40.     fp = fopen("/dev/cua", "rb");
  41.     while (!feof(fp)) {
  42.         /* ... */
  43.     }
  44.  
  45. ... or maybe like this ...
  46.  
  47.     FILE *fp;
  48.     int nread;
  49.     char buf[BUFSIZE];
  50.  
  51.     fp = fopen("/dev/cua", "rb");
  52.  
  53.     for (;;) {
  54.         nread = fread(buf, BUFSIZE, 1, fp);
  55.         if (nread == 0)
  56.             break;  /* No more input ... */
  57.     }
  58.  
  59. , try a poll like this:
  60.  
  61.     FILE *fp;
  62.     int fd;
  63.     int nfound;
  64.     fd_set fdset;
  65.     fd_set readfds;
  66.     struct timeval timeout = { 0L, 0L };
  67.     BOOL input_waiting_on_cua;
  68.  
  69.     fp = fopen("/dev/cua", "rb");
  70.     fd = fileno(fp);
  71.  
  72.     FD_ZERO(&fdset);
  73.     FD_SET(fd, &fdset);
  74.     nfound = select(FD_SETSIZE, &readfds, NULL, NULL, &timeout);
  75.     input_waiting_on_cua = nfound == 1 ? YES : NO;
  76.  
  77. I had a problem with fread() and feof() myself a while back that took
  78. a terribly long time to figure out.  fread() is nice sometimes, but it
  79. oversimplifies the picture of what files really are, file descriptors.
  80. To write code that deals with asynchronous I/O without blocking, there
  81. really isn't any way around using open(), read(), write(), and
  82. select().  Actually, that's a lie.  You could do something like this:
  83.  
  84.     int fd_flags;
  85.     int fd;
  86.  
  87.     fd = /* ... */
  88.  
  89.     fd_flags = fcntl(fd, F_GETFL, 0);
  90.     fd_flags |= FASYNC;
  91.     fcntl(fd, F_SETFL, fd_flags);
  92.     signal(SIGIO, sigio_fn);
  93.  
  94.     /* ... */
  95.  
  96.     int sigio_fn(int signal)
  97.     {
  98.         process_input();
  99.     {
  100.  
  101. ... but it's hard to get signal stuff to work robustly; and if your
  102. program is an AppKit program, it is extremely difficult to write code
  103. that works; The AppKit and signals don't mix well at all.
  104.  
  105. If NeXT were running on a real version of UNIX, you'd have more
  106. options available.  For example, on a Sun, you could use fcntl() to
  107. set a flag called FNDELAY on an open file descriptor that would mark
  108. it as non-blocking.  Then, you could use fread(); it would just return
  109. 0 when there was no more input waiting on your stream.  But NOOOOOOOO
  110. ... If NeXT were even POSIX-compliant, which they said they were going
  111. to be come 3.0, they'd have a similar facility.
  112.  
  113. I'll shut up now.  Your message is several days old anyway; you
  114. probably have already figured out how to solve your problem by
  115. yourself!  :-)
  116.  
  117. ...jkd
  118.  
  119. P.S. -- Email to you bounced for me, too.  I tried sending this mail a
  120. couple of days ago.
  121. --
  122. jdawson@ccwf.cc.utexas.edu (John Dawson)
  123.