BenbenIO / LearningAboutCan

Learning about Can and Linux Socket Can.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Learning About CAN

Learning about CAN and Linux Socket CAN.

This document tries to summarize basic explanations and useful links-resources to understand how CAN works, and presents some tools to start prototyping.

It also contains a simple project based on CAN-socket that's can be useful to explore a bit CAN with simple code.

Introduction

CANBUS (Control Area Network) is a widespread networking technology is the automobile industry, automation and embedded system.

ISO model

image

CAN model are based on ISO_15765 and ISO 11898 .

Hardware

This section sum-up information about CAN related hardware information.

Type

High-Speed CAN

A multi-star configuration seems typical of this bus with a primary bus line that branches into sub bus lines at its extremities then attaches to multiple device nodes. Differential voltage is applied over twisted pair at 1.5 to 2.5V and 2.5 to 3.5V for noise resistant signaling. Bit rates up to 1 Mbit/s are possible at network lengths below 40 m. Decreasing the bit rate allows longer network distances (e.g., 500 m at 125 kbit/s).

It also exist low speed CAN and CANFD.

Hardware solutions

The section list the some devices available on the market:

CAN packets

Structure

Image from Sparfun:

imageframe

  • SOF: The Start of Frame is a 'dominant 0' to tell the other nodes that a CAN node intends to talk
  • ID: The ID is the frame identifier - lower values have higher priority
  • RTR: The Remote Transmission Request indicates whether a node sends data or requests dedicated data from another node
  • Control: The Control contains the Identifier Extension Bit (IDE) which is a 'dominant 0' for 11-bit. It also contains the 4 bit Data Length Code (DLC) that specifies the length of the data bytes to be transmitted (0 to 8 bytes)
  • Data: The Data contains the data bytes aka payload, which includes CAN signals that can be extracted and decoded for information
  • CRC: The Cyclic Redundancy Check is used to ensure data integrity
  • ACK: The ACK slot indicates if the node has acknowledged and received the data correctly
  • EOF: The EOF marks the end of the CAN frame

CAN BDS

CAN BDS files content the database used to encode and decode CAN messages. They basically match CAN message to a meaningful information.

  1. Match CANID to BDSID For 11-bits ID, we can just match the decimal CANID to DBSID. For extended ID (29-bits), we need to apply a mask to the 32-bits DBSID to get the 29-bits CANID.

  2. Understand related signal The DBSfile provides signal information to decode the message. This signal include:

  • Start bit: where the information is content in the payload.
  • Length: the actual length of the data.
  • Encoding type: LittleEndian vs BigEndian.
  • Signed or Unsigned Value.
  • Scale and Offset: used to convert the data.
  • Data range.
  • Unit of the value.
  1. Decode Value Once the signal information is extracted, the value can be read from the payload, and then converted into meaningful data:
meaningful_value = scale * raw_value_decimal + offset
  1. Other part of DBS The DBS file content other information:
  • Comment for message or signal

  • Add attribute to message: format, version, multiplexing messages, ...

  • DBS file explained

  • []

Simple application

In order to get familiar with CAN, we tried to write a simple application:

  • Encode simple BDS messages.
  • Send CAN message over CANBus.
  • Read CAN message over CANBus.
  • Decode simple BDS messages.

diag ex

Development environment

On Ubuntu, we can use the available Linux Socket CAN, and related tools:

sudo modprobe vcan

Install the tools:

sudo apt install can-utils

Also Wireshark allows to dump, filter CAN socket packages.

Run the application

Dependencies

Please install:

  • spdlog: a header only logging librar.
  • boost: used for program option, but can be use next for parsing.
sudo apt install libspdlog-dev libboost-all-dev

Make and Run

1] bring up the virtual can up ./setup_vcan0.sh

2] make

3] run in two terminal:

./CAN_send.exe --dbs dbs/simplified.bds
./CAN_read.exe --dbs dbs/simplified.bds

exe_image

Similar project links:

Conclusion

CAN can seems quite forward on the first view, but a deeper look raise a lot of technical points: requiring quite a lot of low level management. (at least for me :)

ToDo

For documentation:

  • a modern status on CAN: what is the latest?
  • more complete

For the small application:

  • SocketCAN for extended ID.
  • a real DBS parser: all support, generating code
  • Research and implement a more real data formatter: frame.data <=> float, taking in account endianness, start, ect...
  • optimize Makefile with .o
  • check some RUST implementation

Others Resources

About

Learning about Can and Linux Socket Can.

License:Apache License 2.0


Languages

Language:C++ 94.1%Language:Makefile 4.9%Language:Shell 1.0%