Skip to content

Commit 06033a8

Browse files
author
ChenSZ
committed
unify canmessagetype definition
1 parent 4684194 commit 06033a8

File tree

5 files changed

+176
-21
lines changed

5 files changed

+176
-21
lines changed

.vscode/settings.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"files.associations": {
3+
"*.m": "matlab",
34
"__locale": "cpp",
45
"__string": "cpp",
56
"string": "cpp",
@@ -18,6 +19,10 @@
1819
"tuple": "cpp",
1920
"type_traits": "cpp",
2021
"utility": "cpp",
21-
"ios": "cpp"
22+
"ios": "cpp",
23+
"iterator": "cpp",
24+
"array": "cpp",
25+
"istream": "cpp",
26+
"ostream": "cpp"
2227
}
2328
}

CANKvaser.cpp

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

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

1212
CANStatus CANKvaser::OpenChannel(int channel, CANRate baudRate, int argc,
@@ -88,6 +88,32 @@ void CANKvaser::ReadLoop(
8888
while (loopOn) {
8989
stat = canReadWait(handle, &msg.id, msg.msg, &msg.length, &msg.type,
9090
&msg.timestamp, interval);
91+
unsigned int type = (unsigned int)CANMSGType::STANDARD;
92+
if (msg.type & canMSG_EXT) {
93+
type |= (unsigned int)CANMSGType::EXTENDED;
94+
msg.type &= ~canMSG_EXT;
95+
}
96+
if (msg.type & canMSG_RTR) {
97+
type |= (unsigned int)CANMSGType::RTR;
98+
msg.type &= ~canMSG_RTR;
99+
}
100+
if (msg.type & canMSG_ERROR_FRAME) {
101+
type |= (unsigned int)CANMSGType::ERRFRAME;
102+
msg.type &= ~canMSG_ERROR_FRAME;
103+
}
104+
if (msg.type & canFDMSG_FDF) {
105+
type |= (unsigned int)CANMSGType::FD;
106+
msg.type &= ~canFDMSG_FDF;
107+
}
108+
if (msg.type & canFDMSG_BRS) {
109+
type |= (unsigned int)CANMSGType::BRS;
110+
msg.type &= ~canFDMSG_BRS;
111+
}
112+
if (msg.type & canFDMSG_ESI) {
113+
type |= (unsigned int)CANMSGType::ESI;
114+
msg.type &= ~canFDMSG_ESI;
115+
}
116+
msg.type = type;
91117
callback(&msg, stat);
92118
}
93119
});
@@ -100,21 +126,64 @@ void CANKvaser::EndReadLoop() {
100126
}
101127

102128
CANStatus CANKvaser::ReadOnce(CANMessage& msg, uint64_t timeout) {
103-
return canReadWait(handle, &msg.id, msg.msg, &msg.length, &msg.type,
104-
&msg.timestamp, timeout);
129+
auto&& status = canReadWait(handle, &msg.id, msg.msg, &msg.length,
130+
&msg.type, &msg.timestamp, timeout);
131+
unsigned int type = (unsigned int)CANMSGType::STANDARD;
132+
if (msg.type & canMSG_EXT) {
133+
type |= (unsigned int)CANMSGType::EXTENDED;
134+
msg.type &= ~canMSG_EXT;
135+
}
136+
if (msg.type & canMSG_RTR) {
137+
type |= (unsigned int)CANMSGType::RTR;
138+
msg.type &= ~canMSG_RTR;
139+
}
140+
if (msg.type & canMSG_ERROR_FRAME) {
141+
type |= (unsigned int)CANMSGType::ERRFRAME;
142+
msg.type &= ~canMSG_ERROR_FRAME;
143+
}
144+
if (msg.type & canFDMSG_FDF) {
145+
type |= (unsigned int)CANMSGType::FD;
146+
msg.type &= ~canFDMSG_FDF;
147+
}
148+
if (msg.type & canFDMSG_BRS) {
149+
type |= (unsigned int)CANMSGType::BRS;
150+
msg.type &= ~canFDMSG_BRS;
151+
}
152+
if (msg.type & canFDMSG_ESI) {
153+
type |= (unsigned int)CANMSGType::ESI;
154+
msg.type &= ~canFDMSG_ESI;
155+
}
156+
msg.type = type;
157+
return status;
105158
}
106159

