Skip to content

Commit 380adaa

Browse files
author
ChenSZ
committed
zlg可以打开两个can
1 parent 80ec013 commit 380adaa

11 files changed

+79
-42
lines changed

CANBase.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#pragma once
21
#ifndef __CANBase_H
32
#define __CANBase_H
43
#include <functional>
@@ -20,7 +19,7 @@ class CANBase {
2019
* @return a error code. Generally, 0 means OK. @see CanStatus
2120
*/
2221
virtual CANStatus OpenChannel(int channel, CANRate baudRate, int type) {
23-
char* argv[]={(char*)&type};
22+
void* argv[]={&type};
2423
return OpenChannel(channel, baudRate, 1, argv);
2524
};
2625

@@ -33,7 +32,7 @@ class CANBase {
3332
* @return a error code. Generally, 0 means OK. @see CanStatus
3433
*/
3534
virtual CANStatus OpenChannel(int channel, CANRate baudRate, int argc,
36-
char* argv[]) = 0;
35+
void* argv[]) = 0;
3736

3837
/**
3938
* @brief Read CAN message continuously with async mode.

CANHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ CANStatus CANHandler::OpenChannel(int channel, CANRate baudRate, int type) {
2323
}
2424

2525
CANStatus CANHandler::OpenChannel(int channel, CANRate baudRate, int argc,
26-
char *argv[]) {
26+
void *argv[]) {
2727
return baseCan->OpenChannel(channel, baudRate, argc, argv);
2828
}
2929

CANHandler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DLLEXPORT CANHandler {
4646
* @return a error code. Generally, 0 means OK. @see CanStatus
4747
*/
4848
CANStatus OpenChannel(int channel, CANRate baudRate, int argc,
49-
char* argv[]);
49+
void* argv[]);
5050

5151
/**
5252
* @brief Read CAN message continuously with async mode.

CANKvaser.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ CANKvaser::CANKvaser() : loopOn(false) { canInitializeLibrary(); }
55
CANKvaser::~CANKvaser() {}
66

77
CANStatus CANKvaser::OpenChannel(int channel, CANRate baudRate, int type) {
8-
char* argv[] = {(char*)&type};
8+
void* argv[] = {&type};
99
return OpenChannel(channel, baudRate, 1, argv);
1010
}
1111

1212
CANStatus CANKvaser::OpenChannel(int channel, CANRate baudRate, int argc,
13-
char* argv[]) {
13+
void* argv[]) {
1414
long freq = 0;
1515
switch (baudRate) {
1616
case CANRate::CAN_RATE_10K:

CANKvaser.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CANKvaser : public CANBase {
2828
* @see <canlib.h>
2929
*/
3030
CANStatus OpenChannel(int channel, CANRate baudRate, int argc,
31-
char* argv[]) override;
31+
void* argv[]) override;
3232
void ReadLoop(
3333
std::function<void(const CANMessage* msg, CANStatus status)> callback,
3434
uint64_t interval) override;

CANPeak.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ CANPeak::CANPeak() {}
99
CANPeak::~CANPeak() {}
1010

1111
CANStatus CANPeak::OpenChannel(int channel, CANRate baudRate, int type) {
12-
char* argv[]={(char*)&type};
12+
void* argv[]={&type};
1313
return OpenChannel(channel, baudRate, 1, argv);
1414
}
1515

