1 // 取得串口参数 2 int get_com_info(char* ttysname, unsigned int *bps_index, unsigned int *databits_index, unsigned int *paritybits_index, unsigned int *stopbits_index) 3 { 4 unsigned int bps0[] = { 5 B110, B300, B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600 6 }; 7 8 int fd = 0; 9 fd = open(ttysname, O_RDWR); 10 if (-1 == fd) { 11 perror("Open tty error."); 12 return -1; 13 } 14 15 int state = 0; 16 struct termios opt; 17 state = tcgetattr(fd, &opt); 18 if (state < 0) { 19 perror("Get tty attr error."); 20 close(fd); 21 return -1; 22 } 23 24 speed_t spd = cfgetispeed(&opt); 25 26 // 读波特率 27 int tagbps = -1; 28 int index = 0; 29 for (index = 0; index < 13; index++ ) { 30 if (spd == bps0[index]) { 31 *bps_index = index; 32 tagbps = 0; 33 break; 34 } 35 } 36 // 读数据位 37 int tagdb = 0; 38 if ((opt.c_cflag & CS5) == CS5) { 39 *databits_index = 0; // 5 40 } 41 else if ((opt.c_cflag & CS6) == CS6) { 42 *databits_index = 1;// 6 43 } 44 else if ((opt.c_cflag & CS7) == CS7) { 45 *databits_index = 2;// 7 46 } 47 else if ((opt.c_cflag & CS8) == CS8) { 48 *databits_index = 3;// 8 49 } 50 else { 51 tagdb = -1; 52 } 53 // 读校验位 54 int tagpb = 0; 55 if (((opt.c_cflag & PARENB) != PARENB) && ((opt.c_iflag & INPCK) != INPCK)) { 56 *paritybits_index = 2; // 无校验 57 } 58 else if (((opt.c_cflag & (PARODD | PARENB)) == (PARODD | PARENB)) && ((opt.c_iflag & INPCK) == INPCK)) { 59 *paritybits_index = 0; // 偶校验 60 } 61 else if (((opt.c_cflag & (PARENB | PARODD)) == PARENB) && ((opt.c_iflag & INPCK) == INPCK)) { 62 *paritybits_index = 1; // 奇校验 63 } 64 else { 65 tagpb = -1; 66 } 67 // 结束位 68 if ((opt.c_cflag & CSTOPB) == CSTOPB) { 69 *stopbits_index = 1; // 2bit 70 } else { 71 *stopbits_index = 0; // 1bit 72 } 73 74 close(fd); 75 76 return (tagbps || tagdb || tagpb) ? -1 : 0; 77 }
1 // 设置串口参数 2 int set_com_info(char* ttysname, unsigned int bps_index, unsigned int databits_index, unsigned int paritybits_index, unsigned int stopbits_index) 3 { 4 unsigned int bps0[] = { 5 B110, B300, B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600 6 }; 7 8 int fd = 0; 9 fd = open(ttysname, O_RDWR); 10 if (-1 == fd) { 11 perror("Open tty error."); 12 return -1; 13 } 14 15 struct termios oldtio; 16 if (tcgetattr(fd, &oldtio) != 0) { 17 perror("SetupSerial 1"); 18 close(fd); 19 return -1; 20 } 21 22 struct termios newtio; 23 bzero(&newtio, sizeof(newtio)); 24 newtio.c_cflag |= CLOCAL | CREAD; 25 26 int tagdb = 0; 27 newtio.c_cflag &= ~CSIZE; 28 if (databits_index == 0) { 29 newtio.c_cflag |= CS5; 30 } 31 else if (databits_index == 1) { 32 newtio.c_cflag |= CS6; 33 } 34 else if (databits_index == 2) { 35 newtio.c_cflag |= CS7; 36 } 37 else if (databits_index == 3) { 38 newtio.c_cflag |= CS8; 39 } 40 else { 41 tagdb = -1; 42 } 43 44 int tagpb = 0; 45 if (paritybits_index == 2) { 46 newtio.c_cflag &= ~PARENB; 47 } 48 else if (paritybits_index == 1) { 49 newtio.c_cflag |= PARENB; 50 newtio.c_cflag |= PARODD; 51 newtio.c_iflag |= (INPCK | ISTRIP); 52 } 53 else if (paritybits_index == 0) { 54 newtio.c_iflag |= (INPCK | ISTRIP); 55 newtio.c_cflag |= PARENB; 56 newtio.c_cflag &= ~PARODD; 57 } 58 else { 59 tagpb = -1; 60 } 61 62 int tagbps = -1; 63 if ((bps_index >= 0) && (bps_index < 13)) { 64 cfsetispeed(&newtio, bps0[bps_index]); 65 cfsetospeed(&newtio, bps0[bps_index]); 66 tagbps = 0; 67 } 68 69 if (stopbits_index == 0) { 70 newtio.c_cflag &= ~CSTOPB; 71 } 72 else if (stopbits_index == 1) { 73 newtio.c_cflag |= CSTOPB; 74 } 75 76 newtio.c_cc[VTIME] = 0; 77 newtio.c_cc[VMIN] = 0; 78 79 if (tagbps || tagdb || tagpb) { 80 perror("com set error"); 81 close(fd); 82 return -1; 83 } 84 85 tcflush(fd, TCIFLUSH); 86 if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) { 87 perror("com set error"); 88 close(fd); 89 return -1; 90 } 91 92 close(fd); 93 94 printf("set done!\n"); 95 96 return 0; 97 }
以下为转载:
http://www.cnblogs.com/wblyuyang/archive/2011/11/21/2257544.html
struct termios
{
tcflag_t c_iflag; //input flags
tcflag_t c_oflag; //output flags
tcflag_t c_cflag; //control flags
tcflag_t c_lflag; //local flags
cc_t c_cc[NCCS]; //control characters
};
该结构体中c_cflag最为重要,可设置波特率、数据位、校验位、停止位。
串口控制函数
Tcgetattr 取属性(termios结构)
Tcsetattr 设置属性(termios结构)
cfgetispeed 得到输入速度
Cfgetospeed 得到输出速度
Cfsetispeed 设置输入速度
Cfsetospeed 设置输出速度
Tcdrain 等待所有输出都被传输
tcflow 挂起传输或接收
tcflush 刷清未决输入和/或输出
Tcsendbreak 送BREAK字符
tcgetpgrp 得到前台进程组ID
tcsetpgrp 设置前台进程组ID
串口配置流程
1>保存原先串口配置,用tcgetattr(fd,&oldtio)函数
struct termios newtio,oldtio;
tcgetattr(fd,&oldtio);
2>激活选项有CLOCAL和CREAD,用于本地连接和接收使用
newtio.c_cflag | = CLOCAL | CREAD;
3>设置波特率,使用函数cfsetispeed、cfsetospeed
cfsetispeed(&newtio,B115200);
cfsetospeed(&newtio,B115200);
4>设置数据位,需使用掩码设置
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;
5>设置奇偶校验位,使用c_cflag和c_iflag.
设置奇校验:
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
设置偶校验:
newtio.c_iflag |= (INPCK|ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag |= ~PARODD;
6>设置停止位,通过激活c_cflag中的CSTOPB实现。若停止位为1,则清除CSTOPB,若停止位为2,则激活CSTOPB。
newtio.c_cflag &= ~CSTOPB;
7>设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时,可设为0:
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
8>处理要写入的引用对象
tcflush函数刷清(抛弃)输入缓存(终端驱动程序已接收到,但用户程序尚未读)或输出缓存(用户程序已经写,但尚未发送).
int tcflush(int filedes,int quene)
quene数应当是下列三个常数之一:
*TCIFLUSH 刷清输入队列
*TCOFLUSH 刷清输出队列
*TCIOFLUSH 刷清输入、输出队列
例如:tcflush(fd,TCIFLUSH);
9>激活配置。在完成配置后,需要激活配置使其生效。使用tcsetattr()函数:
int tcsetattr(int filedes,int opt,const struct termios *termptr);
opt使我们可以指定在什么时候新的终端属性才起作用,
*TCSANOW:更改立即发生
*TCSADRAIN:发送了所有输出后更改才发生。若更改输出参数则应使用此选项
*TCSAFLUSH:发送了所有输出后更改才发生。更进一步,在更改发生时未读的
所有输入数据都被删除(刷清).
例如: tcsetattr(fd,TCSANOW,&newtio);
串口使用详解
打开串口
fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);
参数--O_NOCTTY:通知linux系统,这个程序不会成为这个端口的控制终端.
O_NDELAY:通知linux系统不关心DCD信号线所处的状态(端口的另一端是否激活或者停止).
然后恢复串口的状态为阻塞状态,用于等待串口数据的读入,用fcntl函数:
fcntl(fd,F_SETFL,0); //F_SETFL:设置文件flag为0,即默认,即阻塞状态
接着测试打开的文件描述符是否应用一个终端设备,以进一步确认串口是否正确打开.
isatty(STDIN_FILENO);
串口的读写与普通文件一样,使用read,write函数
read(fd,buff,8);
write(fd,buff,8);