在mini2440上运行发送程序:(以下只是发送循环)
printf("please send datas to the port...\n");
while(1)
{
memset(buf,'\0',20);
if(fgets(buf,20,stdin) == NULL)
{
printf("fgets error!\n");
exit(1);
}
nwrite=strlen(buf);
write(fd,buf,nwrite);
printf("have sent:%s,nwrite=%d\n",buf,nwrite);
}
在主机上运行接受程序:(以下是接受循环)
printf("receiving datas...\n");
FD_ZERO(&rd);
FD_SET(fd,&rd);
while(1)
{
//FD_ZERO(&rd);
//FD_SET(fd,&rd);
switch(select(fd+1,&rd,NULL,NULL,NULL))
{
case -1:exit(1);
case 0:break;//等待超时
default://有可读写文件
if(FD_ISSET(fd,&rd))
{
memset(buf,'\0',20);
nread=read(fd,buf,20);
printf("nread=%d,%s\n",nread,buf);
}
}
}
运行结果却是发送端发送一次,接收端却作两次接收,第一次只接收发送数据的第一个字符,第二次才把剩下的字符都接收过来,
运行结果如下:
发送端:
[root@(none) uart]# ./write_port
open port1 success!
fcntl success!
isatty success!
open fd=3
uart port set done!
fd=3
please send datas to the port...
egrhsgg
have sent:egrhsgg
,nwrite=8
2353265
have sent:2353265
,nwrite=8
89
have sent:89
,nwrite=3
have sent:
,nwrite=1
接收端:
[root@VM-F10 uart]# ./read_port
open port3 success!
fcntl success!
isatty success!
open fd=3
uart port set done!
receiving datas...
nread=1,e
nread=7,grhsgg
nread=1,2
nread=7,353265
nread=1,8
nread=2,9
nread=1,
很奇怪阿,如果用minicom接收显示,结果一样,那难道是发送端程序有问题?找不出阿。。。高手帮帮忙阿,多谢!
我用的是虚拟机,主机端采用USB转串的