1616
CANStatus CANPeak::OpenChannel(int channel, CANRate baudRate, int argc,
17-
char* argv[]) {
17+
void* argv[]) {
1818
TPCANBaudrate freq = 0;
1919
switch (baudRate) {
2020
case CANRate::CAN_RATE_5K:

CANPeak.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CANPeak : public CANBase {
3232
* @see <PCANBasic.h> CAN_Initialize
3333
*/
3434
CANStatus OpenChannel(int channel, CANRate baudRate, int argc,
35-
char* argv[]) override;
35+
void* argv[]) override;
3636
void ReadLoop(
3737
std::function<void(const CANMessage* msg, CANStatus status)> callback,
3838
uint64_t interval) override;

CANZLG.cpp

+55-24
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#include "CANZLG.h"
22
#include <cstring>
33
namespace ZCANBus {
4-
CANZLG::CANZLG() : loopOn(false) {}
4+
CANZLG::CANZLG() : loopOn(false), opened(false) {}
55

66
CANZLG::~CANZLG() {}
77

88
CANStatus CANZLG::OpenChannel(int channel, CANRate baudRate, int type) {
9-
char* argv[] = {(char*)&type};
9+
void* argv[] = {&type};
1010
return OpenChannel(channel, baudRate, 1, argv);
1111
}
1212

1313
CANStatus CANZLG::OpenChannel(int channel, CANRate baudRate, int argc,
14-
char* argv[]) {
14+
void* argv[]) {
1515
can_index = channel;
1616
device_index = 0;
1717
device_type = 0;
@@ -22,7 +22,15 @@ CANStatus CANZLG::OpenChannel(int channel, CANRate baudRate, int argc,
2222
device_type = *(UINT*)argv[0];
2323
}
2424
if (STATUS_OK != VCI_OpenDevice(device_type, device_index, 0)) {
25-
return ERR_DEVICEOPEN;
25+
VCI_ERR_INFO errInfo;
26+
if (STATUS_OK ==
27+
VCI_ReadErrInfo(device_type, device_index, can_index, &errInfo)) {
28+
if (ERR_DEVICEOPENED != errInfo.ErrCode) {
29+
return errInfo.ErrCode;
30+
}
31+
} else {
32+
return ERR_DEVICEOPEN;
33+
}
2634
}
2735
VCI_INIT_CONFIG config;
2836
config.AccCode = 0;
@@ -102,33 +110,49 @@ CANStatus CANZLG::OpenChannel(int channel, CANRate baudRate, int argc,
102110
}
103111
if (STATUS_OK !=
104112
VCI_InitCAN(device_type, device_index, can_index, &config)) {
105-
VCI_CloseDevice(device_type, device_index);
106-
return ERR_DEVICEOPEN;
113+
VCI_ERR_INFO errInfo;
114+
if (STATUS_OK ==
115+
VCI_ReadErrInfo(device_type, device_index, can_index, &errInfo)) {
116+
if (openedCount[{device_type, device_index}] == 0) {
117+
VCI_CloseDevice(device_type, device_index);
118+
}
119+
return errInfo.ErrCode;
120+
}
121+
if (openedCount[{device_type, device_index}] == 0) {
122+
VCI_CloseDevice(device_type, device_index);
123+
}
124+
return -1;
107125
}
108126
if (STATUS_OK != VCI_ResetCAN(device_type, device_index, can_index)) {
109127
VCI_ERR_INFO errInfo;
110128
if (STATUS_OK ==
111129
VCI_ReadErrInfo(device_type, device_index, can_index, &errInfo)) {
112-
VCI_CloseDevice(device_type, device_index);
113-
if (0 < errInfo.ErrCode) {
114-
return errInfo.ErrCode;
130+
if (openedCount[{device_type, device_index}] == 0) {
131+
VCI_CloseDevice(device_type, device_index);
115132
}
133+
return errInfo.ErrCode;
134+
}
135+
if (openedCount[{device_type, device_index}] == 0) {
136+
VCI_CloseDevice(device_type, device_index);
116137
}
117-
VCI_CloseDevice(device_type, device_index);
118138
return -1;
119139
}
120140
if (STATUS_OK != VCI_StartCAN(device_type, device_index, can_index)) {
121141
VCI_ERR_INFO errInfo;
122142
if (STATUS_OK ==
123143
VCI_ReadErrInfo(device_type, device_index, can_index, &errInfo)) {
124-
VCI_CloseDevice(device_type, device_index);
125-
if (0 < errInfo.ErrCode) {
126-
return errInfo.ErrCode;
144+
if (openedCount[{device_type, device_index}] == 0) {
145+
VCI_CloseDevice(device_type, device_index);
127146
}
147+
return errInfo.ErrCode;
148+
}
149+
if (openedCount[{device_type, device_index}] == 0) {
150+
VCI_CloseDevice(device_type, device_index);
128151
}
129-
VCI_CloseDevice(device_type, device_index);
130152
return -1;
131153
}
154+
++openedCount[{device_type, device_index}];
155+
opened = true;
132156
return 0;
133157
}
134158

@@ -242,15 +266,15 @@ CANStatus CANZLG::Write(CANMessage* msg, int count) {
242266
data[i].SendType = 0;
243267
data[i].ID = msg[i].id;
244268
data[i].DataLen = msg[i].length;
245-
if (msg.type & (unsigned int)CANMSGType::RTR) {
246-
data.RemoteFlag = 1;
269+
if (msg[i].type & (unsigned int)CANMSGType::RTR) {
270+
data[i].RemoteFlag = 1;
247271
} else {
248-
data.RemoteFlag = 0;
272+
data[i].RemoteFlag = 0;
249273
}
250-
if (msg.type & (unsigned int)CANMSGType::EXTENDED) {
251-
data.ExternFlag = 1;
274+
if (msg[i].type & (unsigned int)CANMSGType::EXTENDED) {
275+
data[i].ExternFlag = 1;
252276
} else {
253-
data.ExternFlag = 0;
277+
data[i].ExternFlag = 0;
254278
}
255279
memcpy(data[i].Data, msg[i].msg, msg[i].length);
256280
}
@@ -273,8 +297,15 @@ CANStatus CANZLG::CloseChannel() {
273297
if (loopOn) {
274298
EndReadLoop();
275299
}
276-
if (STATUS_OK != VCI_ResetCAN(device_type, device_index, can_index) ||
277-
STATUS_OK != VCI_CloseDevice(device_type, device_index)) {
300+
auto status = VCI_ResetCAN(device_type, device_index, can_index);
301+
if (opened) {
302+
opened = false;
303+
--openedCount[{device_type, device_index}];
304+
}
305+
if (openedCount[{device_type, device_index}] == 0) {
306+
status &= VCI_CloseDevice(device_type, device_index);
307+
}
308+
if (STATUS_OK != status) {
278309
return -1;
279310
}
280311
return 0;
@@ -377,11 +408,11 @@ std::string CANZLG::GetErrorText(CANStatus& status) {
377408
// return "数据发得太快,Socket缓冲区满了";
378409
// break;
379410
default:
380-
return "未知错误";
411+
return "未知错误" + std::to_string(status);
381412
break;
382413
}
383414
} else {
384-
return "未知错误";
415+
return "未知错误" + std::to_string(status);
385416
}
386417
}
387418
} // namespace ZCANBus

CANZLG.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#ifndef __CANBase_ZLG_H
33
#define __CANBase_ZLG_H
44
#include <controlcan.h>
5+
#include <map>
56
#include <thread>
7+
#include <vector>
68
#include "CANBase.h"
79

810
namespace ZCANBus {
@@ -13,6 +15,11 @@ class CANZLG : public CANBase {
1315
UINT can_index = 0;
1416
UINT device_index = 0;
1517
UINT device_type = 0;
18+
using device_index_t = UINT;
19+
using device_type_t = UINT;
20+
static std::map<std::pair<device_type_t, device_index_t>, UINT>
21+
openedCount;
22+
bool opened;
1623

1724
public:
1825
CANZLG();
@@ -31,7 +38,7 @@ class CANZLG : public CANBase {
3138
* @see <controlcan.h>
3239
*/
3340
CANStatus OpenChannel(int channel, CANRate baudRate, int argc,
34-
char* argv[]) override;
41+
void* argv[]) override;
3542
void ReadLoop(
3643
std::function<void(const CANMessage* msg, CANStatus status)> callback,
3744
uint64_t interval) override;

CANZLG2.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ CANZLG2::CANZLG2() : loopOn(false) {}
66
CANZLG2::~CANZLG2() {}
77

88
CANStatus CANZLG2::OpenChannel(int channel, CANRate baudRate, int type) {
9-
char* argv[] = {(char*)&type};
9+
void* argv[] = {&type};
1010
return OpenChannel(channel, baudRate, 1, argv);
1111
}
1212

1313
CANStatus CANZLG2::OpenChannel(int channel, CANRate baudRate, int argc,
14-
char* argv[]) {
14+
void* argv[]) {
1515
UINT can_index = channel;
1616
UINT device_index = 0;
1717
UINT device_type = 0;
@@ -284,13 +284,13 @@ CANStatus CANZLG2::Write(CANMessage* msg, int count) {
284284
ZCAN_Transmit_Data* data = new ZCAN_Transmit_Data[count];
285285
for (int i = 0; i < count; i++) {
286286
uint8_t flag = 0;
287-
if (msg.type & (unsigned int)CANMSGType::EXTENDED) {
287+
if (msg[i].type & (unsigned int)CANMSGType::EXTENDED) {
288288
flag |= 1 << 2;
289289
}
290-
if (msg.type & (unsigned int)CANMSGType::RTR) {
290+
if (msg[i].type & (unsigned int)CANMSGType::RTR) {
291291
flag |= 1 << 1;
292292
}
293-
if (msg.type & (unsigned int)CANMSGType::ERRFRAME) {
293+
if (msg[i].type & (unsigned int)CANMSGType::ERRFRAME) {
294294
flag |= 1;
295295
}
296296
data[i].transmit_type = 0;

CANZLG2.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CANZLG2 : public CANBase {
2929
* @see <zlgcan.h>
3030
*/
3131
CANStatus OpenChannel(int channel, CANRate baudRate, int argc,
32-
char* argv[]) override;
32+
void* argv[]) override;
3333
void ReadLoop(
3434
std::function<void(const CANMessage* msg, CANStatus status)> callback,
3535
uint64_t interval) override;

0 commit comments

Comments
 (0)