The Controller Area Network (CAN) is a multi-master bus communication system able to connect a distributed electronic systems requiring a high-security level, signal integrity, and strictly real-time functionality.
It was originally developed by the company Bosch and it’s now been standardized a series of ISO standards.
It’s widely used in vehicle systems but it’s also being used for a number of other applications in embedded systems, industrial applications and different types of medical devices. For example in a typical car, you could find as many as 70 CAN devices.
The information is transmitted by two twisted wires that connect all system modules. Each node is able to send and receive messages and the bus is able to handle an infinite ( theoretically ) node devices.
It is designed for a vehicle type environments the bit rates are up to about a megabit for second within a 40-meter size limit.
Each node is able to send and receive messages and intercepts the messages destined to it.
It decrypts the packets and extracts the identifier in the data string. Only when is required and the bus is free, a node can start to transmit data.
In another word, In a CAN network, all nodes are not set with a pre-fixed “address”; this feature allows to add or remove nodes into the bus in a very easy way.
Hardware and software used in this tutorial.
In this tutorial, we are going to use an Amber2 board with a CAN extender Module and a PCAN adapter from PEAK-System Technik GmbH. We are going to create a CAN network to exchange CAN data frames from/to Amber board and a PC. Into the Amber board, we are going to create a software application using Qt framework and its CAN bus software module.
Amber Board Can extended card.
Amber board is provided by an extension card to connect the board to physical two-wire CAN bus.
This extension board uses the Flexible Controller Area Network (FlexCAN) peripheral included into all the Imx6 processor an in all Veriscitr SOM too of course!
The only hardware required to connect Amber board into CAN bus is a physical drive to adapt the signal levels and handle the CAN lines in a proper way. In this board is used a TJA1051 device as bus interface.
It is a high-speed CAN transceiver that provides an interface between a Controller Area Network (CAN) protocol controller and the physical two-wire CAN bus. This transceiver can work in high-speed CAN mode an It is providing differential transmit and receive capability to (a micro controller with) a CAN protocol controller.
Pc CAN to USB interface.
To play with a CAN bus we need two different CAN device at least.
As second CAN device, I decided to use a PC provided with a dedicated hardware to catch and show CAN data packet.
Even if we could use a Linux virtual VAN driver to test the device, for this example I used a PCAN-USB adapter to create a second Can Network Node. It is a compact plastic casing devices suitable to connecting a PC into a CAN network using its USB interface.
The device is also supplied with a PC software (PCAN-View for Windows); a very easy and free software to monitor all CAN network and sen receive data from/to the PC.
Qt Can Bus.
Qt framework is provided with a software module called Qt Serial Bus supporting CAN and Modbus protocols too.
It was introduced as technical preview in Qt 5.6.0 and finalized into the follow Qt versions. From Qt 5.8 is stable.
Its license model is similar to all other Qt modules ( LGPLV3 ) and the source code is publicly available on the Qt git repository.
Please don’t confuse this module with the QSerialPort module. QSerialPort module is for rs-232 type serial interface only.
Qt CanBus uses plugins to property handle the hardware interface and there is a different plugin for the different hardware interfaces.
Qt framework Currently supports the following backends:
- SocketCan which supports Linux sockets
- Peak CAN which supports PCAN adaptors from PEAK-System Technik GmbH
- TinyCAN which supports hardware adapters from a company called MHS Elektronik
- VectorCAN, which supports Vector Informatik CAN adapters.
In the Amber board, we are going to use Linux sockets CAN bus plugin.
To handle Can bus there are six major classes.
- QCanBusDevice:: Filter – Define a filter for CAN bus messages
- QCanBusDevice – The interface class used as the plugin interface
- QCanBusFactory – Factory class used as the plugin interface
- QCanBusFrame – Container class representing a single CAN frame
- QCanBusFrame::TimeStamp – timestamp information with usec precision.
To create a software application using CAN Bus module you have to follow these steps:
- Create a device, specify plugin and device name
- Create data frames
- Send data frames
- Disconnect when done.
QCanBusDevice emits signals: errorOccurred, framesReceived, framesWritten, stateChanged.
To receive frames you have to connect to signal framesReceived.
CreateDevice is a static method that ’s basically a singleton design patterns the lets you create a CAN Bus device specifying the backed that you want and the device name.
In the next step, you have to Connect to the device using the connectDevice method.
To a real software application is better to use signal and slots scheme to drive the events and catch all device status change signals.
For example, ConnectDevice works in asynchronous mode and it happens sometime after the call.
After this steps, you would typically create a data frame with your data payload and send the packet.
Hour Amber Can test program is basically a software running the saw Qt code but with a little application around it and hooked it up to some buttons so that you could test connecting sending some data disconnecting and it uses the can device.
Create Qt Application
Create a new empty Qt Project.
Select Amber Kit.
Select a simply QWidget as base class for the new application.
Qt Creator opens a new empty base project.
Select and open.PRO project file and insert the following rows:
QT += serialbus
to include the serial bus module into the project.
target.path = /home/root
INSTALLS += target
to finalize the project into the target Amber board.
Create a very simple GUI with a couple of button to Connect and Disconnect the Can Bus, and a text view to show the incoming CAN data frames.
In the main includes file ( widget.h), defines a Can Bus Device and some slots to connect and disconnect the bus.
Into the connectDeviceSlot(), create an instance of CanDevice, connect all the required slots, and call the conncetDevice API to open the bus.
To write a data frame into the bus, Create a CanBus Frame obj, fill this object with the data payload, set the frameID, and write the frame into the bus.
When the frame is received, the relative slot will be invoked.
Let’s test the application into the Amber board.
From Board Serial Console checks the status of the socketcan interface by typing the following line:
ifconfig can0 up
if CAN interface hasn’t defined yet as in above example, type the following command:
ip link set can0 up type can bitrate 1000000
Now you are ready to test and run the Qt application.
Connect the CanL and CanH lines in a proper way with the PCAN device.
Press the Run button from Qt Creator user interface
After some seconds Qt Can application will start on the Amber Board.
Press the Connect Button.
Now you could view CAN data frame incoming from the PC and you can send data to the PC by press the Send
In the following video you can see my CAN network running.