当前位置: 技术问答>linux和unix
linux 下串口 问题, read 不是返回-1 就是不返回
来源: 互联网 发布时间:2016-03-07
本文导语: 设置的部分如下, 尝试了很多设置组合了, 就是不能用, read不返回, 或者就返回-1 下面的是返回-1的, 请教高人给看看 fd = open(comname,O_RDWR|O_NOCTTY|O_NDELAY); if(fd == -1) { printf("open_port: Unable to open %s",co...
设置的部分如下, 尝试了很多设置组合了, 就是不能用, read不返回, 或者就返回-1
下面的是返回-1的, 请教高人给看看
fd = open(comname,O_RDWR|O_NOCTTY|O_NDELAY);
if(fd == -1)
{
printf("open_port: Unable to open %s",comname );
return 1;
}
else
{
//fcntl(fd,F_SETFL,FNDELAY);
//fcntl(fd,F_SETOWN, getpid());
//fcntl(fd, F_SETFL, FASYNC);
}
struct termios options;
//get current options for com port
tcgetattr(fd,&options);
// User Definable
switch (Rs232.iBaud) {
case 300:
cfsetispeed(&options,B300);
cfsetospeed(&options,B300);
break;
case 1200:
cfsetispeed(&options,B1200);
cfsetospeed(&options,B1200);
break;
case 2400:
cfsetispeed(&options,B2400);
cfsetospeed(&options,B2400);
break;
case 4800:
cfsetispeed(&options,B4800);
cfsetospeed(&options,B4800);
break;
case 9600:
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
break;
case 19200:
cfsetispeed(&options,B19200);
cfsetospeed(&options,B19200);
break;
case 38400:
cfsetispeed(&options,B38400);
cfsetospeed(&options,B38400);
break;
case 57600:
cfsetispeed(&options,B57600);
cfsetospeed(&options,B57600);
break;
case 115200:
cfsetispeed(&options,B115200);
cfsetospeed(&options,B115200);
break;
default:
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
break;
} // switch
//Enable the receiver and set local mode
//options.c_cflag &= (CLOCAL | CREAD);
//Support 8N1 only in linux version
/*
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag &= CS8;
*/
options.c_iflag = 0;
options.c_lflag = 0;
options.c_oflag = 0;
//options.c_cc[VTIME] = 0;
//options.c_cc[VMIN] = 255;
options.c_cc[VINTR] = 0;/**//*Ctrl-c*/
options.c_cc[VQUIT] =0;/**//*Ctrl-*/
options.c_cc[VERASE] = 0;/**//*del*/
options.c_cc[VKILL]=0;/**//*@*/
options.c_cc[VEOF]=0;/**//*Ctrl-d*/
options.c_cc[VTIME]=1;/**//**/
options.c_cc[VMIN]=0;/**//**/
options.c_cc[VSWTC]=0;/**//*''*/
options.c_cc[VSTART]=0;/**//*Ctrl-q*/
options.c_cc[VSTOP]=0;/**//*Ctrl-s*/
options.c_cc[VSUSP]=0;/**//*Ctrl-z*/
options.c_cc[VEOL]=0;/**//*''*/
options.c_cc[VREPRINT]=0;/**//*Ctrl-r*/
options.c_cc[VDISCARD]=0;/**//*Ctrl-u*/
options.c_cc[VWERASE]=0;/**//*Ctrl-w*/
options.c_cc[VLNEXT]=0;/**//*Ctrl-v*/
options.c_cc[VEOL2]=0;/**//*''*/
//tcflush(fd, TCIFLUSH);
//set the new options for the port
if(tcsetattr(fd,TCSANOW,&options)
下面的是返回-1的, 请教高人给看看
fd = open(comname,O_RDWR|O_NOCTTY|O_NDELAY);
if(fd == -1)
{
printf("open_port: Unable to open %s",comname );
return 1;
}
else
{
//fcntl(fd,F_SETFL,FNDELAY);
//fcntl(fd,F_SETOWN, getpid());
//fcntl(fd, F_SETFL, FASYNC);
}
struct termios options;
//get current options for com port
tcgetattr(fd,&options);
// User Definable
switch (Rs232.iBaud) {
case 300:
cfsetispeed(&options,B300);
cfsetospeed(&options,B300);
break;
case 1200:
cfsetispeed(&options,B1200);
cfsetospeed(&options,B1200);
break;
case 2400:
cfsetispeed(&options,B2400);
cfsetospeed(&options,B2400);
break;
case 4800:
cfsetispeed(&options,B4800);
cfsetospeed(&options,B4800);
break;
case 9600:
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
break;
case 19200:
cfsetispeed(&options,B19200);
cfsetospeed(&options,B19200);
break;
case 38400:
cfsetispeed(&options,B38400);
cfsetospeed(&options,B38400);
break;
case 57600:
cfsetispeed(&options,B57600);
cfsetospeed(&options,B57600);
break;
case 115200:
cfsetispeed(&options,B115200);
cfsetospeed(&options,B115200);
break;
default:
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
break;
} // switch
//Enable the receiver and set local mode
//options.c_cflag &= (CLOCAL | CREAD);
//Support 8N1 only in linux version
/*
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag &= CS8;
*/
options.c_iflag = 0;
options.c_lflag = 0;
options.c_oflag = 0;
//options.c_cc[VTIME] = 0;
//options.c_cc[VMIN] = 255;
options.c_cc[VINTR] = 0;/**//*Ctrl-c*/
options.c_cc[VQUIT] =0;/**//*Ctrl-*/
options.c_cc[VERASE] = 0;/**//*del*/
options.c_cc[VKILL]=0;/**//*@*/
options.c_cc[VEOF]=0;/**//*Ctrl-d*/
options.c_cc[VTIME]=1;/**//**/
options.c_cc[VMIN]=0;/**//**/
options.c_cc[VSWTC]=0;/**//*''*/
options.c_cc[VSTART]=0;/**//*Ctrl-q*/
options.c_cc[VSTOP]=0;/**//*Ctrl-s*/
options.c_cc[VSUSP]=0;/**//*Ctrl-z*/
options.c_cc[VEOL]=0;/**//*''*/
options.c_cc[VREPRINT]=0;/**//*Ctrl-r*/
options.c_cc[VDISCARD]=0;/**//*Ctrl-u*/
options.c_cc[VWERASE]=0;/**//*Ctrl-w*/
options.c_cc[VLNEXT]=0;/**//*Ctrl-v*/
options.c_cc[VEOL2]=0;/**//*''*/
//tcflush(fd, TCIFLUSH);
//set the new options for the port
if(tcsetattr(fd,TCSANOW,&options)