This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.cpp
102 lines (76 loc) · 3.04 KB
/
main.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
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
#include <xpcc/debug/logger.hpp>
#include <xpcc/architecture.hpp>
#include <xpcc/architecture/interface/can.hpp>
#include <xpcc/architecture/platform/driver/can/canusb/canusb.hpp>
// #include <xpcc/architecture/platform/driver/can/socketcan/socketcan.hpp>
#include <xpcc/communication.hpp>
#include <xpcc/communication/xpcc/backend/can.hpp>
#include <xpcc/communication/xpcc/backend/can/connector.hpp>
#include <xpcc/communication/xpcc/backend/zeromq/connector.hpp>
/**
* Listens to a CAN bus connected by a CAN2USB and publishes xpcc messages with zeromq.
*
* How to use:
* - Connect a CAN2USB to a CAN bus with traffic.
* - Connect this CAN2USB by USB to your computer.
* - Make sure the user can access the /dev/ttyUSB0 device.
* Brute force method is: #sudo chmod 777 /dev/ttyUSB0
* A wiser method is to add the user the appropriate group
* - Adjust the baud rate of the CAN bus in this example.
* - Do
* scons run
* - All xpcc messages will be published on port 8211 by zeromq
*/
// Default CAN bitrate
static constexpr xpcc::Can::Bitrate canBusBitRate = xpcc::Can::kBps125;
/* Either use an USB CAN2USB adaptor with xpcc Lawicel interpreter
or use a CAN controller supported by Linux' SocketCAN.
With SocketCAN the baudrate must be set with the operating system.
$ ip link set can0 type can bitrate
*/
xpcc::hosted::SerialInterface port("/dev/ttyUSB0", 115200);
using CanUsb = xpcc::hosted::CanUsb<xpcc::hosted::SerialInterface>;
CanUsb canUsb(port);
// static xpcc::hosted::SocketCan canSocket;
static xpcc::CanConnector< CanUsb > canConnector(&canUsb);
// static xpcc::CanConnector< xpcc::hosted::SocketCan > canConnector(&canSocket);
#undef XPCC_LOG_LEVEL
#define XPCC_LOG_LEVEL xpcc::log::DEBUG
int
main()
{
XPCC_LOG_DEBUG << "ZeroMQ SocketCAN XPCC bridge" << xpcc::endl;
if (not canUsb.open(canBusBitRate)) {
// if (not canSocket.open("can0" /*, canBusBitRate */)) {
XPCC_LOG_ERROR << "Could not open port" << xpcc::endl;
exit(EXIT_FAILURE);
}
const std::string endpointOut = "tcp://*:8211";
const std::string endpointIn = "tcp://*:8212";
xpcc::ZeroMQConnector zmqConnector(endpointIn, endpointOut, xpcc::ZeroMQConnector::Mode::PubPull);
XPCC_LOG_DEBUG << "Entering main loop" << xpcc::endl;
while(true)
{
canConnector.update();
zmqConnector.update();
while (canConnector.isPacketAvailable())
{
xpcc::Header header = canConnector.getPacketHeader();
xpcc::SmartPointer payload = canConnector.getPacketPayload();
XPCC_LOG_DEBUG << "C->Z " << header << " " << payload.getSize() << " " << payload << xpcc::endl;
zmqConnector.sendPacket(header, payload);
canConnector.dropPacket();
}
while (zmqConnector.isPacketAvailable())
{
xpcc::Header header = zmqConnector.getPacketHeader();
xpcc::SmartPointer payload = zmqConnector.getPacketPayload();
XPCC_LOG_DEBUG << "Z->C " << header << " " << payload.getSize() << " " << payload << xpcc::endl;
canConnector.sendPacket(header, payload);
zmqConnector.dropPacket();
}
xpcc::delayMilliseconds(10);
}
canUsb.close();
// canSocket.close();
}