Linux下的串口编程.doc_第1页
Linux下的串口编程.doc_第2页
Linux下的串口编程.doc_第3页
Linux下的串口编程.doc_第4页
Linux下的串口编程.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Linux下的串口编程一:串口发送端程序/*文件:w_uart.c*编写者:huangminqiang*编写日期:2012年10月15号*简要描述:串口发送程序,在PC机上发送。*修改者:*修改日期:2012年11月12号*注:*/#include #include #include #include #include #include #define COM /dev/ttyS0typedef enumNON, /无校验ODD, /偶校验EVEN,/奇校验cal_t;/*设置串口*/static int set_com(int fd, int speed, int bits, cal_t cal, int stop )struct termios curtio;memset(&curtio, 0, sizeof(curtio);/取得串口已有的属性if (0 != tcgetattr(fd, &curtio)perror(Failed to tcgetattr);return -1;/设置输入输出波特率cfsetispeed(&curtio, speed);cfsetospeed(&curtio, speed);/设置为原始模式curtio.c_lflag &= (ECHO | ICANON | IEXTEN | ISIG | ECHOE | ECHOK | ECHONL);curtio.c_iflag &= (BRKINT | IUCLC | ICRNL | INLCR | IGNCR);/激活相应选项curtio.c_cflag |= CLOCAL | CREAD;/设置数据位curtio.c_cflag &= CSIZE;curtio.c_cflag |= bits;/设置校验位if (ODD = cal)curtio.c_iflag |= (INPCK | ISTRIP);curtio.c_cflag |= PARENB;curtio.c_cflag |= PARODD;else if(EVEN = cal)curtio.c_iflag |= (INPCK | ISTRIP);curtio.c_cflag |= PARENB;curtio.c_cflag &= PARODD;elsecurtio.c_cflag &= PARENB;/设置停止位if (2 = stop)curtio.c_cflag |= CSTOPB;elsecurtio.c_cflag &= CSTOPB;/设置最少字符等待时间curtio.c_ccVTIME = 0;curtio.c_ccVMIN = 0;/清空缓冲tcflush(fd, TCIOFLUSH);/设置新串口属性if (0 != tcsetattr(fd, TCSANOW, &curtio)perror(Failed to tcgetattr);return -1;printf(set done!n);return 0;/*写入串口信息*/int WriteUartInfo(void)int fd;int cnt = 0;int w_cnt = 0;unsigned char w_buf128;/打开串口fd = open(COM, O_RDWR);if(0 fd)perror(uart open err:);return -1;#if 0/设置串口参数if (0 != set_com(fd, B115200, CS8, NON, 1)printf(set_com failed!n);goto _out;#endif/发送信息while(1)printf(plese input a buffer : );memset(w_buf, 0, sizeof(w_buf);fgets(w_buf, sizeof(w_buf), stdin);w_cnt = write(fd, w_buf, sizeof(w_buf);if(0 w_cnt)perror(write error : );goto _out;printf(sent out ! n);/结束判断if( !strncmp(w_buf, quit, 4) )break;/关闭串口close(fd);return 0;_out:close(fd);return -1;/* 主函数 */int main(void)if( WriteUartInfo() )printf(write uart data failed!n);return -1;return 0;二:串口接收端程序/*文件:r_uart.c*编写者:huangminqiang*编写日期:2012年10月15号*简要描述:串口接收程序,在板子上运行。*修改者:*修改日期:2012年11月12号*注:*/#include #include #include #include #include #include #define COM /dev/ttyAMA0/* 读取串口信息 */int ReadUartInfo(void)int fd;int cnt = 0;int r_cnt = 0;struct termios attr;fd_set r_fds;struct timeval tv;unsigned char r_buf128 = 0;/打开串口fd = open(COM, O_RDWR);if(0 fd)perror(open uart error : );return -1;/由于串口设置已固化,故不需要设置。#if 0/设置串口参数if ( set_com(fd, B115200, CS8, NON, 1) )printf(set_com failed!n);goto _out;#endifwhile(1)/清除监测集合FD_ZERO(&r_fds);/将串口句柄加入到监测集合中FD_SET(fd, &r_fds);/设置超时为3秒tv.tv_sec = 3;tv.tv_usec = 0;/监测串口是否有数据接收到,超时为3秒cnt = select(fd + 1, &r_fds, NULL, NULL, &tv);switch(cnt)case 0:/超时/printf(time out !n);break;case -1:/错误perror(select : );goto _out;default:if( FD_ISSET(fd, &r_fds) ) /有数据可读/接收数据r_cnt = read(fd, r_buf, sizeof (r_buf);if(0 r_cnt)perror(read error : );goto _out;/printf(%s, r_buf);/system(r_buf);memset(r_buf, 0, sizeof(r_buf);/ 关闭串口close(fd);return 0;_out:close(fd);return -1;/* 主函数

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论