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