linux基礎(chǔ)復(fù)習(xí)(7)串口應(yīng)用開發(fā)
break;
case 460800:
cfsetispeed(newtio, B460800);
cfsetospeed(newtio, B460800);
break;
default:
cfsetispeed(newtio, B9600);
cfsetospeed(newtio, B9600);
break;
}
/*設(shè)置停止位*/
if( nStop == 1 )
newtio.c_cflag = ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
/*設(shè)置等待時(shí)間和最小接收字符*/
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
/*處理未接收字符*/
tcflush(fd,TCIFLUSH);
/*激活新配置*/
if((tcsetattr(fd,TCSANOW,newtio))!=0)
{
perror(com set error);
return -1;
}
printf(set done!n);
return 0;
}
[b]串口使用詳解[/b]
在配置完串口的相關(guān)屬性后,就可對串口進(jìn)行打開,讀寫操作了。其使用方式與文件操作一樣,區(qū)別在于串口是一個(gè)終端設(shè)備。
[b]打開串口[/b]
fd = open( /dev/ttyS0, O_RDWR|O_NOCTTY|O_NDELAY);
Open函數(shù)中除普通參數(shù)外,另有兩個(gè)參數(shù)O_NOCTTY和O_NDELAY。
O_NOCTTY: 通知linix系統(tǒng),這個(gè)程序不會成為這個(gè)端口的控制終端。
O_NDELAY: 通知linux系統(tǒng)不關(guān)心DCD信號線所處的狀態(tài)(端口的另一端是否激活或者停止)。
然后,恢復(fù)串口的狀態(tài)為阻塞狀態(tài),用于等待串口數(shù)據(jù)的讀入。用fcntl函數(shù):
fcntl(fd, F_SETFL, 0);
接著,測試打開的文件描述府是否引用一個(gè)終端設(shè)備,以進(jìn)一步確認(rèn)串口是否正確打開。
isatty(STDIN_FILENO);
串口的讀寫與普通文件一樣,使用read,write函數(shù)。
read(fd,buff,8);
write(fd,buff,8);
實(shí)例
#i nclude stdio.h>
#i nclude string.h>
#i nclude sys/types.h>
#i nclude errno.h>
#i nclude sys/stat.h>
#i nclude fcntl.h>
#i nclude unistd.h>
#i nclude termios.h>
#i nclude stdlib.h>
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,oldtio) != 0) {
perror(SetupSerial 1);
return -1;
}
bzero( newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag = ~CSIZE;
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch( nEvent )
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag = ~PARODD;
break;
case 'N':
newtio.c_cflag = ~PARENB;
break;
}
switch( nSpeed )
{
case 2400:
cfsetispeed(newtio, B2400);
cfsetospeed(newtio, B2400);
break;
case 4800:
cfsetispeed(newtio, B4800);
cfsetospeed(newtio, B4800);
break;
case 9600:
cfsetispeed(newtio, B9600);
cfsetospeed(newtio, B9600);
break;
case 115200:
cfsetispeed(newtio, B115200);
cfsetospeed(newtio, B115200);
break;
default:
cfsetispeed(newtio, B9600);
cfsetospeed(newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag = ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,newtio))!=0)
{
perror(com set error);
return -1;
}
printf(set done!n);
return 0;
}
int open_port(int fd,int comport)
{
char *dev[]={/dev/ttyS0,/dev/ttyS1,/dev/ttyS2};
long vdisable;
if (comport==1)
{ fd = open( /dev/ttyS0, O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror(Can't Open Serial Port);
return(-1);
}
else
printf(open ttyS0 .....n);
}
else if(comport==2)
{ fd = open( /dev/ttyS1, O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror(Can't Open Serial Port);
return(-1);
}
else
printf(open ttyS1 .....n);
}
else if (comport==3)
{
fd = open( /dev/ttyS2, O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror(Can't Open Serial Port);
return(-1);
}
else
printf(open ttyS2 .....n);
}
if(fcntl(fd, F_SETFL, 0)0)
printf(fcntl failed!n);
else
評論