/*uart_open_function*/
int open_port(int fd,int comport)
{
char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long vdisable;
if(comport==1)//uart1
{
fd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd){
perror("Can't Open Serial Port1!");
return(-1);
}
}
else if(comport==2)//uart2
{
fd=open("/dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd){
perror("Can't Open Serial Port2!");
return(-1);
}
}
else if(comport==3)//uart3
{
fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd){
perror("Can't Open Serial Port3!");
return(-1);
}
}
/*return uart status is block*/
if(fcntl(fd,F_SETFL,0)<0)
printf("fcntl failed!\n");
else
printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
/*test terminal device*/
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
printf("fd-open=%d\n",fd);
return fd;
}
/*uart_set_function*/
int set_opt(int fd,int nSpeed,int nBits,int nFctl,char nEvent,int nStop)
{
struct termios newtio,oldtio;
/*save local informion of uart,if uart port error,then about it informion be appeard*/
if(tcgetattr(fd,&oldtio)!=0){
perror("SetupSerial 1");
return -1;
}
bzero(&newtio,sizeof(newtio));
/*step 1,set fontsize*/
newtio.c_cflag |=CLOCAL | CREAD;
newtio.c_cflag &=~CSIZE;
/*set bit_stop*/
switch(nBits)
{
case 7:
newtio.c_cflag |=CS7;
break;
case 8:
newtio.c_cflag |=CS8;
break;
}
/*set flow control */
switch(nFctl)
{
case 0:
newtio.c_cflag &= ~CRTSCTS; //no flow control
break;
case 1:
newtio.c_cflag |= CRTSCTS; //hardware flow control
break;
case 2:
newtio.c_cflag |= IXON|IXOFF|IXANY;//software flow control
break;
}