-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaninterface.cpp
More file actions
374 lines (313 loc) · 8.7 KB
/
caninterface.cpp
File metadata and controls
374 lines (313 loc) · 8.7 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
#include "caninterface.h"
CanInterface* CanInterface::m_instance = NULL;
u_int16_t CanInterface::m_canSpeed = 125;
const u_int32_t CanInterface::m_index = 0;
bool CanInterface::m_canDeviceOnline = false;
bool CanInterface::m_canOnline = false;
const QByteArray CanInterface::m_libPath = "../libmhstcan.so";
CanInterface::CanInterface(QObject *parent) :
QObject(parent)
{
}
bool CanInterface::startCanInterface()
{
bool success = false;
if (loadCanDriver() == false)
{
// Fehlerbehandlung
return success = false;
}
// Initialisiere Treiber
if (initCanDriver() == false)
{
// Fehlerbehandlung
return success = false;
}
// Events setzen und freischalten
initEvents();
// Setze CAN-Speed
CanSetSpeed(m_index, m_canSpeed);
// Setze AutoConnect
CanSetOptions("AutoConnect=1");
// Schnittstelle PC <-> Tiny-Can öffnen
if (openCanDevice() == false)
{
// Fehlerbehandlung
return success = false;
}
// CAN-Bus starten und initialisieren
if (startCanBus() == false)
{
// Fehlerbehandlung
return success = false;
}
return success = true;
}
bool CanInterface::getCanOnline()
{
return m_canOnline;
}
int CanInterface::getCanSpeed()
{
return m_canSpeed;
}
bool CanInterface::startCanBus()
{
int32_t ret = 0;
bool success = false;
ret = CanSetMode(m_index, OP_CAN_START, CAN_CMD_ALL_CLEAR);
if (ret < 0)
{
// Setze Fehlermeldung ab
m_canOnline = false;
success = false;
}
else
{
// _canOnline nur setzen, wenn TinyCan angeschlossen
if (m_canDeviceOnline)
{
m_canOnline = true;
}
success = true;
}
return success;
}
bool CanInterface::resetCanBus()
{
//* Beschreibung *
//* Resetet den CAN-Bus und löscht alle Fehlerflags
int32_t ret = 0;
ret = CanSetMode(m_index, OP_CAN_RESET, CAN_CMD_ALL_CLEAR);
if (ret < 0)
{
m_canOnline = false;
return false;
}
else
{
// _canOnline nur setzen, wenn TinyCan angeschlossen
if (m_canDeviceOnline)
{
m_canOnline = true;
}
return true;
}
}
bool CanInterface::stopCanBus()
{
//* Beschreibung *
//* Stoppt den CanBus
int32_t ret = 0;
ret = CanSetMode(m_index, OP_CAN_STOP, CAN_CMD_ALL_CLEAR);
if (ret < 0)
{
m_canOnline = false;
return false;
}
else
{
// _canOnline nur setzen, wenn TinyCan angeschlossen
if (m_canDeviceOnline)
{
m_canOnline = false;
}
return true;
}
}
bool CanInterface::loadCanDriver()
{
//* Beschreibung *
//* Ladet den Tiny-Can Treiber
//* Sie muss nur einmal ausgeführt werden.
int32_t ret = 0;
bool success = false;
// Bei compile für RasPi muss eine andere Lib verwendet werden.
// Define für RASPICONST geschieht in .pro Datei
// Methode ladet die Lib und initialisiert den Treiber.
ret = LoadDriver(m_libPath.data());
// Fehlerbehandlung
if (ret < 0)
{
// Hole Fehlerbeschreibung und setzte Fehlermeldung ab
success = false;
}
else
{
success = true;
}
return success;
}
bool CanInterface::initCanDriver()
{
//* Beschreibung *
//* Initialisiert den CAN-Treiber
int32_t ret = 0;
bool success = false;
ret = CanInitDriver(NULL);
// Fehlerbehandlung
if (ret < 0)
{
// Hole Fehlerbeschreibung und setzte Fehlermeldung ab
success = false;
}
else
{
success = true;
}
return success;
}
void CanInterface::initEvents()
{
//* Beschreibung *
//* Setzt die Event-Funktionen und schaltet die Events frei.
// Event Funktionen setzen
CanSetRxEventCallback(&canRxEvent);
// Alle Events freigeben
CanSetEvents(EVENT_ENABLE_ALL);
}
bool CanInterface::openCanDevice()
{
//* Beschreibung *
//* Öffnet das TinyCanDevice
int32_t ret = 0;
bool success = false;
ret = CanDeviceOpen(m_index, NULL);
if (ret < 0)
{
// Setze Fehlermeldung ab
success = false;
}
else
{
m_canDeviceOnline = true;
success = true;
}
return success;
}
bool CanInterface::sendMessage(quint32 txIdent, QByteArray txData)
{
//* Beschreibung *
//* Sendet die übergebene Nachricht mit dem übergebenen Identifier per CAN raus.
//* Dabei wird nur das Basis-Frame-Format erlaubt. Dies kann jedoch über
//* das Attribut '_enableEFF' jedoch entsperrt werden.
bool success = false;
struct TCanMsg message;
message.MsgFlags = 0L; // Setze Flags zurück
// // Bei Extended-Frame-Format
// if (m_enableEFF)
// {
// message.Flags.Flag.EFF = 1; // Setze EFF-Flag
// }
// Identifier darf bei Basis-Frame-Format nur 11 Bit haben
if (txIdent < 2048 /*&& !m_enableEFF*/)
{
message.Id = (u_int32_t)txIdent;
}
// // Identifier darf bei Extended-Frame-Format nur 29 Bit haben
// else if (txIdent < 536870912)
// {
// message.Id = (u_int32_t)txIdent;
// }
// // Error: Identifier zu groß
// else
// {
// return success = false;
// }
// Schreibe Größe der Daten, wenn es max. 8 Byte sind
if (txData.size() <= 8)
{
message.MsgLen = txData.size();
}
// Daten größer als 8 Byte -> return
else
{
return success = false;
}
// Drehe alle Bytes, damit Wertigekeit zum Versenden stimmt
QByteArray txDataMirrored;
for (int i = 0; i < txData.size(); i++)
{
txDataMirrored.append(txData.at(txData.size()-1 -i));
}
// Kopieren der Daten in SendeTelegramm
memcpy(message.MsgData, txDataMirrored.constData(), txDataMirrored.size());
// Wenn CAN Online -> Nachricht versenden
if (m_canOnline)
{
int32_t ret = 0;
ret = CanTransmit(m_index, &message, 1);
// Fehlerbehandlung
if (ret < 0)
{
// Hole Fehlerbeschreibung und setzte Fehlermeldung ab
success = false;
}
else
{
success = true;
}
}
// CAN Offline -> Fehlermeldung
else
{
}
return success;
}
void CanInterface::canRxEvent(u_int32_t index, TCanMsg *msg, int32_t count)
{
//* Beschreibung *
//* Callback-Methode wird beim Empfang einer Nachricht aufgerufen.
//* Dabei werden alle eingegangenen Nachrichten ausgelesen und
//* es wird ein Signal emitiert, welches mit der "Empfangsmethode"
//* des CanParsers verbunden wird.
//*
//* Folgende Nachrichten werden verworfen:
//* - Leere Nachrichten
//* - Extended-Frame-Format Nachrichten
//*
//* Damit die while-Schleife nicht unendlich lange durchlaufen werden kann,
//* wird die Variable 'watchVar' bei jeder Schleifen-Iteration erhöht.
//* Beim Überschreiten von 'watchVarMax' wird die Schleife verlassen!
struct TCanMsg message;
quint32 rxIdent = 0;
QByteArray rxData = 0;
u_int32_t i;
unsigned int watchVar = 0;
unsigned int watchVarMax = 1500;
// Lese solang Nachichten, bis keine mehr vorhanden
while (CanReceive(0, &message, 1) > 0 && watchVar < watchVarMax)
{
// Erhöhe watchVar, zum Sicherstellen, dass while nicht unendlich lange durchlaufen wird
watchVar++;
// SenderID holen
rxIdent = (quint32)message.Id;
// Verwendet Extended-Frame-Format -> verwerfen
if (message.Flags.Flag.EFF)
{
// Lese nächste Nachricht
continue;
}
// Falls Daten vorhanden -> auslesen
else if (message.MsgLen)
{
// Leere Datenfeld
rxData.clear();
for (i = 0; i < message.MsgLen; i++)
{
// Daten müssen gedreht werden, da rxData aneinander gehängt wird.
// -1 da < MsgLen
rxData.append(message.MsgData[message.MsgLen -1 - i]);
}
}
// Leere Nachricht -> verwerfen
else
{
// Lese nächste Nachricht
continue;
}
// Versende Signal mit Daten
// Quelle: http://stackoverflow.com/questions/9411153/sending-signal-from-static-class-method-in-qt
emit m_instance->newMessage(rxIdent, rxData);
}
}