arm板子,在程序中怎么read "/dev/xxxxx"串口,cat/dev/xxx可以获得收到数据,想在程序中用
源码:
int main(int argc, char **argv)
{
int fd;
int nread;
//int time =100;
char *dev ="/dev/mygpio";
char revbuf[1024];
char *com_dev ="/dev/s3c2410_serial1";
int com_fd;
int time2 =100;
//int len;
int nwrite;
//char ch='Z';
char *buff="send a message to com!";
com_fd = OpenDev(com_dev);
if(com_fd < 0)
{
perror("myerror");
printf("Can't Open !\n");
exit(1);
}else{//open /dev/s3c2410_serial1 success
printf("open s3c2440_serial1 success!\n");
set_speed(com_fd,115200);
if (set_Parity(com_fd,8,1,'N')== FALSE)
{
printf("Set Parity Error\n");
exit(1);
}
if((nwrite = write(com_fd,buff,strlen(buff)))>0)
{
printf("%s\n",buff);
printf("\nLen %d\n",nwrite);
}else{
printf("write Error\n");
}
//read /dev/s3c2410_serial1
while(1)
{
fd_set rds;
int ret;
struct timeval tv;
FD_ZERO(&rds);
FD_SET(com_fd, &rds);
tv.tv_sec = 0.01;
tv.tv_usec = 500;
ret = select(com_fd+1, &rds, NULL, NULL, &tv);
if (ret < 0) {
perror("select");
exit(1);
}
if (ret == 0) {
printf("Timeout.\n");
}else
{
printf("select successs!!!!!!\n");
}
//sleep(1);
}
}
close(com_fd);
}
现在程序会一直timeout,但是串口工具确实一直在发数据,本来以为sleep的原因,注释掉,还是不行,设置波特率停止位那些函数,就是网上很流行的那套了
[解决办法]
我的资源里面有实例,可以看看
[解决办法]
tv.tv_sec = 0.01;
tv.tv_usec = 500;
是不是你设置的超时时间太小?改大点试试。实际上只有100ms,对于系统忙时,是不会有时间响应的。