大概流程就是打开一个串口、然后进行串口设置。开启二个线程,一个线程写数据,另一个线程读数据。

代码如下:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <errno.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <termios.h>
  10. #include <pthread.h>
  11. #include <sys/time.h>
  12.  
  13. #define MAX 2
  14. pthread_t thread[2];
  15. pthread_mutex_t mut;
  16. int fd;
  17.  
  18. int set_port(int fd,int nbits)
  19. {
  20. struct termios newtio,oldtio;
  21. if(tcgetattr(fd,&oldtio)!=0)
  22. {
  23. perror("pei zhi cuo wu1\n");
  24. return -1;
  25. }
  26.  
  27. bzero(&newtio,sizeof(newtio)); //清零
  28. newtio.c_cflag |=CLOCAL|CREAD;//用于本地连接和接收使能
  29.  
  30. newtio.c_cflag &=~CSIZE;//设置数据位
  31. switch(nbits)
  32. {
  33. case 7:
  34. newtio.c_cflag |=CS7;break;
  35. case 8:
  36. newtio.c_cflag |=CS8;break;
  37. }
  38.  
  39. //设置奇校验位
  40. newtio.c_cflag |=PARENB;
  41.  
  42. //设置波特率
  43. cfsetispeed(&newtio,B115200);
  44. cfsetospeed(&newtio,B115200);
  45.  
  46. //设置停止位
  47. newtio.c_cflag &=~PARENB;
  48.  
  49. if((tcsetattr(fd,TCSANOW,&newtio))!=0)
  50. {
  51. perror("pei zhi cuo wu2\n");
  52. return -1;
  53. }
  54.  
  55. printf("bao cun wan bi \n");
  56. return 0;
  57. }
  58.  
  59. void *thread1()
  60. {
  61. int i;
  62. printf ("thread1 \n");
  63. for( i=0;i<MAX;i++){
  64. pthread_mutex_lock(&mut);
  65. if(i==0){
  66. printf("write %d\n",i+1);
  67. char buf1[]="AT+FCLASS=0\r\n";
  68. int length=sizeof(buf1);
  69. int j=write(fd,buf1,length);
  70. puts(buf1);
  71. if(j<0)printf("fa song shi bai\n");
  72. printf("%d \n",j);
  73. }
  74. else if(i==1){
  75. printf("write %d\n",i+1);
  76. char buf2[]="AT+CBST=7,0,0\r\n";
  77. int length=sizeof(buf2);
  78. int j=write(fd,buf2,length);
  79. puts(buf2);
  80. if(j<0)printf("fa song shi bai\n");
  81. printf("%d \n",j);
  82.  
  83. }
  84. sleep(3);
  85. pthread_mutex_unlock(&mut);
  86. }
  87. printf("thread1 stop\n");
  88. pthread_exit(NULL);
  89.  
  90. }
  91.  
  92. void *thread2()
  93. {
  94. int j;
  95. sleep(1);
  96. printf("thread2\n");
  97. char buf[100];
  98. for (j = 0; j< MAX; j++)
  99. {
  100. pthread_mutex_lock(&mut);
  101. sleep(3);
  102. printf("read %d\n",j+1);
  103. int k=read(fd,buf,100);
  104. printf("k+%d\n",k);
  105. puts(buf);
  106. pthread_mutex_unlock(&mut);
  107. sleep(2);
  108. }
  109. printf("thread2 :stop\n");
  110. pthread_exit(NULL);
  111.  
  112. }
  113.  
  114. void thread_create(void)
  115. {
  116. int temp;
  117. memset(&thread, 0, sizeof(thread)); //comment1
  118. /*创建线程*/
  119. if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2
  120. printf("xian chegn 1 faile\n");
  121. else
  122. printf("xian cheng 1 chegn gong\n");
  123.  
  124. if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3
  125. printf("2 faile\n");
  126. else
  127. printf("2 surcess\n");
  128. }
  129.  
  130. void thread_wait(void)
  131. {
  132. /*等待线程结束*/
  133. if(thread[0] !=0) { //comment4
  134. pthread_join(thread[0],NULL);
  135. printf("1 stop \n");
  136. }
  137. if(thread[1] !=0) { //comment5
  138. pthread_join(thread[1],NULL);
  139. printf("2 stop \n");
  140. }
  141. }
  142.  
  143. int main(void) {
  144. int i,j,k;
  145. fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY|O_NDELAY);
  146. if(-1==fd)printf("mei da kai tong xin duan kou hao\n");
  147. else
  148. {
  149. i=set_port(fd, 8);
  150. if(i<0)
  151. {
  152. perror("pei zhi cuo wu3\n");
  153. return 0;
  154. }
  155. pthread_mutex_init(&mut,NULL);
  156. printf("creat preadth\n");
  157. thread_create();
  158. printf("chu li \n");
  159. thread_wait();
  160. close(fd);
  161. }
  162. return 0;
  163.  
  164. }

  

