[Bug c/12722] New: ioctl not returning number of bytes in /dev/ttyS0
sweathers at toptech dot com
gcc-bugzilla@gcc.gnu.org
Wed Oct 22 12:30:00 GMT 2003
PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12722
Summary: ioctl not returning number of bytes in /dev/ttyS0
Product: gcc
Version: 3.2.3
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: sweathers at toptech dot com
CC: gcc-bugs at gcc dot gnu dot org
Description of problem:
It is my understanding that the FIONREAD should return the number of bytes
in /dev/ttyS0. I am try to do an intelligent read for the number of bytes in
serial port buffer so I don't have to wait for a time out to occur. When I
use ioctl( comm_fd, FIONREAD, &nchar ) it does not return me the number of
bytes in the serial port buffer.
Any in site would be of great help. If this is the wrong place to ask this
question please direction to the correct place.
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/ioctl.h>
char comm_log_buf[5000];
int wcomm_fd,
comm_fd;
extern unsigned old_mode;
int caught_alrm = 0;
//-----------------------------------------------------------------------------
int tty_open (char *line, int flg)
{
int fd;
char devname[100];
strcpy(devname, "/dev/");
strcat(devname, line);
fd = open (devname, flg);
if (fd == -1)
{
printf("error (%d) opening %s - [%s]\n", errno, devname,
strerror(errno));
return(errno);
}
if(isatty(fd) == 0)
{
fprintf(stderr, "%s is not a tty\n", devname);
return(-1);
}
return(fd);
}
//-----------------------------------------------------------------------------
static void sig_alrm(int signo)
{
caught_alrm = 1;
return;
}
//-----------------------------------------------------------------------------
size_t my_get_com_byte(int fd, char *buf)
{
for ( ; ; )
{
if(caught_alrm)
{
fprintf(stderr, "\nAlarm Read Timeout...\n");
return(-1);
}
if(read(fd, buf, 1) == 1)
{
//fprintf(stderr, "Read more than one byte...\n");
return (1);
}
if(errno == EINTR && caught_alrm)
{
fprintf(stderr, "\nRead Timeout after read...\n");
return(-1);
}
}
}
//-----------------------------------------------------------------------------
int dev_str(int fd, char *ptr)
{
int nchar,
i;
if(signal(SIGALRM, sig_alrm) == SIG_ERR)
{
fprintf(stderr, "signal SIGLARM error in dev_str");
return(-1);
}
caught_alrm = 0;
alarm(1);
if( (i = ioctl( fd, FIONREAD, &nchar )) == -1)
return(-1);
fprintf(stdout, "Found %d %d bytes in port buffer\n",i, nchar);
for(i = 1; i <= 80; i++)
{
if (my_get_com_byte(fd, &ptr[i]) == -1)
break;
if(ptr[i]<32 || ptr[i]>126)
sprintf(comm_log_buf,"[%x]",ptr[i]);
else
sprintf(comm_log_buf,"%c",ptr[i]);
fprintf(stdout,"%s ",comm_log_buf);
}
return(i);
}
//-----------------------------------------------------------------------------
int main (int argc, char **argv)
{
long n,
c,
len,
done,
bad,
mismatch,
cnt = 0,
spurious;
char buf [500],
test_pattern[256],
line [500];
int err,
i,
nchar;
strcpy (test_pattern, "0123456789");
wcomm_fd = comm_fd = open ("/dev/ttyS0", O_RDWR | O_NONBLOCK);
if (comm_fd == -1)
{
printf("error (%d) opening [%s]\n", errno, strerror(errno));
return(errno);
}
/* send the data */
err = write(wcomm_fd,test_pattern,strlen(test_pattern));
fprintf(stdout, "\nWrite %d bytes \n", err);
if( (err = ioctl( comm_fd, FIONREAD, &nchar )) == -1)
{
fprintf(stderr, "ioctl - error (%d) [%s]\n", errno, strerror
(errno));
return(-1);
}
fprintf(stdout, "Found %d %d bytes in port buffer\n",err, nchar);
dev_str(comm_fd,buf);
}
Version-Release number of selected component (if applicable):
How reproducible:
Steps to Reproduce:
1.compile
2.execute test code
3.
Actual results:
Write 10 bytes
Found 0 0 bytes in port buffer
Found 0 0 bytes in port buffer
Alarm Read Timeout...
0 1 2 3 4 5 6 7 8 9
Expected results:
Write 10 bytes
Found 0 10 bytes in port buffer
Found 0 10 bytes in port buffer
Alarm Read Timeout...
0 1 2 3 4 5 6 7 8 9
Additional info:
GCC version:
gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-
checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20)
More information about the Gcc-bugs
mailing list