我希望使用串行COM端口进行通信,并且希望在每次调用read()函数调用时实现超时.
int filedesc = open( "dev/ttyS0", O_RDWR );
read( filedesc, buff, len );
编辑:
我正在使用Linux操作系统.如何使用select函数调用实现?
我希望使用串行COM端口进行通信,并且希望在每次调用read()函数调用时实现超时.
int filedesc = open( "dev/ttyS0", O_RDWR );
read( filedesc, buff, len );
编辑:
我正在使用Linux操作系统.如何使用select函数调用实现?
Select ()
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
int main(void)
{
fd_set set;
struct timeval timeout;
int rv;
char buff[100];
int len = 100;
int filedesc = open( "dev/ttyS0", O_RDWR );
FD_ZERO(&set); /* clear the set */
FD_SET(filedesc, &set); /* add our file descriptor to the set */
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
rv = select(filedesc + 1, &set, NULL, NULL, &timeout);
if(rv == -1)
perror("select"); /* an error accured */
else if(rv == 0)
printf("timeout"); /* a timeout occured */
else
read( filedesc, buff, len ); /* there was data to read */
close(filedesc);
}