在程序中,很容易配置串口的属性,这些属性定义在结构体struct termios中。
关于termios的详细介绍,可以另行查资料,或者参考:详解linux下的串口通讯开发:http://blog.itpub.net/24790158/viewspace-1041147/
#include
#include
#include
#include
#include
#include
#include
#include
#include
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; //CLOCAL:忽略modem控制线 CREAD:打开接受者
newtio.c_cflag &= ~CSIZE; //字符长度掩码。取值为:CS5,CS6,CS7或CS8
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); // INPACK:启用输入奇偶检测;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;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB; //CSTOPB:设置两个停止位,而不是一个
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0; //VTIME:非cannoical模式读时的延时,以十分之一秒位单位
newtio.c_cc[VMIN] = 0; //VMIN:非canonical模式读到最小字符数
tcflush(fd,TCIFLUSH); // 改变在所有写入 fd 引用的对象的输出都被传输后生效,所有已接受但未读入的输入都在改变发生前丢弃。
if((tcsetattr(fd,TCSANOW,&newtio))!=0) //TCSANOW:改变立即发生
{
perror("com set error");
return -1;
}
printf("set done!
");
return 0;
}
int main(void)
{
int fd1,nset,nread,ret;
char buf[100]={"test com data!...........
"};
char buf1[100];
fd1 = open( "/dev/ttySAC0", O_RDWR);
if (fd1 == -1)
exit(1);
printf("open SAC0 success!!
");
nset = set_opt(fd1, 9600, 8, 'N', 1);
if (nset == -1)
exit(1);
printf("SET SAC0 success!!
");
printf("enter the loop!!
");
while (1)
{
memset(buf1, 0, sizeof(buf1));
ret = write(fd1, buf, 100);
if( ret > 0){
printf("write success! wait data receive
");
}
nread = read(fd1, buf1, 100);
if(nread > 0){
printf("redatad: nread = %s
", buf1);
}
sleep(1);
//nread = read(fd1, buf1,1);
//if(buf1[0] == 'q')
//break;
}
close(fd1);
return 0;
}
用arm-linux-gcc交叉编译后在开发板上运行,将9口串口的2 3短接:
/ # ./com
open SAC0 success!!
set done!
SET SAC0 success!!
enter the loop!!
write success! wait data receive
write success! wait data receive
write success! wait data receive
write success! wait data receive
write success! wait data receive
write success! wait data receive
redatad: nread = test com data!...........
write success! wait data receive
redatad: nread = test com data!...........
write success! wait data receive
redatad: nread = test com data!...........