Linux 多线程串口通信
大概流程就是打开一个串口、然后进行串口设置。开启二个线程,一个线程写数据,另一个线程读数据。
代码如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <errno.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <termios.h>
- #include <pthread.h>
- #include <sys/time.h>
- #define MAX 2
- pthread_t thread[2];
- pthread_mutex_t mut;
- int fd;
- int set_port(int fd,int nbits)
- {
- struct termios newtio,oldtio;
- if(tcgetattr(fd,&oldtio)!=0)
- {
- perror("pei zhi cuo wu1\n");
- return -1;
- }
- bzero(&newtio,sizeof(newtio)); //清零
- newtio.c_cflag |=CLOCAL|CREAD;//用于本地连接和接收使能
- newtio.c_cflag &=~CSIZE;//设置数据位
- switch(nbits)
- {
- case 7:
- newtio.c_cflag |=CS7;break;
- case 8:
- newtio.c_cflag |=CS8;break;
- }
- //设置奇校验位
- newtio.c_cflag |=PARENB;
- //设置波特率
- cfsetispeed(&newtio,B115200);
- cfsetospeed(&newtio,B115200);
- //设置停止位
- newtio.c_cflag &=~PARENB;
- if((tcsetattr(fd,TCSANOW,&newtio))!=0)
- {
- perror("pei zhi cuo wu2\n");
- return -1;
- }
- printf("bao cun wan bi \n");
- return 0;
- }
- void *thread1()
- {
- int i;
- printf ("thread1 \n");
- for( i=0;i<MAX;i++){
- pthread_mutex_lock(&mut);
- if(i==0){
- printf("write %d\n",i+1);
- char buf1[]="AT+FCLASS=0\r\n";
- int length=sizeof(buf1);
- int j=write(fd,buf1,length);
- puts(buf1);
- if(j<0)printf("fa song shi bai\n");
- printf("%d \n",j);
- }
- else if(i==1){
- printf("write %d\n",i+1);
- char buf2[]="AT+CBST=7,0,0\r\n";
- int length=sizeof(buf2);
- int j=write(fd,buf2,length);
- puts(buf2);
- if(j<0)printf("fa song shi bai\n");
- printf("%d \n",j);
- }
- sleep(3);
- pthread_mutex_unlock(&mut);
- }
- printf("thread1 stop\n");
- pthread_exit(NULL);
- }
- void *thread2()
- {
- int j;
- sleep(1);
- printf("thread2\n");
- char buf[100];
- for (j = 0; j< MAX; j++)
- {
- pthread_mutex_lock(&mut);
- sleep(3);
- printf("read %d\n",j+1);
- int k=read(fd,buf,100);
- printf("k+%d\n",k);
- puts(buf);
- pthread_mutex_unlock(&mut);
- sleep(2);
- }
- printf("thread2 :stop\n");
- pthread_exit(NULL);
- }
- void thread_create(void)
- {
- int temp;
- memset(&thread, 0, sizeof(thread)); //comment1
- /*创建线程*/
- if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2
- printf("xian chegn 1 faile\n");
- else
- printf("xian cheng 1 chegn gong\n");
- if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3
- printf("2 faile\n");
- else
- printf("2 surcess\n");
- }
- void thread_wait(void)
- {
- /*等待线程结束*/
- if(thread[0] !=0) { //comment4
- pthread_join(thread[0],NULL);
- printf("1 stop \n");
- }
- if(thread[1] !=0) { //comment5
- pthread_join(thread[1],NULL);
- printf("2 stop \n");
- }
- }
- int main(void) {
- int i,j,k;
- fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY|O_NDELAY);
- if(-1==fd)printf("mei da kai tong xin duan kou hao\n");
- else
- {
- i=set_port(fd, 8);
- if(i<0)
- {
- perror("pei zhi cuo wu3\n");
- return 0;
- }
- pthread_mutex_init(&mut,NULL);
- printf("creat preadth\n");
- thread_create();
- printf("chu li \n");
- thread_wait();
- close(fd);
- }
- return 0;
- }
Linux 多线程串口通信的更多相关文章
- linux下串口通信与管理
linux下的串口与windows有一些区别,下面将介绍一下linux下串口通信管理 查看是否支持USB串口: #lsmod | grep usbserial 如果没有信息:sudo apt-get ...
- 多线程串口通信 MFC CSerialPort
写在前面: 晚上应该继续完成未写完的代码,但Chrome上打开的标签实在太多了,约30个了,必须关掉一些,所以需要把自己看的整理一下然后关掉.本次主要写点MFC环境下多线程串口通信相关的东西,这包括线 ...
- Linux下串口通信工具minicom的用法
一.查看串口设备 例如,将USB转串口线插入交换机Console口后,执行命令:$ll /dev/ttyUSB* 二.连接串口设备 $sudo minicom -D /dev/ttyUSB0 三.设置 ...
- Qt串口通信专题教程
查看以前的教程:Qt编写串口通信程序全程图文讲解 查看Wincom和Lincom介绍:Qt跨平台串口通信软件Wincom与Lincom 下载软件,文档和源码:资源下载 ——————————————20 ...
- Qt 串口通信 高速发送出错的解决方法总结
使用网上的qextserialport-1.2类,自行开发多线程串口通信.开发的过程中,出现两个问题: 问题1:我用信号槽跨线程调用串口类MyCom 发送和接收数据,中间运行的时候,会内存错误,Q ...
- linux下的qt串口通信
1.linux下的qt串口通信跟windows唯一的差别就是端口号的名字,windows下面是COM,而linux是ttyUSB0的路径 2.一般情况下linux插上USB转串口线就可以在/dev/目 ...
- Linux与Windows串口通信
串口是常用的计算机与外部串行设备之间的数据传输通道,由于串行通信方便易行,所以应用广泛.现在国际上不断有串口新技术及新规格推出,结合社会各方面需要,串口通信发展的空间庞大.串口通讯技术因其自身的优势和 ...
- Linux 虚拟串口及 Qt 串口通信实例
Linux 虚拟串口及 Qt 串口通信实例 2011-06-22 17:49 佚名 互联网 字号:T | T Linux 虚拟串口及 Qt 串口通信实例是本文所要介绍的内容,在实现过程中,打开了两个伪 ...
- 多线程CSerialPort类的多串口通信实现
多线程CSerialPort类的多串口通信实现 工作了之后才发现,之前在学校里真是狭隘封闭.坐井观天,拿之前发表的论文来说,工作后接触到了底层的串口.网口开发,对线程(也叫任务).操作系统时间片轮流 ...
随机推荐
- debian安装jdk8
1.官方下载java想安装的JAVA版本: http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.比如说,常安装的J ...
- perl 回调必须是函数引用
[root@wx03 lib]# cat a1.pl use AE; use AnyEvent; ##定义watch my $t = AnyEvent->timer( after => 0 ...
- 设计模式(五)适配器模式Adapter(结构型)
设计模式(五)适配器模式Adapter(结构型) 1. 概述: 接口的改变,是一个需要程序员们必须(虽然很不情愿)接受和处理的普遍问题.程序提供者们修改他们的代码;系统库被修正;各种程序语言以及相 ...
- Ext的异步请求(二级级联动态加载下拉列表)
页面: <tr> <td class="label" width="300" >作业计划项模板</td> <td> ...
- 《高质量程序设计指南:C++/C语言》面试题整理
本试题仅用于考查C++/C程序员的基本编程技能.内容限于C++/C常用 语法,不涉及 数据结构. 算法以及深奥的语法.考试成绩能反映出考生的编程质量以及对C++/C的理解程度,但不能反映考生的智力和软 ...
- STL--G - For Fans of Statistics(两个推断条件-二分)
G - For Fans of Statistics Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- [linux]linux命令学习-netstat
linux非常多服务都与网络相关.当服务调不通或者是启动port被占用,或者是又是被防火墙挡住的时候,就须要查询网络相关的问题,netstat命令之前仅仅会用一两个參数这里.好好学习一番. 经常使用的 ...
- javascript实现快速排
其基本思路应该是排成两部分单独记录,确定枢轴,实施枢轴到左侧值我们都小于枢轴值.枢轴向右大于枢轴值.这样子不断递归下去 function quicksort(arr,low,high){ var pi ...
- UVA 2039 Pets(网络流)
Problem Description Are you interested in pets? There is a very famous pets shop in the center of th ...
- Storm流计算之项目篇(Storm+Kafka+HBase+Highcharts+JQuery,含3个完整实际项目)
1.1.课程的背景 Storm是什么? 为什么学习Storm? Storm是Twitter开源的分布式实时大数据处理框架,被业界称为实时版Hadoop. 随着越来越多的场景对Hadoop的MapRed ...