linux多线程下打开串口发送和接收数据

   

1 启动线程1读串口

2 等待3秒后

3 启动线程2写串口,发送字符串后关闭

4 等待10秒

5 关闭两个线程

 

[c-sharp]  view plain copy
  1. #include <pthread.h>  
  2. #include <stdio.h>  
  3. #include <sys/time.h>  
  4. #include <string.h>  
  5. #include<termios.h>  
  6. #include<sys/stat.h>  
  7. #include<fcntl.h>  
  8.  
  9. #define BAUDRATE B115200  
  10. #define MODEMDEVICE "/dev/ttyS0"  
  11. #define R_BUF_LEN (256)  
  12. void printtid(void);  
  13. void* com_read(void* pstatu)  
  14. {  
  15.     printtid();  
  16.     int i=0;  
  17.     int fd,c=0,num;  
  18.     struct termios oldtio,newtio;  
  19.     char buf[R_BUF_LEN];  
  20.     printf("start.../n");  
  21.     /*打开PC机的COM1通信端口*/  
  22.     fd=open(MODEMDEVICE,O_RDWR | O_NOCTTY | O_NONBLOCK/*| O_NDELAY*/);  
  23.     if(fd<0)  
  24.     {  
  25.         perror(MODEMDEVICE);  
  26.         exit(1);  
  27.     }  
  28.     printf("open .../n");  
  29.     /*将目前终端机的结构保存至oldtio结构*/  
  30.     tcgetattr(fd,&oldtio);  
  31.     /*清除newtio结构,重新设置通信协议*/  
  32.     bzero(&newtio,sizeof(newtio));  
  33.     /*通信协议设为8N1,8位数据位,N没有效验,1位结束位*/  
  34.     newtio.c_cflag = BAUDRATE |CS8|CLOCAL|CREAD;  
  35.     newtio.c_iflag = IGNPAR;  
  36.     newtio.c_oflag = 0;  
  37.     /*设置为正规模式*/  
  38.     newtio.c_lflag=ICANON;  
  39.     /*清除所有队列在串口的输入*/  
  40.     tcflush(fd,TCIFLUSH);   /*新的termios的结构作为通信端口的参数*/  
  41.     tcsetattr(fd,TCSANOW,&newtio);  
  42.     printf("reading.../n");  
  43.     while(*(int*)pstatu)  
  44.     {  
  45.         num = read(fd,buf, R_BUF_LEN);  
  46.         buf[R_BUF_LEN-1] = 0;  
  47.         if(num > 0 && num <= R_BUF_LEN)  
  48.         {   
  49.             buf[num]=0;  
  50.             printf("%s", buf);  
  51.             fflush(stdout);  
  52.         }  
  53.     }  
  54.     printf("close.../n");  
  55.     close(fd);  
  56.     /*恢复旧的通信端口参数*/  
  57.     tcsetattr(fd,TCSANOW,&oldtio);  
  58. }  
  59. void* com_send(void* p)  
  60. {  
  61.     printtid();  
  62.     int fd,c=0;  
  63.     struct termios oldtio,newtio;  
  64.     char ch;  
  65.     static char s1[20];  
  66.     printf("Start.../n ");  
  67.     /*打开arm平台的COM1通信端口*/  
  68.     fd=open(MODEMDEVICE,O_RDWR | O_NOCTTY);  
  69.     if(fd<0)  
  70.     {  
  71.         perror(MODEMDEVICE);  
  72.         exit(1);  
  73.     }  
  74.     printf(" Open.../n ");  
  75.      /*将目前终端机的结构保存至oldtio结构*/  
  76.        tcgetattr(fd,&oldtio);  
  77.     /*清除newtio结构,重新设置通信协议*/  
  78.     bzero(&newtio,sizeof(newtio));  
  79.     /*通信协议设为8N1*/  
  80.     newtio.c_cflag =BAUDRATE |CS8|CLOCAL|CREAD; //波特率 8个数据位 本地连接 接受使能  
  81.     newtio.c_iflag=IGNPAR;                      //忽略奇偶校验错误  
  82.     newtio.c_oflag=0;  
  83.     /*设置为正规模式*/  
  84.     newtio.c_lflag=ICANON;                     //规范输入  
  85.     /*清除所有队列在串口的输出*/  
  86.     tcflush(fd,TCOFLUSH);  
  87.     /*新的termios的结构作为通信端口的参数*/  
  88.     tcsetattr(fd,TCSANOW,&newtio);  
  89.     printf("Writing.../n ");  
  90.     ///*  
  91.     while(*(char*)p != 0)  
  92.     {  
  93.         int res = 0;  
  94.         res = write(fd,(char*)p, 1);  
  95.         if(res != 1) printf("send %c error/n", *(char*)p);  
  96.         else printf("send %c ok/n", *(char*)p);  
  97.         ++p;  
  98.     }  
  99.     printf("Close.../n");  
  100.     close(fd);  
  101.     /*还原旧的通信端口参数*/  
  102.     tcsetattr(fd,TCSANOW,&oldtio);  
  103.     printf("leave send thread/n");  
  104. }  
  105. /* 
  106.     开始线程 
  107.     thread_fun  线程函数 
  108.     pthread     线程函数所在pthread变量 
  109.     par     线程函数参数 
  110.     COM_STATU   线程函数状态控制变量 1:运行 0:退出 
  111. */  
  112. int start_thread_func(void*(*func)(void*), pthread_t* pthread, void* par, int* COM_STATU)  
  113. {  
  114.     *COM_STATU = 1;  
  115.     memset(pthread, 0, sizeof(pthread_t));  
  116.     int temp;  
  117.         /*创建线程*/  
  118.         if((temp = pthread_create(pthread, NULL, func, par)) != 0)  
  119.         printf("线程创建失败!/n");  
  120.         else  
  121.     {  
  122.         int id = pthread_self();  
  123.                 printf("线程%u被创建/n", *pthread);  
  124.     }  
  125.     return temp;  
  126. }  
  127. /* 
  128.     结束线程 
  129.     pthread     线程函数所在pthread变量 
  130.     COM_STATU   线程函数状态控制变量 1:运行 0:退出 
  131. */  
  132. int stop_thread_func(pthread_t* pthread, int* COM_STATU)  
  133. {  
  134.     printf("prepare stop thread %u/n", *pthread);  
  135.     *COM_STATU = 0;  
  136.     if(*pthread !=0)   
  137.     {  
  138.                 pthread_join(*pthread, NULL);  
  139.     }  
  140.     printf("线程%d退出!/n", *COM_STATU);  
  141. }  
  142. void printtid(void)  
  143. {  
  144.     int id = pthread_self();  
  145.         printf("in thread %u/n", id);  
  146. }  
  147. int main()  
  148. {  
  149.     pthread_t thread[2];  
  150.     printtid();  
  151.     const int READ_THREAD_ID = 0;  
  152.     const int SEND_THREAD_ID = 1;  
  153.     int COM_READ_STATU = 0;  
  154.     int COM_SEND_STATU = 0;  
  155.     if(start_thread_func(com_read, &thread[READ_THREAD_ID],  &COM_READ_STATU, &COM_READ_STATU) != 0)  
  156.     {  
  157.         printf("error to leave/n");  
  158.         return -1;  
  159.     }  
  160.     printf("wait 3 sec/n");  
  161.     sleep(3);  
  162.     printf("wake after 3 sec/n");  
  163.     if(start_thread_func(com_send, &thread[SEND_THREAD_ID], "ABCDEFGHIJKLMNOPQRST", &COM_SEND_STATU) != 0)  
  164.     {  
  165.         printf("error to leave/n");  
  166.         return -1;  
  167.     }  
  168.     printtid();  
  169.     printf("wait 10 sec/n");  
  170.     sleep(10);  
  171.     printf("wake after 10 sec/n");  
  172.     stop_thread_func(&thread[READ_THREAD_ID], &COM_READ_STATU);  
  173.     stop_thread_func(&thread[SEND_THREAD_ID], &COM_SEND_STATU);  
  174.     return 0;  
  175. }  

 

涉及到线程基础知识和串口知识

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值