Linux下串口通信(2)

2019-04-15 22:28

通俗地说就是将输出缓冲器清空,把输入缓冲区清空。缓冲区里的数据都废弃。

4)波特率配置

串口通过函数cfsetispeed和cfsetospeed设置端口的输入/输出波特率:

int cfsetispeed(structtermios *termios_p, speed_t speed); int cfsetospeed(structtermios *termios_p, speed_t speed);

其中termios_p为串口结构体termios指针变量;speed 为需要设置的串口传输速率,取值与波特率对应关系见表一。

7.例子程序:

程序实现了配置串口0参数与向串口0输出数据 #include #include #include #include #include #include #include #include #include

voidSet_uart(intfd, intdatabits, intstopbit, char parity, char datastream) { int ret;

structtermiostermios_opt; if(tcgetattr(fd, &termios_opt)) {

perror(\return; }

/*flush memory*/ tcflush(fd,TCIOFLUSH); /*Set i/o speed*/

cfsetispeed(&termios_opt, B115200); cfsetospeed(&termios_opt, B115200); /*set databits,default is 8 databits*/ termios_opt.c_cflag&= ~CSIZE; switch (databits) { case 5:

termios_opt.c_cflag |= CS5; break; case 6:

termios_opt.c_cflag |= CS6; break; case 7:

termios_opt.c_cflag |= CS7; break; case 8:

termios_opt.c_cflag |= CS8; break; default:

termios_opt.c_cflag |= CS8; break; }

/*Set Parity, default is no vertify*/ switch (parity) {

case 'N': /*no vertify*/

termios_opt.c_cflag&= ~PARENB; termios_opt.c_iflag&= ~INPCK; break;

case 'O': /*odd vertify*/

termios_opt.c_cflag |= (PARODD | PARENB); termios_opt.c_iflag |= INPCK;

break;

case 'E': /*even vertify*/ termios_opt.c_cflag |= PARENB; termios_opt.c_cflag&= ~PARODD; termios_opt.c_iflag |= INPCK; break;

case 'S': /*space vertify*/ termios_opt.c_cflag&= ~PARENB; termios_opt.c_cflag&= ~CSTOPB; termios_opt.c_iflag |= INPCK; break; default:

termios_opt.c_cflag&= ~PARENB; termios_opt.c_iflag&= ~INPCK; break; }

/*Set stop bits, default is 1 stopbit*/ switch (stopbit) { case 1:

termios_opt.c_cflag&= ~CSTOPB; break;

case 2:

termios_opt.c_cflag |= CSTOPB; break; default:

termios_opt.c_cflag&= ~CSTOPB; break; }

/*set data stream,default is no data stream control*/ switch (datastream) {

case 'N': /*no data stream control*/ termios_opt.c_cflag&= ~CRTSCTS; break;

case 'H': /*hardware data stream control*/ termios_opt.c_cflag |= CRTSCTS; break;

case 'S': /*software data stream control*/ termios_opt.c_cflag |= IXON | IXOFF | IXANY; break; default:

termios_opt.c_cflag&= ~CRTSCTS; break;


Linux下串口通信(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:园林施工组织方案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: