博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux 串口编程
阅读量:4284 次
发布时间:2019-05-27

本文共 6060 字,大约阅读时间需要 20 分钟。

参考资料

  • 关于串口,参考书就这三本了


##简述

include/uapi/asm-generic/termbits.htypedef unsigned char   cc_t;typedef unsigned int    speed_t;typedef unsigned int    tcflag_t;#define NCCS 19struct termios {
tcflag_t c_iflag; /* input mode flags */ tcflag_t c_oflag; /* output mode flags */ tcflag_t c_cflag; /* control mode flags */ tcflag_t c_lflag; /* local mode flags */ cc_t c_line; /* line discipline */ cc_t c_cc[NCCS]; /* control characters */};include/uapi/asm-generic/termbits.h

什么串口阻塞不阻塞的,追一下源码就知道了,就知道read write 写到串口文件的实际过程了


open

include/uapi/asm-generic/fcntl.h  open("/dev/ttyf1", O_RDWR | O_NOCTTY | O_NDELAY);The O_NOCTTY flag tells UNIX that this program doesn't want to be the "controlling terminal" for that port. If you don't specify this then any input (such as keyboard abort signals and so forth) will affect your process. Programs like getty(1M/8) use this feature when starting the login process, but normally a user program does not want this behavior.//这样子一些控制字符不会影响你的串口  The O_NDELAY flag tells UNIX that this program doesn't care what state the DCD signal line is in - whether the other end of the port is up and running. If you do not specify this flag, your process will be put to sleep until the DCD signal line is the space voltage.//这样不会阻塞,就是以非阻塞方式打开.读到读不到都返回.//但是这个宏在最新的内核中已经没有了.所以下面的//open("/dev/ttyf1", O_RDWR | O_NDELAY); //设置非阻塞//fcntl(fd, F_SETFL, FNDELAY);//设置非阻塞已经过时了,不再用了-----------------------------------------------------------目前用的是 O_NONBLOCK用 open 设置阻塞和非阻塞//open("/dev/ttyf1", O_RDWR); //串口打开默认阻塞//open("/dev/ttyf1", O_RDWR | O_NONBLOCK); //设置非阻塞当然也可以通过 fcntl ,来设置阻塞和非阻塞1/设置阻塞//fcntl(fd, F_SETFL, 0);//设置阻塞2/设置非阻塞//fcntl(fd, F_SETFL, O_NONBLOCK);//设置非阻塞

设置

