int uart_port_open(int com_port)//打开相应的uart端口(0~4中的一个),返回值为fd
{
int fd;
char device[20];
#if 0
//char *dev[]={"/dev/ttyO1","/dev/ttyO2","/dev/ttyO3","/dev/ttyO4","/dev/ttyO5"};
if ((com_port < 0)||(com_port > MAX_COM_NUM))
{
return -1;
}
fd = open(dev[com_port],O_RDWR|O_NOCTTY);//|O_NDELAY);
#endif
sprintf(device,"/dev/ttyO%d",com_port);
if ((com_port < 0)||(com_port > MAX_COM_NUM))
{
return -1;
}
fd = open(device,O_RDWR|O_NOCTTY);
if(fd<0)
{
printf("open serial port failed\n");
return -1;
}
/*restore the serial port blocking state,wait for the readed data of the uart*/
if (fcntl(fd,F_SETFL,0)<0) //set the flag of file status,and set access module//设置为阻塞模式
{
printf("open fcntl F_SETFL failed\n" );
}
/*test whether is the terminal device*/
if (isatty(STDIN_FILENO) == 0)
{
printf("standaed input is not a terminal device");
}
return fd;
}
可以不进行判断吗?为什么要进行判断?
{
int fd;
char device[20];
#if 0
//char *dev[]={"/dev/ttyO1","/dev/ttyO2","/dev/ttyO3","/dev/ttyO4","/dev/ttyO5"};
if ((com_port < 0)||(com_port > MAX_COM_NUM))
{
return -1;
}
fd = open(dev[com_port],O_RDWR|O_NOCTTY);//|O_NDELAY);
#endif
sprintf(device,"/dev/ttyO%d",com_port);
if ((com_port < 0)||(com_port > MAX_COM_NUM))
{
return -1;
}
fd = open(device,O_RDWR|O_NOCTTY);
if(fd<0)
{
printf("open serial port failed\n");
return -1;
}
/*restore the serial port blocking state,wait for the readed data of the uart*/
if (fcntl(fd,F_SETFL,0)<0) //set the flag of file status,and set access module//设置为阻塞模式
{
printf("open fcntl F_SETFL failed\n" );
}
/*test whether is the terminal device*/
if (isatty(STDIN_FILENO) == 0)
{
printf("standaed input is not a terminal device");
}
return fd;
}
可以不进行判断吗?为什么要进行判断?