|
UART Polling/Interrupt
I have a question regarding reading the serial part. Currently, I have opened a serial port and configured it, and then mapped a stream to it using fdopen(). I then use the returned FILE * stream to do all my io (using fread, fwrite). Since my programming basically sits for commands on the serial port, using while fread() is very CPU intensive, taking up nearly 100%. Is there any way to use streams and have it block on reading?
This is my code to open the port.. which calls the code to configure the serial port.
FILE * open_port(void)
{
int fd;
fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);
if (fd==-1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
printf("Serial Port 1 Open\n");
fcntl(fd,F_SETFL,0);
}
//configure port
configure_port(fd);
return fdopen(fd,"r+");
}
void configure_port(int fd)
{
struct termios newtio;
printf("Configuring Port\n");
newtio.c_cflag = CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
cfsetospeed(&newtio,B19200);
cfsetispeed(&newtio,B19200);
tcsetattr(fd,TCSANOW,&newtio);
fcntl(fd,F_SETFL,FNDELAY);
}
Thx.
|