-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcp_demo.cpp
186 lines (156 loc) · 4.05 KB
/
tcp_demo.cpp
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
// 解析后的数据可以在 data_process 分析
// 请根据实际连接方式修改main函数中对应参数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <pthread.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <math.h>
#include "data.h"
int uart_talk(int fd, int n, const char* cmd,
int nhdr, const char* hdr_str,
int nfetch, char* fetch)
{
return -1;
}
int main(int argc, char **argv)
{
int with_chk = 1; // 使能数据校验
if (argc < 8) {
printf("usage : ./lidar 雷达地址 端口 单位是毫米 数据中带有强度 分辨率[0,1,200,225,250,300,333...] 去拖点 平滑\n");
return -1;
}
char dev_ip[256]; // 雷达的网络地址
strcpy(dev_ip, argv[1]);
int tcp_port = atoi(argv[2]); // 雷达的TCP端口
int unit_is_mm = atoi(argv[3]); // 数据是毫米为单位,厘米时为0
int with_confidence = atoi(argv[4]); // 数据中带有强度
int resample = atoi(argv[5]); // 分辨率,0:原始数据,1:角度修正数据,200:0.2°,333:0.3°。。。。
int with_deshadow = atoi(argv[6]); // 去拖点,0:关闭,1:开启
int with_smooth = atoi(argv[7]); // 数据平滑, 0:关闭, 1:开启
int fd_tcp = -1;
unsigned char* buf = new unsigned char[BUF_SIZE];
int buf_len = 0;
FILE* fp_rec = NULL;// fopen("/tmp/rec.dat", "ab");
bool should_publish = false;
int fan_span = 360;
while (1)
{
if (fd_tcp < 0)
{
// open TCP port
int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0) { printf("socket TCP failed\n"); return 0; }
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr)); /* Zero out structure */
addr.sin_family = AF_INET; /* Internet address family */
addr.sin_addr.s_addr = inet_addr(dev_ip);
addr.sin_port = htons(tcp_port);
int ret = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
if (ret != 0)
{
printf("connect (%s:%d) failed", dev_ip, tcp_port);
close(sock);
sleep(15);
continue;
}
fd_tcp = sock;
printf("connect (%s:%d) ok", dev_ip, tcp_port);
}
fd_set fds;
FD_ZERO(&fds);
int fd_max = -1;
if (fd_tcp > 0)
{
FD_SET(fd_tcp, &fds);
if (fd_max < fd_tcp) fd_max = fd_tcp;
}
struct timeval to = { 1, 1 };
int ret = select(fd_max+1, &fds, NULL, NULL, &to);
if (ret == 0)
{
printf("read data timeout\n");
if (fd_tcp > 0) {
close(fd_tcp);
fd_tcp = -1;
}
continue;
}
if (ret < 0) {
printf("select error\n");
return -1;
}
int new_data = -1;
if (fd_tcp > 0 && FD_ISSET(fd_tcp, &fds))
{
int nr = recv(fd_tcp, buf+buf_len, BUF_SIZE - buf_len, 0);
if (nr < 0) {
printf("tcp error");
close(fd_tcp);
fd_tcp = -1;
continue;
}
new_data = nr;
}
if (new_data > 0)
{
buf_len += new_data;
int consume = 0;
RawData dat;
bool is_pack;
if (unit_is_mm && with_confidence)
{
is_pack = parse_data_x(buf_len, buf,
fan_span,unit_is_mm, with_confidence,
dat, consume, with_chk);
}
else {
is_pack = parse_data(buf_len, buf,
fan_span, unit_is_mm, with_confidence,
dat, consume, with_chk);
}
if (is_pack)
{
data_process(dat);
}
if (consume > 0)
{
if (!is_pack) {
FILE* fp = fopen("/tmp/bad.dat", "ab");
if (fp) {
fwrite(buf, 1, consume, fp);
fclose(fp);
}
printf("drop %d bytes: %02x %02x %02x %02x %02x %02x",
consume,
buf[0], buf[1], buf[2],
buf[3], buf[4], buf[5]);
}
for (int i=consume; i<buf_len; i++)
buf[i - consume] = buf[i];
buf_len -= consume;
}
}
}
//close(fd);
return 0;
}