-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCLX000CanBusUSB.cpp
71 lines (50 loc) · 1.55 KB
/
CLX000CanBusUSB.cpp
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
#include "CLX000CanBusUSB.h"
#include <QDebug>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
CLX000CanBusUSB::CLX000CanBusUSB(QString interface) : port(interface) {
port.setReadBufferSize(1024*1024*10);
connect(
&port,
SIGNAL(readyRead()),
this,
SLOT(dataAvailable())
);
queue.reserve(1024*1024);
}
bool CLX000CanBusUSB::open() {
qDebug() << "CLX000CanBusUSB - open";
frameCount = 0;
setState(CanBusDeviceState::ConnectingState);
bool status = port.open(QSerialPort::OpenModeFlag::ReadWrite);
if(status) {
setState(CanBusDeviceState::ConnectedState);
}
return status;
}
void CLX000CanBusUSB::close() {
setState(CanBusDeviceState::ClosingState);
qDebug() << "CLX000CanBusUSB - close";
setState(CanBusDeviceState::UnconnectedState);
port.close();
}
bool CLX000CanBusUSB::writeFrame(const QCanBusFrame &frame) {
bool resultFlag = false;
QByteArray packedFrame;
resultFlag = packFrame(frame, packedFrame);
qint64 bytesWritten = 0;
if( resultFlag ) {
bytesWritten = port.write(packedFrame);
resultFlag = (bytesWritten == packedFrame.length());
}
return resultFlag;
}
QString CLX000CanBusUSB::interpretErrorFrame(const QCanBusFrame &errorFrame) {
return QString();
}
void CLX000CanBusUSB::dataAvailable() {
auto const data = port.read(1024);
// Copy all data except the zero termination byte.
std::copy_n(data.cbegin(), data.length(), std::back_inserter(queue));
attemptParse();
}