闲着无事,把linux串口编程复习一下,谢了个读串口小程序如下。这将是linux下串口程序的第一个。我还会不定期地对这个程序改进增加新的功能.
NO.1
/***********read serialport on Linux*************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
int open_port(int fd, int comport){
//char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
switch(comport){
case 1: //选用串口1,下面以此类推。
fd= open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);
if(-1==fd)
{perror("Cannot open serialport!\n");
return -1;
}
break;
case 2:
fd=open("/dev/ttyS1",O_RDWR | O_NOCTTY | O_NDELAY);
if(-1==fd)
{
perror("Cannot open serialport!");
return -1;
}
break;
case 3:
fd=open("/dev/ttyS2",O_RDWR | O_NOCTTY | O_NDELAY);
if(-1==fd)
{
perror("Cannot open serialport!");
return -1;
}
break;
}
if(fcntl(fd,F_SETFL,0)<0) //恢复串口阻塞状态。
printf("fcntl failed!\n");
else
printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
if (isatty(STDIN_FILENO )==0) //测试是否为终端设备
printf("standard input is not terminal device!\n");
else
printf("isatty successful!\n");
printf("fd-open=%d\n",fd);
return fd;
}
int set_port(int fd,int nspeed, int nbits,char parity,int nstop)
{ struct termios newtio,oldtio;
/*保存现有的串口参数设置*/
if (tcgetattr(fd,&oldtio)!=0)
{ perror("setup serialport error!");
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(parity){
case 'O': //奇校验
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK|ISTRIP);
break;
case 'E'://偶校验
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
newtio.c_iflag |= (INPCK|ISTRIP);
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;
case 460800:
cfsetispeed(&newtio,B460800);
cfsetospeed(&newtio,B460800);
break;
default:
cfsetispeed(&newtio,B9600);
cfsetospeed(&newtio,B9600);
break;
}
/*设置停止位*/
switch(nstop){
case 1:
newtio.c_cflag &= ~CSTOPB;
break;
case 2:
newtio.c_cflag |= CSTOPB;
break;
}
/*对于接受字符和等待时间,没有要求,都设为0*/
newtio.c_cc[VTIME]=0;
newtio.c_cc[VMIN]=0;
/*刷新串口数据*/
tcflush(fd,TCIOFLUSH);
/*设置新的串口参数*/
if(tcsetattr(fd,TCSANOW,&newtio)!=0)
{
perror("com set error!\n");
return -1;
}
printf("set serialport successfully!\n");
return 0;
}
int main()
{
int fd,i=100;
long nread;
char N,buff[100];
if((fd=open_port(fd,1))<0)
{
perror("cannot open serialport!\n");
return -1;
}
if(set_port(fd,115200,8,'N',1)< 0)
{
perror("set serialport failed!\n");
return -1;
}
do{
nread = read(fd,buff,sizeof(buff));
if(nread>0){
buff[nread]='\0';
printf("read %d bits string: %s\n",nread,buff);
}
}while(--i);
close(fd);
if(close(fd)== -1)
perror("close failed!\n");
return 0;
}
阅读(1021) | 评论(0) | 转发(0) |