107160
CANStatus CANKvaser::Write(const CANMessage& msg) {
108-
return canWriteWait(handle, msg.id, (void*)msg.msg, msg.length, msg.type,
109-
0xFF);
110-
;
161+
unsigned int flag = (unsigned int)CANMSGType::STANDARD;
162+
if (msg.type & (unsigned int)CANMSGType::EXTENDED) {
163+
flag |= canMSG_EXT;
164+
}
165+
if (msg.type & (unsigned int)CANMSGType::RTR) {
166+
flag |= canMSG_RTR;
167+
}
168+
if (msg.type & (unsigned int)CANMSGType::ERRFRAME) {
169+
flag |= canMSG_ERROR_FRAME;
170+
}
171+
if (msg.type & (unsigned int)CANMSGType::FD) {
172+
flag |= canFDMSG_FDF;
173+
}
174+
if (msg.type & (unsigned int)CANMSGType::BRS) {
175+
flag |= canFDMSG_BRS;
176+
}
177+
if (msg.type & (unsigned int)CANMSGType::ESI) {
178+
flag |= canFDMSG_ESI;
179+
}
180+
return canWriteWait(handle, msg.id, (void*)msg.msg, msg.length, flag, 0xFF);
111181
};
112182

113183
CANStatus CANKvaser::Write(CANMessage* msg, int count) {
114184
CANStatus status = 0;
115185
for (int i = 0; i < count; ++i) {
116-
status |= canWriteWait(handle, msg[i].id, msg[i].msg, msg[i].length,
117-
msg[i].type, 0xFF);
186+
status |= Write(msg[i]);
118187
}
119188
return status;
120189
}