1.波特率struct termios options;tcgetattr(fd, &options);cfsetispeed(&options, B19200);cfsetospeed(&options, B19200);options.c_cflag |= (CLOCAL | CREAD);tcsetattr(fd, TCSANOW, &options);//立刻生效,有些选项会等到发送完,或者接收完2.字符sizeoptions.c_cflag &= ~CSIZE; /* Mask the character size bits */options.c_cflag |= CS8;    /* Select 8 data bits */3.校验//判断一个字符中1的个数是奇数还是偶数No parity (8N1):options.c_cflag &= ~PARENBoptions.c_cflag &= ~CSTOPBoptions.c_cflag &= ~CSIZE;options.c_cflag |= CS8;Even parity (7E1): //奇options.c_cflag |= PARENBoptions.c_cflag &= ~PARODDoptions.c_cflag &= ~CSTOPBoptions.c_cflag &= ~CSIZE;options.c_cflag |= CS7;Odd parity (7O1): //偶options.c_cflag |= PARENBoptions.c_cflag |= PARODDoptions.c_cflag &= ~CSTOPBoptions.c_cflag &= ~CSIZE;options.c_cflag |= CS7;Space parity is setup the same as no parity (7S1):options.c_cflag &= ~PARENBoptions.c_cflag &= ~CSTOPBoptions.c_cflag &= ~CSIZE;options.c_cflag |= CS8;4.硬件流控制options.c_cflag |= CNEW_RTSCTS;    /* Enable*/options.c_cflag &= ~CNEW_RTSCTS;  /* disable */5.c_lflag  Choosing Canonical Input  options.c_lflag |= (ICANON | ECHO | ECHOE);  Choosing Raw Input  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // The local modes member c_lflag controls how input characters are managed by the serial driver. In general you will configure the c_lflag member for canonical or raw input.6.c_iflag  //The input modes member c_iflag controls any input processing that is done to characters received on the port. Like the c_cflag field, the final value stored in c_iflag is the bitwise OR of the desired options.  options.c_iflag |= (INPCK | ISTRIP);//流控制options.c_iflag |= (IXON | IXOFF | IXANY);//流控制disableoptions.c_iflag &= ~(IXON | IXOFF | IXANY);7.c_oflag//The c_oflag member contains output filtering options. Like the input modes, you can select processed or raw data output.options.c_oflag |= OPOST;//Choosing Processed Output//Of all the different options, you will only probably use the ONLCR option which maps newlines into CR-LF pairs.options.c_oflag &= ~OPOST;//Choosing Raw Output8.超时设置UNIX serial interface drivers provide the ability to specify character and packet timeouts. Two elements of the c_cc array are used for timeouts: VMIN and VTIME. Timeouts are ignored in canonical input mode or when the NDELAY option is set on the file via open or fcntl.VMIN specifies the minimum number of characters to read. If it is set to 0, then the VTIME value specifies the time to wait for every character read. Note that this does not mean that a read call for N bytes will wait for N characters to come in. Rather, the timeout will apply to the first character and the read call will return the number of characters immediately available (up to the number you request).If VMIN is non-zero, VTIME specifies the time to wait for the first character read. If a character is read within the time given, any read will block (wait) until all VMIN characters are read. That is, once the first character is read, the serial interface driver expects to receive an entire packet of characters (VMIN bytes total). If no character is read within the time allowed, then the call to read returns 0. This method allows you to tell the serial driver you need exactly N bytes and any read call will return 0 or N bytes. However, the timeout only applies to the first character read, so if for some reason the driver misses one character inside the N byte packet then the read call could block forever waiting for additional input characters.VTIME specifies the amount of time to wait for incoming characters in tenths of seconds. If VTIME is set to 0 (the default), reads will block (wait) indefinitely unless the NDELAY option is set on the port with open or fcntl.

fcntl

fcntlfcntl(fd, F_SETFL, 0);//设置阻塞

write

n = write(fd, "ATZ\r", 4);The write function returns the number of bytes sent or -1 if an error occurred. Usually the only error you'll run into is EIO when a MODEM or data link drops the Data Carrier Detect (DCD) line. This condition will persist until you close the port.

read

//有的话会读出来,读没有会阻塞,直到字符进来,或者时间到了,或者错误发生.fcntl(fd, F_SETFL, FNDELAY);//上面这一句会让read在没有字符的话返回0fcntl(fd, F_SETFL, 0);//上面这一句会让read正常阻塞.

close

close(fd);Closing a serial port will also usually set the DTR signal low which causes most MODEMs to hang up.

参考文档

  • 关于资料,我感觉最好的有三本书.

  • 剩下的都是对三本书的解读

转载地址:http://utigi.baihongyu.com/

你可能感兴趣的文章
ExtJs4.2学习--Ext.tab.Panel 选项卡
查看>>
mybatis+spring+struts2框架整合
查看>>
你真正理解java的字符类型了吗?(bit,byte,short,int等字符类型)
查看>>
数据挖掘开源软件:WEKA基础操作
查看>>
Python一些容易忽略的知识
查看>>
Ubuntu(-13.10)中安装JDK图文详解教程
查看>>
Hadoop2.2.0单节点安装和配置环境图文详解
查看>>
数据挖掘学习笔记--决策树C4.5
查看>>
数据挖掘学习笔记之人工神经网络(一)
查看>>
数据挖掘学习笔记之人工神经网络(二)
查看>>
人工神经网络关键核心知识点
查看>>
贝叶斯学习--极大后验概率假设和极大似然假设
查看>>
贝叶斯学习--极大后验假设学习
查看>>
朴素贝叶斯分类器
查看>>
贝叶斯学习举例--学习分类文本
查看>>
hadoop HDFS原理基础知识
查看>>
数据挖掘十大算法----EM算法(最大期望算法)
查看>>
android StrictMode应用
查看>>
TabHost的两种使用方法
查看>>
Android---TextView属性详解
查看>>