public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Problem piping stdout
       [not found] <1159709654.451fc3d67797a@webmail.adelaide.edu.au>
@ 2006-10-01 13:55 ` Owen Lucas
  2006-10-01 16:20   ` John (Eljay) Love-Jensen
       [not found] ` <200610011628.31370.vda.linux@googlemail.com>
  1 sibling, 1 reply; 4+ messages in thread
From: Owen Lucas @ 2006-10-01 13:55 UTC (permalink / raw)
  To: gcc-help


 Im doing a program that reads in stuff from the serial port under linux.
 Once read in there is a bit of formatting and it then needs to get piped
 to something else. At the end is the cut down code.

 anyway the interesting part is in the while loop the rest is just formatting.

   while (1)
     {
      // read(fd,buf,50);
       printf("hello world\n");
     }

 it prints out real fast to screen (as you would expect) and it can be
 piped to a file ./a.out > test.txt resulting in a large text file (as you
 would expect)


 now if the read(fd,buf,50); line is uncommented, it now only does
 printf("hello world\n") everytime a CR is reciveded on the serial port
 which terminates the read function (as it should). Now it displays to
 screen (as it should) but it does NOT pipe to file with ./a.out >
 test.txt. It creates a blank file but doesnt put anything in it. Why does
 a read function unrelated to STDIN or STDOUT in the code muck up a linux
 pipe?????? Got be baffled and dont know what to do now.

 Is this a GCC thing, a BASH thing or a linux thing
 Im lost on this one. Can anyone spot the problem
 (It probably is me but I cant see it)

 owen
 (appologies to the gcc@gcc.gnu.org where I may have accidently posted this. got
a bit confused in the sign up process ie email from gcc-help-help@gcc.gnu.org
for this list and gcc-help@gcc.gnu.org for the other one. I clicked the wrong
post to. woops very sorry)
 -----

 #include <fcntl.h>
 #include <termios.h>
 #include <stdio.h>

 #define BAUDRATE B19200
 #define MODEMDEVICE "/dev/ttyS0"
 #define _POSIX_SOURCE 1 /* POSIX compliant source */


 main()
 {
   int fd;
   struct termios oldtio,newtio;
   char buf[50];

   fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
   if (fd <0) {perror(MODEMDEVICE); exit(-1); }
   tcgetattr(fd,&oldtio); /* save current serial port settings */
   bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
   newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
   newtio.c_iflag = IGNPAR | ICRNL;
   newtio.c_oflag = 0;
   newtio.c_lflag = ICANON;
   tcflush(fd, TCIFLUSH);
   tcsetattr(fd,TCSANOW,&newtio);


   while (1)
     {
      // read(fd,buf,50);
       printf("hello world\n");
     }

   tcsetattr(fd,TCSANOW,&oldtio);  // restore the old port settings
 }




^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: Problem piping stdout
  2006-10-01 13:55 ` Problem piping stdout Owen Lucas
@ 2006-10-01 16:20   ` John (Eljay) Love-Jensen
  0 siblings, 0 replies; 4+ messages in thread
From: John (Eljay) Love-Jensen @ 2006-10-01 16:20 UTC (permalink / raw)
  To: Owen Lucas, gcc-help

Hi Owen,

You probably want to set the terminal into unbuffered mode.

Google turned up...

http://cboard.cprogramming.com/archive/index.php/t-27714.html

Note:  your question is not a GCC question, it's a general Unix question.

Personally, for unbuffered interactive input at the terminal, I like to use ncurses 5.5 <http://www.gnu.org/software/ncurses/>.  The ncurses library handles putting the tty into the correct mode, and restoring back to the regular mode when finished.

If you find that your tty has gone all pear-shaped, here's the magic command to restore it to sanity:
stty sane

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: problem piping stdout
       [not found] ` <200610011628.31370.vda.linux@googlemail.com>
@ 2006-10-02  2:04   ` Owen Lucas
  0 siblings, 0 replies; 4+ messages in thread
From: Owen Lucas @ 2006-10-02  2:04 UTC (permalink / raw)
  To: gcc-help

> Use strace to find out what is going on.

shows what I would expect

> Does it work if you do write(1, "foo bar\n", 8) instead of printf?

YES it does!
So why does that work and not printf?

owen


Quoting Denis Vlasenko <vda.linux@googlemail.com>:
>
> Use strace to find out what is going on.
>
> Does it work if you do write(1, "foo bar\n", 8) instead of printf?
> --
> vda
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: problem piping stdout
       [not found] <043c01c6e568$167abda0$a501a8c0@CAM.ARTIMI.COM>
@ 2006-10-02  2:08 ` Owen Lucas
  0 siblings, 0 replies; 4+ messages in thread
From: Owen Lucas @ 2006-10-02  2:08 UTC (permalink / raw)
  To: gcc-help

yep the fflush (stdout) fixes the problem.
I would assume that what some others wrote about changing the buffering mode
would probably work too.

thanks
owen

Quoting Dave Korn <dave.korn@artimi.com>:
>   Something's gone funny with the stdout buffering.  Maybe related to ICANON.
> Try adding a fflush (stdout) after the printf.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-10-02  2:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1159709654.451fc3d67797a@webmail.adelaide.edu.au>
2006-10-01 13:55 ` Problem piping stdout Owen Lucas
2006-10-01 16:20   ` John (Eljay) Love-Jensen
     [not found] ` <200610011628.31370.vda.linux@googlemail.com>
2006-10-02  2:04   ` problem " Owen Lucas
     [not found] <043c01c6e568$167abda0$a501a8c0@CAM.ARTIMI.COM>
2006-10-02  2:08 ` Owen Lucas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).