Skip to content

Commit d180eb0

Browse files
author
ChenSZ
committed
CANMessage位数统一
1 parent 380adaa commit d180eb0

File tree

3 files changed

+141
-95
lines changed

3 files changed

+141
-95
lines changed

CANKvaser.cpp

+104-69
Original file line numberDiff line numberDiff line change
@@ -86,34 +86,49 @@ void CANKvaser::ReadLoop(
8686
CANMessage msg;
8787
CANStatus stat;
8888
while (loopOn) {
89-
stat = canReadWait(handle, &msg.id, msg.msg, &msg.length, &msg.type,
90-
&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;
89+
long id;
90+
unsigned int length;
91+
unsigned int flag;
92+
unsigned long time;
93+
stat = canReadWait(handle, &id, msg.msg, &length, &flag, &time,
94+
interval);
95+
msg.id = (unsigned long)id;
96+
msg.length = length;
97+
msg.timestamp = time;
98+
uint32_t type = (uint32_t)CANMSGType::STANDARD;
99+
if (flag & canMSG_EXT) {
100+
type |= (uint32_t)CANMSGType::EXTENDED;
101+
flag &= ~canMSG_EXT;
95102
}
96-
if (msg.type & canMSG_RTR) {
97-
type |= (unsigned int)CANMSGType::RTR;
98-
msg.type &= ~canMSG_RTR;
103+
if (flag & canMSG_RTR) {
104+
type |= (uint32_t)CANMSGType::RTR;
105+
flag &= ~canMSG_RTR;
99106
}
100-
if (msg.type & canMSG_ERROR_FRAME) {
101-
type |= (unsigned int)CANMSGType::ERRFRAME;
102-
msg.type &= ~canMSG_ERROR_FRAME;
107+
if (flag & canMSG_ERROR_FRAME) {
108+
type |= (uint32_t)CANMSGType::ERRFRAME;
109+
flag &= ~canMSG_ERROR_FRAME;
103110
}
104-
if (msg.type & canFDMSG_FDF) {
105-
type |= (unsigned int)CANMSGType::FD;
106-
msg.type &= ~canFDMSG_FDF;
111+
if (flag & canFDMSG_FDF) {
112+
type |= (uint32_t)CANMSGType::FD;
113+
flag &= ~canFDMSG_FDF;
107114
}
108-
if (msg.type & canFDMSG_BRS) {
109-
type |= (unsigned int)CANMSGType::BRS;
110-
msg.type &= ~canFDMSG_BRS;
115+
if (flag & canFDMSG_BRS) {
116+
type |= (uint32_t)CANMSGType::BRS;
117+
flag &= ~canFDMSG_BRS;
111118
}
112-
if (msg.type & canFDMSG_ESI) {
113-
type |= (unsigned int)CANMSGType::ESI;
114-
msg.type &= ~canFDMSG_ESI;
119+
if (flag & canFDMSG_ESI) {
120+
type |= (uint32_t)CANMSGType::ESI;
121+
flag &= ~canFDMSG_ESI;
122+
}
123+
if (flag & canMSG_STD) {
124+
flag &= ~canMSG_STD;
125+
}
126+
if (flag) {
127+
msg.type =
128+
((uint32_t)CANMSGType::HARDWAREDEF) | ((uint32_t)flag);
129+
} else {
130+
msg.type = type;
115131
}
116-
msg.type = type;
117132
callback(&msg, stat);
118133
}
119134
});
@@ -126,58 +141,78 @@ void CANKvaser::EndReadLoop() {
126141
}
127142

128143
CANStatus CANKvaser::ReadOnce(CANMessage& msg, uint64_t 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;
144+
long id;
145+
unsigned int length;
146+
unsigned int flag;
147+
unsigned long time;
148+
auto&& status =
149+
canReadWait(handle, &id, msg.msg, &length, &flag, &time, timeout);
150+
msg.id = (unsigned long)id;
151+
msg.length = length;
152+
msg.timestamp = time;
153+
uint32_t type = (uint32_t)CANMSGType::STANDARD;
154+
if (flag & canMSG_EXT) {
155+
type |= (uint32_t)CANMSGType::EXTENDED;
156+
flag &= ~canMSG_EXT;
157+
}
158+
if (flag & canMSG_RTR) {
159+
type |= (uint32_t)CANMSGType::RTR;
160+
flag &= ~canMSG_RTR;
161+
}
162+
if (flag & canMSG_ERROR_FRAME) {
163+
type |= (uint32_t)CANMSGType::ERRFRAME;
164+
flag &= ~canMSG_ERROR_FRAME;
165+
}
166+
if (flag & canFDMSG_FDF) {
167+
type |= (uint32_t)CANMSGType::FD;
168+
flag &= ~canFDMSG_FDF;
169+
}
170+
if (flag & canFDMSG_BRS) {
171+
type |= (uint32_t)CANMSGType::BRS;
172+
flag &= ~canFDMSG_BRS;
173+
}
174+
if (flag & canFDMSG_ESI) {
175+
type |= (uint32_t)CANMSGType::ESI;
176+
flag &= ~canFDMSG_ESI;
177+
}
178+
if (flag & canMSG_STD) {
179+
flag &= ~canMSG_STD;
180+
}
181+
if (flag) {
182+
msg.type = ((uint32_t)CANMSGType::HARDWAREDEF) | ((uint32_t)flag);
183+
} else {
184+
msg.type = type;
185+
}
157186
return status;
158187
}
159188

160189
CANStatus CANKvaser::Write(const CANMessage& msg) {
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;
190+
uint32_t flag = canMSG_STD;
191+
if (msg.type & (uint32_t)CANMSGType::HARDWAREDEF) {
192+
flag = msg.type & (~(uint32_t)CANMSGType::HARDWAREDEF);
193+
} else {
194+
if ((uint32_t)msg.type & (uint32_t)CANMSGType::EXTENDED) {
195+
flag |= canMSG_EXT;
196+
flag &= ~canMSG_STD;
197+
}
198+
if ((uint32_t)msg.type & (uint32_t)CANMSGType::RTR) {
199+
flag |= canMSG_RTR;
200+
}
201+
if ((uint32_t)msg.type & (uint32_t)CANMSGType::ERRFRAME) {
202+
flag |= canMSG_ERROR_FRAME;
203+
}
204+
if ((uint32_t)msg.type & (uint32_t)CANMSGType::FD) {
205+
flag |= canFDMSG_FDF;
206+
}
207+
if ((uint32_t)msg.type & (uint32_t)CANMSGType::BRS) {
208+
flag |= canFDMSG_BRS;
209+
}
210+
if ((uint32_t)msg.type & (uint32_t)CANMSGType::ESI) {
211+
flag |= canFDMSG_ESI;
212+
}
179213
}
180-
return canWriteWait(handle, msg.id, (void*)msg.msg, msg.length, flag, 0xFF);
214+
return canWriteWait(handle, (int32_t)msg.id, (void*)msg.msg, msg.length,
215+
flag, 0xFF);
181216
};
182217

183218
CANStatus CANKvaser::Write(CANMessage* msg, int count) {

CANPeak.cpp

+30-19
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ CANPeak::CANPeak() {}
99
CANPeak::~CANPeak() {}
1010

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

1616
CANStatus CANPeak::OpenChannel(int channel, CANRate baudRate, int argc,
@@ -106,17 +106,18 @@ void CANPeak::ReadLoop(
106106
while (loopOn) {
107107
#ifdef _WIN32
108108
if (WaitForSingleObject(evRecv, interval) == WAIT_OBJECT_0)
109-
#elif __linux__ || __APPLE__ || __unix__
109+
#else
110+
// #elif __linux__ || __APPLE__ || __unix__
110111
struct timeval time;
111112
time.tv_sec = 0;
112113
time.tv_usec = interval * 1000;
113114
FD_ZERO(&fds);
114115
FD_SET(evRecv, &fds);
115116
if (select(evRecv + 1, &fds, NULL, NULL, &time) > 0 &&
116117
FD_ISSET(evRecv, &fds))
117-
#else
118-
#warning Unsupported OS
119-
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
118+
// #else
119+
// #warning Unsupported OS
120+
// std::this_thread::sleep_for(std::chrono::milliseconds(interval));
120121
#endif
121122
{
122123
TPCANMsg buf;
@@ -128,7 +129,12 @@ void CANPeak::ReadLoop(
128129
loopOn) {
129130
msg.id = buf.ID;
130131
msg.length = buf.LEN;
131-
msg.type = buf.MSGTYPE;
132+
if (buf.MSGTYPE & PCAN_MESSAGE_STATUS) {
133+
msg.type = (uint32_t)buf.MSGTYPE &
134+
(uint32_t)CANMSGType::HARDWAREDEF;
135+
} else {
136+
msg.type = buf.MSGTYPE;
137+
}
132138
msg.timestamp = timeStamp.millis;
133139
memcpy(msg.msg, buf.DATA, msg.length);
134140
callback(&msg, status);
@@ -151,7 +157,8 @@ CANStatus CANPeak::ReadOnce(CANMessage& msg, uint64_t timeout) {
151157
HANDLE evRecv = CreateEvent(NULL, FALSE, FALSE, NULL);
152158
stat = CAN_SetValue(channel, PCAN_RECEIVE_EVENT, &evRecv, sizeof(evRecv));
153159
if (WaitForSingleObject(evRecv, interval) == WAIT_OBJECT_0)
154-
#elif __linux__ || __APPLE__ || __unix__
160+
#else
161+
// #elif __linux__ || __APPLE__ || __unix__
155162
int evRecv = 0;
156163
stat = CAN_GetValue(channel, PCAN_RECEIVE_EVENT, &evRecv, sizeof(evRecv));
157164
fd_set fds;
@@ -163,9 +170,9 @@ CANStatus CANPeak::ReadOnce(CANMessage& msg, uint64_t timeout) {
163170
// std::this_thread::sleep_for(std::chrono::milliseconds(interval));
164171
if (select(evRecv + 1, &fds, NULL, NULL, &time) > 0 &&
165172
FD_ISSET(evRecv, &fds))
166-
#else
167-
#warning Unsupported OS
168-
std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
173+
// #else
174+
// #warning Unsupported OS
175+
// std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
169176
#endif
170177
{
171178
TPCANMsg buf;
@@ -174,7 +181,12 @@ CANStatus CANPeak::ReadOnce(CANMessage& msg, uint64_t timeout) {
174181
if (stat == PCAN_ERROR_OK) {
175182
msg.id = buf.ID;
176183
msg.length = buf.LEN;
177-
msg.type = buf.MSGTYPE;
184+
if (buf.MSGTYPE & PCAN_MESSAGE_STATUS) {
185+
msg.type =
186+
(uint32_t)buf.MSGTYPE & (uint32_t)CANMSGType::HARDWAREDEF;
187+
} else {
188+
msg.type = buf.MSGTYPE;
189+
}
178190
msg.timestamp = timeStamp.millis;
179191
memcpy(msg.msg, buf.DATA, msg.length);
180192
}
@@ -186,20 +198,19 @@ CANStatus CANPeak::Write(const CANMessage& msg) {
186198
TPCANMsg message;
187199
message.ID = msg.id;
188200
message.LEN = msg.length;
189-
message.MSGTYPE = msg.type;
201+
if (msg.type & (uint32_t)CANMSGType::HARDWAREDEF) {
202+
message.MSGTYPE = msg.type & (~(uint32_t)CANMSGType::HARDWAREDEF);
203+
} else {
204+
message.MSGTYPE = msg.type;
205+
}
190206
memcpy(message.DATA, msg.msg, msg.length);
191207
return CAN_Write(channel, &message);
192208
}
193209

194210
CANStatus CANPeak::Write(CANMessage* msg, int count) {
195211
CANStatus status = 0;
196212
for (int i = 0; i < count; ++i) {
197-
TPCANMsg message;
198-
message.ID = msg[i].id;
199-
message.LEN = msg[i].length;
200-
message.MSGTYPE = msg[i].type;
201-
memcpy(message.DATA, msg[i].msg, msg[i].length);
202-
status |= CAN_Write(channel, &message);
213+
status |= Write(msg[i]);
203214
}
204215
return status;
205216
}

CANType.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
#include <cstdint>
44
namespace ZCANBus {
55

6-
enum class CANMSGType : unsigned int {
6+
enum class CANMSGType : uint32_t {
77
STANDARD = 0,
88
RTR = 1,
99
EXTENDED = 1 << 1,
1010
FD = 1 << 2,
1111
BRS = 1 << 3,
1212
ESI = 1 << 4,
1313
ERRFRAME = 1 << 6,
14-
UNKNOWN = 1 << 7
14+
HARDWAREDEF = 1U << 31
1515
};
1616

1717
/**
1818
* The struct contains CAN message, CAN ID, message length, message type and
1919
* timestamp
2020
*/
2121
typedef struct {
22-
unsigned long timestamp;
23-
long id;
24-
unsigned int length;
25-
unsigned int type;
26-
unsigned char msg[8];
22+
uint64_t timestamp;
23+
uint32_t id;
24+
uint32_t type;
25+
uint8_t length;
26+
uint8_t msg[8];
2727
} CANMessage;
2828

2929
/**

0 commit comments

Comments
 (0)