On Thu, 10 Feb 2011, melloannapolis wrote:
> fprintf(stderr,"opening\n");
> if (fd = open(COM2, O_RDWR | O_NOCTTY | O_NDELAY) < 0) {
> fprintf(stderr, "open failed: 0x%x\n", errno);
> exit(errno);
> }
You've opened with O_NDELAY, therefore a read should never hang,
perhaps because later you say are getting fd=0, i.e. stdin! Wierd.
Actually you never check the result of your read() below. With O_NDELAY
set and no data, read() will return -1 with errno set to EAGAIN
Here's a snippet of read code that works fine for me...
for ( n--; N<n && getmillisecs()<tend; ) {
st=read(fd,s,1);
if (st==0 || (st<0 && errno==EAGAIN)) { usleep(1000); continue; }
if (st<0) { return(-1); }
N++;
.... do stuff....
}
You need the usleep or similar or else cpu usage goes thru the roof.
>
> fprintf(stderr,"getattrs\n");
> if (tcgetattr(fd, &oldtio) < 0) {
> fprintf(stderr, "tcgetattr failed: 0x%x\n", errno);
> exit(errno);
> }
>
> bzero(&newtio, sizeof(newtio)); // clear struct for new port settings
>
> /* { */
>
> newtio.c_cflag = B4800 | CS8 | CLOCAL | CREAD;
> newtio.c_iflag = IGNPAR | ICRNL;
> newtio.c_oflag = 0;
> newtio.c_lflag = 0; // wuz ICANON; but we don't want echo going on
>
> newtio.c_cc[VINTR] = 0; // cntrl-c
> newtio.c_cc[VQUIT] = 0; // cntrl-\
> newtio.c_cc[VERASE] = 0; // cntrl-H or del
> newtio.c_cc[VKILL] = 0; // @
> newtio.c_cc[VEOF] = 4; // cntrl-d
> newtio.c_cc[VTIME] = 0; // interchar timer unused, 0 delay
> newtio.c_cc[VMIN] = 1; // wait for up to 1 character
> newtio.c_cc[VSWTC] = 0; // '\0'
> newtio.c_cc[VSTART] = 0; // Ctrl-q
> newtio.c_cc[VSTOP] = 0; // Ctrl-s
> newtio.c_cc[VSUSP] = 0; // Ctrl-z
> newtio.c_cc[VEOL] = 0; // '\0'
> newtio.c_cc[VREPRINT] = 0; // Ctrl-r
> newtio.c_cc[VDISCARD] = 0; // Ctrl-u
> newtio.c_cc[VWERASE] = 0; // Ctrl-w
> newtio.c_cc[VLNEXT] = 0; // Ctrl-v
> newtio.c_cc[VEOL2] = 0; // '\0'
>
> fprintf(stderr, "flushing\n");
> tcflush (fd, TCIFLUSH);
>
> /* } */
>
> /* this doesn't help, same symptom
> cfmakeraw(&newtio);
> cfsetispeed(&newtio, B4800);
> cfsetospeed(&newtio, B4800);
> */
>
> fprintf(stderr, "setting\n"); // we always see this
>
> if (tcsetattr(fd, TCSANOW, &newtio) < 0) {
> fprintf(stderr,"tcsetattr failure: 0x%x\n", errno);
> }
>
> fprintf(stderr, "io control done, start reading\n"); // we never see this
>
> int i, stop = 0;
> char buf[256];
> while (stop == 0) {
> i = read (fd, buf, 255); // line 77 - blocks forever here
> buf[i] = '\0';
> fprintf(stderr,":%s:%d\n", buf, i);
> if (buf[0] == 'z') stop=1;
> }
> tcsetattr(fd, TCSANOW, &oldtio);
>
> fprintf(stderr,"done\n");
> }
> ----------------------------
> termio settings while program is running:
> :tty# stty -a < /dev/ttyAM1
> speed 4800 baud; rows 0; columns 0; line = 0;
> intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
> eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
> lnext = ^V; flush = ^O; min = 1; time = 0;
> -parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
> -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon
> -ixoff -iuclc -ixany -imaxbel
> -opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
> ff0
> -isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop
> -echoprt -echoctl -echoke
>
>
>
------------------------------------
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/ts-7000/join
(Yahoo! ID required)
<*> To change settings via email:
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|