linux 串口编程.docx

上传人:小** 文档编号:88201391 上传时间:2019-04-20 格式:DOCX 页数:18 大小:18.73KB
返回 下载 相关 举报
linux 串口编程.docx_第1页
第1页 / 共18页
linux 串口编程.docx_第2页
第2页 / 共18页
linux 串口编程.docx_第3页
第3页 / 共18页
linux 串口编程.docx_第4页
第4页 / 共18页
linux 串口编程.docx_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《linux 串口编程.docx》由会员分享,可在线阅读,更多相关《linux 串口编程.docx(18页珍藏版)》请在金锄头文库上搜索。

1、这几天,由于长春门检系统项目的需要,涉及到了读卡器信息的串口读取,所以在Linux下串口信息的读取有了一点心得体会。1. 打开串口 与其他的关于设备编程的方法一样,在Linux下,操作、控制串口也是通过操作起设备文件进行的。在Linux下,串口的设备文件是/dev/ttyS0或/dev/ttyS1等。因此要读写串口,我们首先要打开串口: char *dev = /dev/ttyS0; /串口1 int fd = open( dev, O_RDWR ); /| O_NOCTTY | O_NDELAY if (-1 = fd) perror(Cant Open Serial Port); retu

2、rn -1; else return fd; 2. 设置串口速度 打开串口成功后,我们就可以对其进行读写了。首先要设置串口的波特率: int speed_arr = B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, ;int name_arr = 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, ;void set_speed

3、(int fd, int speed) int i; int status; struct termios Opt; tcgetattr(fd, &Opt); for ( i= 0; i sizeof(speed_arr) / sizeof(int); i+) if (speed = name_arri) tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, speed_arri); cfsetospeed(&Opt, speed_arri); status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) perr

4、or(tcsetattr fd); return; tcflush(fd,TCIOFLUSH); 3. 设置串口信息这主要包括:数据位、停止位、奇偶校验位这些主要的信息。 /*brief 设置串口数据位,停止位和效验位*param fd 类型 int 打开的串口文件句柄*param databits 类型 int 数据位 取值 为 7 或者8*param stopbits 类型 int 停止位 取值为 1 或者2*param parity 类型 int 效验类型 取值为N,E,O,S*/int set_Parity(int fd,int databits,int stopbits,int pa

5、rity) struct termios options; if ( tcgetattr( fd,&options) != 0) perror(SetupSerial 1); return(FALSE); options.c_cflag &= CSIZE; options.c_lflag &= (ICANON | ECHO | ECHOE | ISIG); /*Input*/ options.c_oflag &= OPOST; /*Output*/ switch (databits) /*设置数据位数*/ case 7: options.c_cflag |= CS7; break; case

6、8: options.c_cflag |= CS8; break; default: fprintf(stderr,Unsupported data sizen); return (FALSE); switch (parity) case n: case N: options.c_cflag &= PARENB; /* Clear parity enable */ options.c_iflag &= INPCK; /* Enable parity checking */ break; case o: case O: options.c_cflag |= (PARODD | PARENB);

7、/* 设置为奇效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case e: case E: options.c_cflag |= PARENB; /* Enable parity */ options.c_cflag &= PARODD; /* 转换为偶效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case S: case s: /*as no parity*/ options.c_cflag &= PARENB

8、; options.c_cflag &= CSTOPB;break; default: fprintf(stderr,Unsupported parityn); return (FALSE); /* 设置停止位*/ switch (stopbits) case 1: options.c_cflag &= CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; default: fprintf(stderr,Unsupported stop bitsn); return (FALSE); /* Set input parity optio

9、n */ if (parity != n) options.c_iflag |= INPCK; tcflush(fd,TCIFLUSH);options.c_ccVTIME = 0; /* 设置超时0 seconds*/ options.c_ccVMIN = 13; /* define the minimum bytes data to be readed*/if (tcsetattr(fd,TCSANOW,&options) != 0) perror(SetupSerial 3); return (FALSE); return (TRUE); 在上述代码中,有两句话特别重要:options.

10、c_ccVTIME = 0; /* 设置超时0 seconds*/ options.c_ccVMIN = 13; /* define the minimum bytes data to be readed*/这两句话决定了对串口读取的函数read()的一些功能。我将着重介绍一下他们对read()函数的影响。 对串口操作的结构体是Struct tcflag_t c_iflag; /*输入模式标记*/ tcflag_t c_oflag; /*输出模式标记*/ tcflag_t c_cflag; /*控制模式标记*/ tcflag_t c_lflag; /*本地模式标记*/ cc_t c_line; /*线路规程*/

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号