作者 潘浩彬

更新 JZsdk_Uart_UartDeal.c

1 -#include <stdio.h>  
2 -#include <string.h>  
3 -#include <pthread.h>  
4 -#include <stdlib.h>  
5 -  
6 -#include <fcntl.h>  
7 -#include <unistd.h>  
8 -#include <termios.h>  
9 -#include <sys/time.h>  
10 -#include "Uart_Config.h"  
11 -#include "JZsdk_Uart_Recv/JZsdk_Uart_RecvDeal.h"  
12 -#include "JZsdkLib.h"  
13 -  
14 -static int Uart_4G_fd;  
15 -static int Uart_DEV1_fd;  
16 -static int Uart_DEV2_fd;  
17 -  
18 -static void *UartDeal_rece(void *arg);  
19 -  
20 -/*********************  
21 - *  
22 - * 串口接收线程  
23 - *  
24 - * *****************/  
25 -/******************************************************************  
26 -  
27 - 创建串口接收线程  
28 -  
29 -******************************************************************/  
30 -int JZsdk_Uart_UartDeal_Receive(int Uart_fd, int Uart_Dev_name)  
31 -{  
32 - int ret = 0;  
33 - pthread_t Uart_rece_task;  
34 -  
35 - pthread_attr_t task_attribute; //线程属性  
36 - pthread_attr_init(&task_attribute); //初始化线程属性  
37 - pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性  
38 -  
39 - if (Uart_Dev_name == UART_DEV_1)  
40 - {  
41 - Uart_DEV1_fd = Uart_fd;  
42 - }  
43 - else if (Uart_Dev_name == UART_DEV_2)  
44 - {  
45 - Uart_DEV2_fd = Uart_fd;  
46 - }  
47 - else if (Uart_Dev_name == UART_4G)  
48 - {  
49 - Uart_4G_fd = Uart_fd;  
50 - }  
51 -  
52 - int* uart_fd_ptr = malloc(sizeof(int)); // 动态分配内存来存储 Uart_fd 变量  
53 - *uart_fd_ptr = Uart_fd;  
54 -  
55 - ret = pthread_create(&Uart_rece_task,&task_attribute,UartDeal_rece,uart_fd_ptr); //串口接收线程  
56 - if(ret != 0)  
57 - {  
58 - printf("创建串口%x 接收线程失败!\n",Uart_Dev_name);  
59 - free(uart_fd_ptr);  
60 - }  
61 - else{  
62 - printf("创建串口%x 接收线程成功!\n",Uart_Dev_name);  
63 - }  
64 -}  
65 -  
66 -static int UartDeal_Recv_interface(int Uart_fd, unsigned char *getbuf, int len)  
67 -{  
68 - int i = 0;  
69 - int Frame_len = 0;  
70 -  
71 - if (Uart_fd == Uart_DEV1_fd)  
72 - {  
73 - USER_LOG_INFO("串口-设备1号,接受到数据len: %d", len);  
74 - printf("getbuf: %s\n", getbuf);  
75 - for (int i = 0; i < len; i++)  
76 - {  
77 - printf("%x ",getbuf[i]);  
78 - }  
79 - printf("\n");  
80 -  
81 - do  
82 - {  
83 - if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)  
84 - || (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))  
85 - {  
86 - Frame_len = (getbuf[3] << 8) + getbuf[4];  
87 - JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV1(&getbuf[i], Frame_len);  
88 -  
89 - i = i+Frame_len;  
90 - len = len - Frame_len;  
91 - Frame_len = 0;  
92 - }  
93 - else  
94 - {  
95 - JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV1(&getbuf[i], len);  
96 - len = 0;  
97 - }  
98 -  
99 - }  
100 - while (len >= 12);  
101 - }  
102 - else if (Uart_fd == Uart_DEV2_fd)  
103 - {  
104 - USER_LOG_INFO("串口-设备2号,接受到数据len: %d ", len);  
105 - printf("getbuf: %s\n", getbuf);  
106 - for (int i = 0; i < len; i++)  
107 - {  
108 - printf("%x ",getbuf[i]);  
109 - }  
110 - printf("\n");  
111 -  
112 - do  
113 - {  
114 - if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)  
115 - || (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))  
116 - {  
117 - Frame_len = (getbuf[3] << 8) + getbuf[4];  
118 - JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV2(&getbuf[i], Frame_len);  
119 -  
120 - i = i+Frame_len;  
121 - len = len - Frame_len;  
122 - Frame_len = 0;  
123 - }  
124 - else  
125 - {  
126 - JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV2(&getbuf[i], len);  
127 - len = 0;  
128 - }  
129 -  
130 - }  
131 - while (len >= 12);  
132 - }  
133 - else if (Uart_fd == Uart_4G_fd)  
134 - {  
135 - USER_LOG_INFO("串口-设备4G,接受到数据len: %d ", len);  
136 - printf("getbuf: %s\n", getbuf);  
137 - for (int i = 0; i < len; i++)  
138 - {  
139 - printf("%x ",getbuf[i]);  
140 - }  
141 - printf("\n");  
142 -  
143 - do  
144 - {  
145 - if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)  
146 - || (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))  
147 - {  
148 - Frame_len = (getbuf[3] << 8) + getbuf[4];  
149 - JZsdk_Uart_RecvDeal_CharmDeal_Uart_4G(&getbuf[i], Frame_len);  
150 -  
151 - i = i+Frame_len;  
152 - len = len - Frame_len;  
153 - Frame_len = 0;  
154 - }  
155 - else  
156 - {  
157 - JZsdk_Uart_RecvDeal_CharmDeal_Uart_4G(&getbuf[i], len);  
158 - len = 0;  
159 - }  
160 -  
161 - }  
162 - while (len >= 12);  
163 - }  
164 -}  
165 -  
166 -static void *UartDeal_rece(void *arg)  
167 -{  
168 -  
169 - unsigned char getbuf[1024];  
170 -  
171 - int ret = 0;  
172 - fd_set fs_read;  
173 - struct timeval tv_timeout;  
174 -  
175 - int Uart_fd = *(int*) arg;  
176 -  
177 - //FD_ZERO 将指定的文件描述符集清空,在对文件描述符集合进行设置前,必须对其进行初始化  
178 - //如果不清空,由于在系统分配内存空间后,通常并不作清空处理,所以结果是不可知的。  
179 - FD_ZERO(&fs_read);  
180 -  
181 - //FD_SET 用于在文件描述符集合中增加一个新的文件描述符。  
182 - FD_SET(Uart_fd, &fs_read);  
183 -  
184 - //115200 / char 8 位 = 14400 个char数据  
185 - tv_timeout.tv_sec = 6000;//(10*20/115200+2);  
186 - tv_timeout.tv_usec = 0;  
187 -  
188 - //2、正常接收  
189 - while(1)  
190 - {  
191 - //检查fs_read套节字是否有数据  
192 - select(Uart_fd+1, &fs_read, NULL, NULL, &tv_timeout);  
193 - delayMs(10);  
194 -  
195 - //FD_ISSET 用于测试指定的文件描述符是否在该集合中。  
196 - //Uart_fd 是否在fsread中  
197 - if (FD_ISSET(Uart_fd, &fs_read))  
198 - {  
199 - //1、读取串口内容 ret 接收长度 getbuf 获取的字符  
200 - memset(getbuf,0,sizeof(getbuf)); //清空接收数组  
201 - ret = read(Uart_fd,getbuf,sizeof(getbuf));  
202 -  
203 - USER_LOG_INFO("RecvLen:%d\n",ret);  
204 -  
205 - //UartDeal_Recv_interface(Uart_fd, getbuf, ret);  
206 -  
207 - }  
208 - }  
209 -  
210 - free(arg);  
211 -  
212 -}  
213 -  
214 -/****************  
215 - *  
216 - *  
217 - * 发送函数  
218 - *  
219 - * ****************/  
220 -int JZsdk_Uart_UartSend(int UartPort ,unsigned char *send, int num)  
221 -{  
222 - if (UartPort == UART_4G)  
223 - {  
224 - write(Uart_4G_fd,send, num);  
225 - return 0;  
226 - }  
227 - else if (UartPort == UART_DEV_1)  
228 - {  
229 - write(Uart_DEV1_fd,send, num);  
230 - return 0;  
231 - }  
232 - else if (UartPort == UART_DEV_2)  
233 - {  
234 - write(Uart_DEV2_fd,send, num);  
235 - return 0;  
236 - }  
237 -} 1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <pthread.h>
  4 +#include <stdlib.h>
  5 +
  6 +#include <fcntl.h>
  7 +#include <unistd.h>
  8 +#include <termios.h>
  9 +#include <sys/time.h>
  10 +#include "Uart_Config.h"
  11 +#include "JZsdk_Uart_Recv/JZsdk_Uart_RecvDeal.h"
  12 +#include "JZsdkLib.h"
  13 +
  14 +static int Uart_4G_fd;
  15 +static int Uart_DEV1_fd;
  16 +static int Uart_DEV2_fd;
  17 +
  18 +static void *UartDeal_rece(void *arg);
  19 +
  20 +/*********************
  21 + *
  22 + * 串口接收线程
  23 + *
  24 + * *****************/
  25 +/******************************************************************
  26 +
  27 + 创建串口接收线程
  28 +
  29 +******************************************************************/
  30 +int JZsdk_Uart_UartDeal_Receive(int Uart_fd, int Uart_Dev_name)
  31 +{
  32 + int ret = 0;
  33 + pthread_t Uart_rece_task;
  34 +
  35 + pthread_attr_t task_attribute; //线程属性
  36 + pthread_attr_init(&task_attribute); //初始化线程属性
  37 + pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性
  38 +
  39 + if (Uart_Dev_name == UART_DEV_1)
  40 + {
  41 + Uart_DEV1_fd = Uart_fd;
  42 + }
  43 + else if (Uart_Dev_name == UART_DEV_2)
  44 + {
  45 + Uart_DEV2_fd = Uart_fd;
  46 + }
  47 + else if (Uart_Dev_name == UART_4G)
  48 + {
  49 + Uart_4G_fd = Uart_fd;
  50 + }
  51 +
  52 + int* uart_fd_ptr = malloc(sizeof(int)); // 动态分配内存来存储 Uart_fd 变量
  53 + *uart_fd_ptr = Uart_fd;
  54 +
  55 + ret = pthread_create(&Uart_rece_task,&task_attribute,UartDeal_rece,uart_fd_ptr); //串口接收线程
  56 + if(ret != 0)
  57 + {
  58 + printf("创建串口%x 接收线程失败!\n",Uart_Dev_name);
  59 + free(uart_fd_ptr);
  60 + }
  61 + else{
  62 + printf("创建串口%x 接收线程成功!\n",Uart_Dev_name);
  63 + }
  64 +}
  65 +
  66 +static int UartDeal_Recv_interface(int Uart_fd, unsigned char *getbuf, int len)
  67 +{
  68 + int i = 0;
  69 + int Frame_len = 0;
  70 +
  71 + if (Uart_fd == Uart_DEV1_fd)
  72 + {
  73 + USER_LOG_INFO("串口-设备1号,接受到数据len: %d", len);
  74 + printf("getbuf: %s\n", getbuf);
  75 + for (int i = 0; i < len; i++)
  76 + {
  77 + printf("%x ",getbuf[i]);
  78 + }
  79 + printf("\n");
  80 +
  81 + do
  82 + {
  83 + if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)
  84 + || (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))
  85 + {
  86 + Frame_len = (getbuf[3] << 8) + getbuf[4];
  87 + JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV1(&getbuf[i], Frame_len);
  88 +
  89 + i = i+Frame_len;
  90 + len = len - Frame_len;
  91 + Frame_len = 0;
  92 + }
  93 + else
  94 + {
  95 + JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV1(&getbuf[i], len);
  96 + len = 0;
  97 + }
  98 +
  99 + }
  100 + while (len >= 12);
  101 + }
  102 + else if (Uart_fd == Uart_DEV2_fd)
  103 + {
  104 + USER_LOG_INFO("串口-设备2号,接受到数据len: %d ", len);
  105 + printf("getbuf: %s\n", getbuf);
  106 + for (int i = 0; i < len; i++)
  107 + {
  108 + printf("%x ",getbuf[i]);
  109 + }
  110 + printf("\n");
  111 +
  112 + do
  113 + {
  114 + if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)
  115 + || (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))
  116 + {
  117 + Frame_len = (getbuf[3] << 8) + getbuf[4];
  118 + JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV2(&getbuf[i], Frame_len);
  119 +
  120 + i = i+Frame_len;
  121 + len = len - Frame_len;
  122 + Frame_len = 0;
  123 + }
  124 + else
  125 + {
  126 + JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV2(&getbuf[i], len);
  127 + len = 0;
  128 + }
  129 +
  130 + }
  131 + while (len >= 12);
  132 + }
  133 + else if (Uart_fd == Uart_4G_fd)
  134 + {
  135 + USER_LOG_INFO("串口-设备4G,接受到数据len: %d ", len);
  136 + printf("getbuf: %s\n", getbuf);
  137 + for (int i = 0; i < len; i++)
  138 + {
  139 + printf("%x ",getbuf[i]);
  140 + }
  141 + printf("\n");
  142 +
  143 + do
  144 + {
  145 + if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)
  146 + || (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))
  147 + {
  148 + Frame_len = (getbuf[3] << 8) + getbuf[4];
  149 + JZsdk_Uart_RecvDeal_CharmDeal_Uart_4G(&getbuf[i], Frame_len);
  150 +
  151 + i = i+Frame_len;
  152 + len = len - Frame_len;
  153 + Frame_len = 0;
  154 + }
  155 + else
  156 + {
  157 + JZsdk_Uart_RecvDeal_CharmDeal_Uart_4G(&getbuf[i], len);
  158 + len = 0;
  159 + }
  160 +
  161 + }
  162 + while (len >= 12);
  163 + }
  164 +}
  165 +
  166 +static void *UartDeal_rece(void *arg)
  167 +{
  168 +
  169 + unsigned char getbuf[1024];
  170 +
  171 + int ret = 0;
  172 + fd_set fs_read;
  173 + struct timeval tv_timeout;
  174 +
  175 + int Uart_fd = *(int*) arg;
  176 +
  177 + //FD_ZERO 将指定的文件描述符集清空,在对文件描述符集合进行设置前,必须对其进行初始化
  178 + //如果不清空,由于在系统分配内存空间后,通常并不作清空处理,所以结果是不可知的。
  179 + FD_ZERO(&fs_read);
  180 +
  181 + //FD_SET 用于在文件描述符集合中增加一个新的文件描述符。
  182 + FD_SET(Uart_fd, &fs_read);
  183 +
  184 + //115200 / char 8 位 = 14400 个char数据
  185 + tv_timeout.tv_sec = 6000;//(10*20/115200+2);
  186 + tv_timeout.tv_usec = 0;
  187 +
  188 + //2、正常接收
  189 + while(1)
  190 + {
  191 + //检查fs_read套节字是否有数据
  192 + select(Uart_fd+1, &fs_read, NULL, NULL, &tv_timeout);
  193 + delayMs(10);
  194 +
  195 + //FD_ISSET 用于测试指定的文件描述符是否在该集合中。
  196 + //Uart_fd 是否在fsread中
  197 + if (FD_ISSET(Uart_fd, &fs_read))
  198 + {
  199 + //1、读取串口内容 ret 接收长度 getbuf 获取的字符
  200 + memset(getbuf,0,sizeof(getbuf)); //清空接收数组
  201 + ret = read(Uart_fd,getbuf,sizeof(getbuf));
  202 +
  203 + UartDeal_Recv_interface(Uart_fd, getbuf, ret);
  204 +
  205 + }
  206 + }
  207 +
  208 + free(arg);
  209 +
  210 +}
  211 +
  212 +/****************
  213 + *
  214 + *
  215 + * 发送函数
  216 + *
  217 + * ****************/
  218 +int JZsdk_Uart_UartSend(int UartPort ,unsigned char *send, int num)
  219 +{
  220 + if (UartPort == UART_4G)
  221 + {
  222 + write(Uart_4G_fd,send, num);
  223 + return 0;
  224 + }
  225 + else if (UartPort == UART_DEV_1)
  226 + {
  227 + write(Uart_DEV1_fd,send, num);
  228 + return 0;
  229 + }
  230 + else if (UartPort == UART_DEV_2)
  231 + {
  232 + write(Uart_DEV2_fd,send, num);
  233 + return 0;
  234 + }
  235 +}