Linux 多线程串口通信的更多相关文章

  1. linux下串口通信与管理

    linux下的串口与windows有一些区别,下面将介绍一下linux下串口通信管理 查看是否支持USB串口: #lsmod | grep usbserial 如果没有信息:sudo apt-get ...

  2. 多线程串口通信 MFC CSerialPort

    写在前面: 晚上应该继续完成未写完的代码,但Chrome上打开的标签实在太多了,约30个了,必须关掉一些,所以需要把自己看的整理一下然后关掉.本次主要写点MFC环境下多线程串口通信相关的东西,这包括线 ...

  3. Linux下串口通信工具minicom的用法

    一.查看串口设备 例如,将USB转串口线插入交换机Console口后,执行命令:$ll /dev/ttyUSB* 二.连接串口设备 $sudo minicom -D /dev/ttyUSB0 三.设置 ...

  4. Qt串口通信专题教程

    查看以前的教程:Qt编写串口通信程序全程图文讲解 查看Wincom和Lincom介绍:Qt跨平台串口通信软件Wincom与Lincom 下载软件,文档和源码:资源下载 ——————————————20 ...

  5. Qt 串口通信 高速发送出错的解决方法总结

    使用网上的qextserialport-1.2类,自行开发多线程串口通信.开发的过程中,出现两个问题:   问题1:我用信号槽跨线程调用串口类MyCom 发送和接收数据,中间运行的时候,会内存错误,Q ...

  6. linux下的qt串口通信

    1.linux下的qt串口通信跟windows唯一的差别就是端口号的名字,windows下面是COM,而linux是ttyUSB0的路径 2.一般情况下linux插上USB转串口线就可以在/dev/目 ...

  7. Linux与Windows串口通信

    串口是常用的计算机与外部串行设备之间的数据传输通道,由于串行通信方便易行,所以应用广泛.现在国际上不断有串口新技术及新规格推出,结合社会各方面需要,串口通信发展的空间庞大.串口通讯技术因其自身的优势和 ...

  8. Linux 虚拟串口及 Qt 串口通信实例

    Linux 虚拟串口及 Qt 串口通信实例 2011-06-22 17:49 佚名 互联网 字号:T | T Linux 虚拟串口及 Qt 串口通信实例是本文所要介绍的内容,在实现过程中,打开了两个伪 ...

  9. 多线程CSerialPort类的多串口通信实现

    多线程CSerialPort类的多串口通信实现  工作了之后才发现,之前在学校里真是狭隘封闭.坐井观天,拿之前发表的论文来说,工作后接触到了底层的串口.网口开发,对线程(也叫任务).操作系统时间片轮流 ...

随机推荐

  1. debian安装jdk8

    1.官方下载java想安装的JAVA版本: http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.比如说,常安装的J ...

  2. perl 回调必须是函数引用

    [root@wx03 lib]# cat a1.pl use AE; use AnyEvent; ##定义watch my $t = AnyEvent->timer( after => 0 ...

  3. 设计模式(五)适配器模式Adapter(结构型)

      设计模式(五)适配器模式Adapter(结构型) 1. 概述: 接口的改变,是一个需要程序员们必须(虽然很不情愿)接受和处理的普遍问题.程序提供者们修改他们的代码;系统库被修正;各种程序语言以及相 ...

  4. Ext的异步请求(二级级联动态加载下拉列表)

    页面: <tr> <td class="label" width="300" >作业计划项模板</td> <td> ...

  5. 《高质量程序设计指南:C++/C语言》面试题整理

    本试题仅用于考查C++/C程序员的基本编程技能.内容限于C++/C常用 语法,不涉及 数据结构. 算法以及深奥的语法.考试成绩能反映出考生的编程质量以及对C++/C的理解程度,但不能反映考生的智力和软 ...

  6. STL--G - For Fans of Statistics(两个推断条件-二分)

    G - For Fans of Statistics Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & ...

  7. [linux]linux命令学习-netstat

    linux非常多服务都与网络相关.当服务调不通或者是启动port被占用,或者是又是被防火墙挡住的时候,就须要查询网络相关的问题,netstat命令之前仅仅会用一两个參数这里.好好学习一番. 经常使用的 ...

  8. javascript实现快速排

    其基本思路应该是排成两部分单独记录,确定枢轴,实施枢轴到左侧值我们都小于枢轴值.枢轴向右大于枢轴值.这样子不断递归下去 function quicksort(arr,low,high){ var pi ...

  9. UVA 2039 Pets(网络流)

    Problem Description Are you interested in pets? There is a very famous pets shop in the center of th ...

  10. Storm流计算之项目篇(Storm+Kafka+HBase+Highcharts+JQuery,含3个完整实际项目)

    1.1.课程的背景 Storm是什么? 为什么学习Storm? Storm是Twitter开源的分布式实时大数据处理框架,被业界称为实时版Hadoop. 随着越来越多的场景对Hadoop的MapRed ...