-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCANZLG2.cpp
More file actions
430 lines (397 loc) · 13.3 KB
/
Copy pathCANZLG2.cpp
File metadata and controls
430 lines (397 loc) · 13.3 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "CANZLG2.h"
#include <cstring>
namespace ZCANBus {
CANZLG2::CANZLG2() : loopOn(false) {}
CANZLG2::~CANZLG2() {}
CANStatus CANZLG2::OpenChannel(int channel, CANRate baudRate, int type) {
void* argv[] = {&type};
return OpenChannel(channel, baudRate, 1, argv);
}
CANStatus CANZLG2::OpenChannel(int channel, CANRate baudRate, int argc,
void* argv[]) {
UINT can_index = channel;
UINT device_index = 0;
UINT device_type = 0;
if (argc > 1) {
device_index = *(UINT*)argv[1];
}
if (argc > 0) {
device_type = *(UINT*)argv[0];
}
dhandle = ZCAN_OpenDevice(device_type, device_index, 0);
if (dhandle == INVALID_DEVICE_HANDLE) {
return ERR_DEVICEOPEN;
}
std::string baudrate;
ZCAN_CHANNEL_INIT_CONFIG config;
config.can_type = 0;
config.can.acc_code = 0;
config.can.acc_mask = 0xFFFFFFFF;
config.can.reserved = 0;
config.can.filter = 1;
config.can.mode = 0;
switch (baudRate) {
case CANRate::CAN_RATE_10K:
config.can.timing0 = 0x31;
config.can.timing1 = 0x1C;
baudrate = "10000";
break;
case CANRate::CAN_RATE_20K:
config.can.timing0 = 0x18;
config.can.timing1 = 0x1C;
baudrate = "20000";
break;
case CANRate::CAN_RATE_40K:
config.can.timing0 = 0x87;
config.can.timing1 = 0xFF;
baudrate = "40000";
break;
case CANRate::CAN_RATE_50K:
config.can.timing0 = 0x09;
config.can.timing1 = 0x1C;
baudrate = "50000";
break;
case CANRate::CAN_RATE_80K:
config.can.timing0 = 0x83;
config.can.timing1 = 0xFF;
baudrate = "80000";
break;
case CANRate::CAN_RATE_100K:
config.can.timing0 = 0x04;
config.can.timing1 = 0x1C;
baudrate = "100000";
break;
case CANRate::CAN_RATE_125K:
config.can.timing0 = 0x03;
config.can.timing1 = 0x1C;
baudrate = "125000";
break;
case CANRate::CAN_RATE_250K:
config.can.timing0 = 0x01;
config.can.timing1 = 0x1C;
baudrate = "250000";
break;
case CANRate::CAN_RATE_400K:
config.can.timing0 = 0x80;
config.can.timing1 = 0xFA;
baudrate = "400000";
break;
case CANRate::CAN_RATE_500K:
config.can.timing0 = 0x00;
config.can.timing1 = 0x1C;
baudrate = "500000";
break;
case CANRate::CAN_RATE_666K:
config.can.timing0 = 0x80;
config.can.timing1 = 0xB6;
baudrate = "666000";
break;
case CANRate::CAN_RATE_800K:
config.can.timing0 = 0x00;
config.can.timing1 = 0x16;
baudrate = "800000";
break;
case CANRate::CAN_RATE_1M:
config.can.timing0 = 0x00;
config.can.timing1 = 0x14;
baudrate = "1000000";
break;
default:
return ERR_DEVICEOPEN;
break;
}
chandle = ZCAN_InitCAN(dhandle, can_index, &config);
if (INVALID_CHANNEL_HANDLE == chandle) {
ZCAN_CloseDevice(dhandle);
return ERR_DEVICEOPEN;
}
switch (device_type) {
case ZCAN_USBCAN_E_U:
case ZCAN_USBCAN_2E_U:
case ZCAN_USBCAN_4E_U:
case ZCAN_PCI5010U:
case ZCAN_PCI5020U:
case ZCAN_CANDTU: {
auto* p = GetIProperty(dhandle);
auto path = std::to_string(can_index) + "/baud_rate";
if (0 == p->SetValue(path.c_str(), baudrate.c_str())) {
ZCAN_CHANNEL_ERR_INFO errInfo;
if (STATUS_OK == ZCAN_ReadChannelErrInfo(chandle, &errInfo)) {
ZCAN_CloseDevice(dhandle);
if (0 < errInfo.error_code) {
return errInfo.error_code;
}
}
ZCAN_CloseDevice(dhandle);
return -1;
}
break;
}
default:
break;
}
if (STATUS_OK != ZCAN_ResetCAN(chandle)) {
ZCAN_CHANNEL_ERR_INFO errInfo;
if (STATUS_OK == ZCAN_ReadChannelErrInfo(chandle, &errInfo)) {
ZCAN_CloseDevice(dhandle);
if (0 < errInfo.error_code) {
return errInfo.error_code;
}
}
ZCAN_CloseDevice(dhandle);
return -1;
}
if (STATUS_OK != ZCAN_StartCAN(chandle)) {
ZCAN_CHANNEL_ERR_INFO errInfo;
if (STATUS_OK == ZCAN_ReadChannelErrInfo(chandle, &errInfo)) {
ZCAN_CloseDevice(dhandle);
if (0 < errInfo.error_code) {
return errInfo.error_code;
}
}
ZCAN_CloseDevice(dhandle);
return -1;
}
return 0;
}
void CANZLG2::ReadLoop(
std::function<void(const CANMessage* msg, CANStatus status)> callback,
uint64_t interval) {
th = new std::thread([&, callback, interval]() -> void {
loopOn = true;
CANMessage msg;
CANStatus stat;
ZCAN_Receive_Data data;
ZCAN_CHANNEL_ERR_INFO errInfo;
while (loopOn) {
stat = ZCAN_Receive(chandle, &data, 1, interval);
if (stat > 0) {
msg.timestamp = data.timestamp;
msg.length = data.frame.can_dlc;
msg.id = data.frame.can_id & 0x1FFFFFFF;
msg.type = (data.frame.can_id >> 29) & 0x07;
unsigned int type = (unsigned int)CANMSGType::STANDARD;
if (msg.type & 1) {
type |= (unsigned int)CANMSGType::ERRFRAME;
}
if (msg.type & (1 << 1)) {
type |= (unsigned int)CANMSGType::RTR;
}
if (msg.type & (1 << 2)) {
type |= (unsigned int)CANMSGType::EXTENDED;
}
msg.type = type;
memcpy(msg.msg, data.frame.data, msg.length);
callback(&msg, 0);
} else if (stat < 0) {
if (STATUS_OK == ZCAN_ReadChannelErrInfo(chandle, &errInfo)) {
if (0 < errInfo.error_code) {
callback(&msg, errInfo.error_code);
}
}
}
}
});
}
void CANZLG2::EndReadLoop() {
loopOn = false;
th->join();
delete th;
}
CANStatus CANZLG2::ReadOnce(CANMessage& msg, uint64_t timeout) {
CANStatus stat;
ZCAN_Receive_Data data;
stat = ZCAN_Receive(chandle, &data, 1, timeout);
if (stat > 0) {
msg.timestamp = data.timestamp;
msg.length = data.frame.can_dlc;
msg.id = data.frame.can_id & 0x1FFFFFFF;
msg.type = (data.frame.can_id >> 29) & 0x07;
unsigned int type = (unsigned int)CANMSGType::STANDARD;
if (msg.type & 1) {
type |= (unsigned int)CANMSGType::ERRFRAME;
}
if (msg.type & (1 << 1)) {
type |= (unsigned int)CANMSGType::RTR;
}
if (msg.type & (1 << 2)) {
type |= (unsigned int)CANMSGType::EXTENDED;
}
msg.type = type;
memcpy(msg.msg, data.frame.data, msg.length);
return 0;
} else if (stat < 0) {
ZCAN_CHANNEL_ERR_INFO errInfo;
if (STATUS_OK == ZCAN_ReadChannelErrInfo(chandle, &errInfo)) {
if (0 < errInfo.error_code) {
return errInfo.error_code;
}
}
}
return -1;
}
CANStatus CANZLG2::Write(const CANMessage& msg) {
uint8_t flag = 0;
if (msg.type & (unsigned int)CANMSGType::EXTENDED) {
flag |= 1 << 2;
}
if (msg.type & (unsigned int)CANMSGType::RTR) {
flag |= 1 << 1;
}
if (msg.type & (unsigned int)CANMSGType::ERRFRAME) {
flag |= 1;
}
ZCAN_Transmit_Data data;
data.transmit_type = 0;
data.frame.can_id = (msg.id & 0x1FFFFFFF) | ((flag & (0x07)) << 29);
data.frame.can_dlc = msg.length;
memcpy(data.frame.data, msg.msg, msg.length);
auto stat = ZCAN_Transmit(chandle, &data, 1);
if (stat > 0) {
return 0;
} else if (stat < 0) {
ZCAN_CHANNEL_ERR_INFO errInfo;
if (STATUS_OK == ZCAN_ReadChannelErrInfo(chandle, &errInfo)) {
if (0 < errInfo.error_code) {
return errInfo.error_code;
}
}
}
return -1;
};
CANStatus CANZLG2::Write(CANMessage* msg, int count) {
ZCAN_Transmit_Data* data = new ZCAN_Transmit_Data[count];
for (int i = 0; i < count; i++) {
uint8_t flag = 0;
if (msg[i].type & (unsigned int)CANMSGType::EXTENDED) {
flag |= 1 << 2;
}
if (msg[i].type & (unsigned int)CANMSGType::RTR) {
flag |= 1 << 1;
}
if (msg[i].type & (unsigned int)CANMSGType::ERRFRAME) {
flag |= 1;
}
data[i].transmit_type = 0;
data[i].frame.can_id = (msg.id & 0x1FFFFFFF) | ((flag & (0x07)) << 29);
data[i].frame.can_dlc = msg[i].length;
memcpy(data[i].frame.data, msg[i].msg, msg[i].length);
}
auto stat = ZCAN_Transmit(chandle, data, count);
delete[] data;
if (stat < 0) {
ZCAN_CHANNEL_ERR_INFO errInfo;
if (STATUS_OK == ZCAN_ReadChannelErrInfo(chandle, &errInfo)) {
if (0 < errInfo.error_code) {
return errInfo.error_code;
}
}
}
return stat - count;
}
CANStatus CANZLG2::CloseChannel() {
if (loopOn) {
EndReadLoop();
}
if (STATUS_OK != ZCAN_ResetCAN(chandle) ||
STATUS_OK != ZCAN_CloseDevice(dhandle)) {
return -1;
}
return 0;
}
CANStatus CANZLG2::FlushQueue() {
if (STATUS_OK != ZCAN_ClearBuffer(chandle)) {
ZCAN_CHANNEL_ERR_INFO errInfo;
if (STATUS_OK == ZCAN_ReadChannelErrInfo(chandle, &errInfo)) {
if (0 < errInfo.error_code) {
return errInfo.error_code;
}
}
return -1;
} else {
return 0;
}
}
std::string CANZLG2::GetErrorText(CANStatus& status) {
if (status == 0) {
return "OK";
} else if (status > 0) {
switch (status) {
case ERR_CAN_OVERFLOW:
return "CAN控制器内部FIFO溢出";
break;
case ERR_CAN_ERRALARM:
return "CAN控制器错误报警";
break;
case ERR_CAN_PASSIVE:
return "CAN控制器消极错误";
break;
case ERR_CAN_LOSE:
return "CAN控制器仲裁丢失";
break;
case ERR_CAN_BUSERR:
return "CAN控制器总线错误";
break;
case ERR_CAN_BUFFER_OVERFLOW:
return "CAN控制器内部BUFFER溢出";
break;
case ERR_DEVICEOPENED:
return "设备已经打开";
break;
case ERR_DEVICEOPEN:
return "打开设备错误";
break;
case ERR_DEVICENOTOPEN:
return "设备没有打开";
break;
case ERR_BUFFEROVERFLOW:
return "缓冲区溢出";
break;
case ERR_DEVICENOTEXIST:
return "此设备不存在";
break;
case ERR_LOADKERNELDLL:
return "装载动态库失败";
break;
case ERR_CMDFAILED:
return "执行命令失败错误码";
break;
case ERR_BUFFERCREATE:
return "内存不足";
break;
case ERR_CANETE_PORTOPENED:
return "端口已经被打开";
break;
case ERR_CANETE_INDEXUSED:
return "设备索引号已经被占用";
break;
case ERR_REF_TYPE_ID:
return "SetReference或GetReference传递的RefType不存在";
break;
case ERR_CREATE_SOCKET:
return "创建Socket失败";
break;
case ERR_OPEN_CONNECT:
return "打开Socket的连接时失败,可能设备连接已经存在";
break;
case ERR_NO_STARTUP:
return "设备没启动";
break;
case ERR_NO_CONNECTED:
return "设备无连接";
break;
case ERR_SEND_PARTIAL:
return "只发送了部分的CAN帧";
break;
case ERR_SEND_TOO_FAST:
return "数据发得太快,Socket缓冲区满了";
break;
default:
return "未知错误";
break;
}
} else {
return "未知错误";
}
}
} // namespace ZCANBus