利用select接收串口数据 亲测可用小demo
来源:互联网 发布:淘宝首页服务 编辑:程序博客网 时间:2024/06/05 03:16
/*开启一个线程,用于接收串口数据并打印*/#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <errno.h> #include <string.h> #include <pthread.h>int fd; fd_set rd;void pthread_uartrecv(void){ struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 10000; int nread; char buff[8]; bzero(buff,8); while(1) {FD_ZERO(&rd); FD_SET(fd,&rd);//printf("here2\n"); if(FD_ISSET(fd,&rd)) { //printf("here1\n"); select(fd+1,&rd,NULL,NULL,NULL); nread = read(fd,buff,8); buff[nread]='\0'; printf("nread = %d,%s\n",nread,buff); } }} int main() { struct termios opt; fd = open("/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NDELAY); fcntl(fd, F_SETFL, 0); tcgetattr(fd, &opt); cfsetispeed(&opt, B9600); cfsetospeed(&opt, B9600); if(tcsetattr(fd, TCSANOW, &opt) != 0 ) { perror("tcsetattr error1"); return -1; } opt.c_cflag &= ~CSIZE; opt.c_cflag |= CS8; opt.c_cflag &= ~CSTOPB; opt.c_cflag &= ~PARENB; opt.c_iflag &= ~INPCK;//opt.c_cflag &= ~INPCK;the last this is fault set opt.c_cflag |= (CLOCAL | CREAD); opt.c_cflag &= ~CRTSCTS; opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); opt.c_oflag &= ~OPOST; opt.c_oflag &= ~(ONLCR | OCRNL); //添加的 opt.c_iflag &= ~(ICRNL | INLCR); opt.c_iflag &= ~(IXON | IXOFF | IXANY); //添加的 opt.c_cc[VTIME] = 8; opt.c_cc[VMIN] = 10; tcflush(fd, TCIOFLUSH); printf("configure complete\n"); if(tcsetattr(fd, TCSANOW, &opt) != 0) { perror("serial error"); return -1; } pthread_t id; pthread_create(&id,NULL,(void*)pthread_uartrecv,NULL); while(1) { sleep(2);printf("this is main function\n"); } }
阅读全文
0 0
- 利用select接收串口数据 亲测可用小demo
- 改进利用串口的FIFO接收数据
- epoll 获取串口数据 亲测可用
- 关于利用android-serialport-api实现在安卓设备上进行串口通信,附精简版demo,亲测可用。
- VC上位机MFC利用串口控件发送接收数据
- 利用STM32 的串口来发送和接收数据实验
- MATLAB利用串口接收数据,并实时显示图形
- 利用epoll接收串口数据和socket进程通信问题
- linux下的select用于串口接收数据时,select函数无法返回的问题
- 串口接收数据c#
- 串口接收数据c#
- 串口数据接收小结
- Linux 串口接收数据
- Matlab接收串口数据
- Java接收串口数据
- Matlab接收串口数据
- 串口接收数据
- 串口数据的接收
- 起泡排序验证性实验
- WebGL 着色器语言(GLSL ES)
- Visual Paradigm for UML工具
- php excel文件导出之二 图像导出
- charles使用教程
- 利用select接收串口数据 亲测可用小demo
- 配置本地yum源
- BZOJ1823 [JSOI2010]满汉全席
- 图--最小生成树(数据结构)
- 缓存技术:Redis与Memcached对比
- 关于JAVA的redis客户端的实现
- 使用word 2013 发布csdn博客
- 往数据库里插入时间数据时,时间自动减少了14h
- HeadFirst设计模式总结_第四章工厂模式