|
我写的读写串口的程序,分开就没有问题,但是合在一起就不行,到read那里就停了.我看原来的贴子有大侠和我碰到相同的问题,但是他出问题的几个串口设置的参数,我都设置的和他改好后是一样的.实在是找不到问题所在,我把程序贴上来,大家帮我看看,我究竟哪里写的有问题.我是用一台机器,用串口线将ttyS1和ttyS0连起来,ttyS1发送数据,ttyS0接收数据,这个程序总是运行输出到3就停了:
int SerialInit(char *dev)
{
int fd;
struct termios Opt;
fd=open(dev,O_RDWR | O_NOCTTY);
if(fd<=0)
return -1;
else
fcntl(fd,F_SETFL,0);
cfsetispeed(&Opt, B9600);
cfsetospeed(&Opt, B9600);
Opt.c_cflag |= (CLOCAL | CREAD);
Opt.c_lflag &=~(ICANON | ECHO | ECHOE | ISIG);
Opt.c_oflag &=~OPOST;
Opt.c_iflag &=~(IXON | ICRNL);
Opt.c_cflag &= ~CSIZE;
Opt.c_cflag |= CS8;
Opt.c_cflag &= ~PARENB; /* Clear parity enable */
Opt.c_iflag &= ~INPCK; /* Enable parity checking */
Opt.c_cflag &= ~CSTOPB;
tcflush(fd,TCIOFLUSH);
return(fd);
}
main()
{
int fd1;
int fd0;
int nread;
char buffr[512];
char *test="hello";
fd1=SerialInit("/dev/ttyS1");
if(fd1)
{
write(fd1,test,strlen(test));
printf("1\n");
}
else
{
printf("Can't Open Serial Port ttyS1!\n");
exit(0);
}
close(fd1);
fd0=SerialInit("/dev/ttyS0");
printf("2\n");
if(fd0)
{
while(1)
{
printf("3\n");
while((nread=read(fd0,buffr,512))>0)
{
printf("4");
printf("\n%s",buffr);
}
}
}
else
{
printf("Can't Open Serial Port ttyS0!\n");
exit(0);
}
close(fd0);
} |
|