CANType.h

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
#define __CAN_TYPE_H
33
#include <cstdint>
44
namespace ZCANBus {
5+
6+
enum class CANMSGType : unsigned int {
7+
STANDARD = 0,
8+
RTR = 1,
9+
EXTENDED = 1 << 1,
10+
FD = 1 << 2,
11+
BRS = 1 << 3,
12+
ESI = 1 << 4,
13+
ERRFRAME = 1 << 6,
14+
UNKNOWN = 1 << 7
15+
};
16+
517
/**
618
* The struct contains CAN message, CAN ID, message length, message type and
719
* timestamp

CANZLG.cpp

+34-6
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ void CANZLG::ReadLoop(
148148
msg.timestamp = data.TimeStamp;
149149
msg.length = data.DataLen;
150150
msg.id = data.ID;
151-
msg.type = data.ExternFlag;
151+
msg.type = (unsigned int)CANMSGType::STANDARD;
152+
if (data.ExternFlag) {
153+
msg.type |= (unsigned int)CANMSGType::EXTENDED;
154+
}
155+
if (data.RemoteFlag) {
156+
msg.type |= (unsigned int)CANMSGType::RTR;
157+
}
152158
memcpy(msg.msg, data.Data, msg.length);
153159
callback(&msg, 0);
154160
} else if (stat < 0) {
@@ -177,7 +183,13 @@ CANStatus CANZLG::ReadOnce(CANMessage& msg, uint64_t timeout) {
177183
msg.timestamp = data.TimeStamp;
178184
msg.length = data.DataLen;
179185
msg.id = data.ID;
180-
msg.type = data.ExternFlag;
186+
msg.type = (unsigned int)CANMSGType::STANDARD;
187+
if (data.ExternFlag) {
188+
msg.type |= (unsigned int)CANMSGType::EXTENDED;
189+
}
190+
if (data.RemoteFlag) {
191+
msg.type |= (unsigned int)CANMSGType::RTR;
192+
}
181193
memcpy(msg.msg, data.Data, msg.length);
182194
return 0;
183195
} else if (stat < 0) {
@@ -197,8 +209,16 @@ CANStatus CANZLG::Write(const CANMessage& msg) {
197209
data.SendType = 0;
198210
data.ID = msg.id;
199211
data.DataLen = msg.length;
200-
data.RemoteFlag = 0;
201-
data.ExternFlag = msg.type;
212+
if (msg.type & (unsigned int)CANMSGType::RTR) {
213+
data.RemoteFlag = 1;
214+
} else {
215+
data.RemoteFlag = 0;
216+
}
217+
if (msg.type & (unsigned int)CANMSGType::EXTENDED) {
218+
data.ExternFlag = 1;
219+
} else {
220+
data.ExternFlag = 0;
221+
}
202222
memcpy(data.Data, msg.msg, msg.length);
203223
CANStatus stat =
204224
VCI_Transmit(device_type, device_index, can_index, &data, 1);
@@ -222,8 +242,16 @@ CANStatus CANZLG::Write(CANMessage* msg, int count) {
222242
data[i].SendType = 0;
223243
data[i].ID = msg[i].id;
224244
data[i].DataLen = msg[i].length;
225-
data[i].RemoteFlag = 0;
226-
data[i].ExternFlag = msg[i].type;
245+
if (msg.type & (unsigned int)CANMSGType::RTR) {
246+
data.RemoteFlag = 1;
247+
} else {
248+
data.RemoteFlag = 0;
249+
}
250+
if (msg.type & (unsigned int)CANMSGType::EXTENDED) {
251+
data.ExternFlag = 1;
252+
} else {
253+
data.ExternFlag = 0;
254+
}
227255
memcpy(data[i].Data, msg[i].msg, msg[i].length);
228256
}
229257
CANStatus stat =

CANZLG2.cpp

+46-5
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,18 @@ void CANZLG2::ReadLoop(
186186
msg.timestamp = data.timestamp;
187187
msg.length = data.frame.can_dlc;
188188
msg.id = data.frame.can_id & 0x1FFFFFFF;
189-
msg.type = (data.frame.can_id >> 29) & 0x7F;
189+
msg.type = (data.frame.can_id >> 29) & 0x07;
190+
unsigned int type = (unsigned int)CANMSGType::STANDARD;
191+
if (msg.type & 1) {
192+
type |= (unsigned int)CANMSGType::ERRFRAME;
193+
}
194+
if (msg.type & (1 << 1)) {
195+
type |= (unsigned int)CANMSGType::RTR;
196+
}
197+
if (msg.type & (1 << 2)) {
198+
type |= (unsigned int)CANMSGType::EXTENDED;
199+
}
200+
msg.type = type;
190201
memcpy(msg.msg, data.frame.data, msg.length);
191202
callback(&msg, 0);
192203
} else if (stat < 0) {
@@ -214,7 +225,18 @@ CANStatus CANZLG2::ReadOnce(CANMessage& msg, uint64_t timeout) {
214225
msg.timestamp = data.timestamp;
215226
msg.length = data.frame.can_dlc;
216227
msg.id = data.frame.can_id & 0x1FFFFFFF;
217-
msg.type = (data.frame.can_id >> 29) & 0x7F;
228+
msg.type = (data.frame.can_id >> 29) & 0x07;
229+
unsigned int type = (unsigned int)CANMSGType::STANDARD;
230+
if (msg.type & 1) {
231+
type |= (unsigned int)CANMSGType::ERRFRAME;
232+
}
233+
if (msg.type & (1 << 1)) {
234+
type |= (unsigned int)CANMSGType::RTR;
235+
}
236+
if (msg.type & (1 << 2)) {
237+
type |= (unsigned int)CANMSGType::EXTENDED;
238+
}
239+
msg.type = type;
218240
memcpy(msg.msg, data.frame.data, msg.length);
219241
return 0;
220242
} else if (stat < 0) {
@@ -229,9 +251,19 @@ CANStatus CANZLG2::ReadOnce(CANMessage& msg, uint64_t timeout) {
229251
}
230252

231253
CANStatus CANZLG2::Write(const CANMessage& msg) {
254+
uint8_t flag = 0;
255+
if (msg.type & (unsigned int)CANMSGType::EXTENDED) {
256+
flag |= 1 << 2;
257+
}
258+
if (msg.type & (unsigned int)CANMSGType::RTR) {
259+
flag |= 1 << 1;
260+
}
261+
if (msg.type & (unsigned int)CANMSGType::ERRFRAME) {
262+
flag |= 1;
263+
}
232264
ZCAN_Transmit_Data data;
233265
data.transmit_type = 0;
234-
data.frame.can_id = (msg.id & 0x1FFFFFFF) | ((msg.type & (0x7F)) << 29);
266+
data.frame.can_id = (msg.id & 0x1FFFFFFF) | ((flag & (0x07)) << 29);
235267
data.frame.can_dlc = msg.length;
236268
memcpy(data.frame.data, msg.msg, msg.length);
237269
auto stat = ZCAN_Transmit(chandle, &data, 1);
@@ -251,9 +283,18 @@ CANStatus CANZLG2::Write(const CANMessage& msg) {
251283
CANStatus CANZLG2::Write(CANMessage* msg, int count) {
252284
ZCAN_Transmit_Data* data = new ZCAN_Transmit_Data[count];
253285
for (int i = 0; i < count; i++) {
286+
uint8_t flag = 0;
287+
if (msg.type & (unsigned int)CANMSGType::EXTENDED) {
288+
flag |= 1 << 2;
289+
}
290+
if (msg.type & (unsigned int)CANMSGType::RTR) {
291+
flag |= 1 << 1;
292+
}
293+
if (msg.type & (unsigned int)CANMSGType::ERRFRAME) {
294+
flag |= 1;
295+
}
254296
data[i].transmit_type = 0;
255-
data[i].frame.can_id =
256-
(msg[i].id & 0x1FFFFFFF) | ((msg[i].type & (0x7F)) << 29);
297+
data[i].frame.can_id = (msg.id & 0x1FFFFFFF) | ((flag & (0x07)) << 29);
257298
data[i].frame.can_dlc = msg[i].length;
258299
memcpy(data[i].frame.data, msg[i].msg, msg[i].length);
259300
}

0 commit comments

Comments
 (0)