Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Tuesday, June 23, 2009

Playing a bit with SocketCan and its utilities

This time I had a better look at the SocketCan utilities that I mentioned in a previous post. As I have already told, you can get and install them with these commands:
svn checkout http://svn.berlios.de/svnroot/repos/socketcan/trunk canbus
cd canbus/can-utils/
make
make install

And you will end up with lots of new programs under /usr/local/bin:
asc2log canbusload candump cangen
canlogserver canplayer cansend cansniffer
isotpdump isotprecv isotpsend isotpsniffer
isotptun log2long slcand slcanpty vcan

To see how canbus works, I tried to setup a virtual can bus with these commands:

modprobe vcan can-raw
ip link add dev vcan-test type vcan
ifconfig vcan-test up

These basically creates a canbus named vcan-test and set the interface up.
Then I opened two terminals and ran the command candump on one terminal and the command cansend on the other terminal. These are the outputs:
# cansend vcan-test 5A1#11.2233.44556677.88
# cansend vcan-test 5A1#11.2233.44556677
# cansend vcan-test 1F334455#1122334455667788

# candump vcan-test
vcan-test 5A1 [8] 11 22 33 44 55 66 77 88
vcan-test 5A1 [7] 11 22 33 44 55 66 77
vcan-test 1F334455 [8] 11 22 33 44 55 66 77 88

That are the message id and the data. The number between the brackets is the data length and the last case is with an extended id.

This could also be the text format used by the slcan driver to communicate via serial port.
I have tried to use slcanpty to attach the vcan-test interface to a pseudo terminal but without success so far. I will have to try again soon as it could be the way to gather the data on the Linux machine without writing an additional custom layer.

Thursday, June 18, 2009

Two interesting CANBUS projects and some links

This CANalyser tool is pretty similar to what we want to do at the moment and is built with very similar chips. The project seems a bit outdated but the information is still very useful.
The tool is composed by a PIC microcontroller PIC18F458, and a MCP2551 CAN transceiver. The main features are listen to the CAN bus in active or passive mode and forward data to the serial port to the PC and send data to the CANBUS. This is exactly what we need now.
Digging further in the web pages I discover that they have built a real-time kernel to run their software on the PIC. There is a company behind this (Pragmatec) but they have released the sources under a GPL license. The website of the os is at PICOS18
There is a new version of the pages here but the page about the CANalyser tool is the same.

Another very similar tool is CAN2PIC. This one uses
PIC18F248 and MCP2551 CAN.
You can find there all the schematics of the circuit used and also all the software! So this page should provide all we need to get started and running very fast!

There are some other interesting links I found tonight:
  • GNUPIC: a small directory site with information on PIC programming with GNU/Linux
  • Another very good CANBUS learning page at CERN (yes they used it in LHC)
  • PICMICRO WebRing: A collection of websites about the Microchip PIC microcontroller and related projects

Sunday, June 14, 2009

Canbus programming under linux: SocketCAN

Well the good news is that someone already did the hard part for us!
The Linux kernel includes since several versions the support to the can protocol and provides what seems to be a very easy interface to it. It's called SocketCAN and it is modeled after the standard unix socket API.
The project page is at BerliOS and hosts the kernel patches (not needed anymore as it is included in mainstream) and some user space tools that I just downloaded and installed without a glitch. You can get them by accessing the project subversion repository with this command: svn checkout http://svn.berlios.de/svnroot/repos/socketcan/trunk canbus

More info on what is and how to use SocketCan can be found in the linux kernel documentation directory (available on the web here)

As for the device drivers, SocketCAN supports a few drivers alreay, two of which are particularly interesting: vcan and slcan. Vcan is a virtual CAN interface driver and is there for testing when there is no hardware available. This will be probably the first to try once I realize how the user space utilities work. The slcan driver is "For a bunch of CAN adaptors that are attached via a serial line ASCII protocol (for serial / USB adaptors)" and should match exactly what we are trying to do. I just wonder what is the data format the slcan drivers expects to receive from the serial port. I have only found a strange document here so far but the semantic is not clear to me and I will have to investigate this further.

Saturday, May 23, 2009

Great Can Bus hacked page on a Saab

I have found this really interesting page here. It is from a guy who reverse engineered the instrument bus on a Saab. This bus is called I-bus and is based on CAN bus. He gives a lot of information on how he did it, starting from locating the connector, attaching to the bus and interfacing with the PC, along with the tools he used.
It is very similar to what we would like to do and thus we can get lots of advices from it. There are important differences though. He bought ready-to-use CANBUS connectors while we are trying to do it by ourselves. Well actually he says that he tried to do it himself but could not make it work so he switched to commercial products, but he does not give much information on how he tried and what did not work.
The other big difference is that as he is using commercial products, he also has access to programs on Windows platform that can decode bus messages and help during reverse engineering. We won't have these programs because of our do-it-yourself choice and also because we plan to use Linux to get access to the bus by using a serial interface. This is clearly much harder to do but I hope it is not so hard that we will eventually be forced to switch to commercial products.
We will also have to develop our own programs on the Linux platform, but this should not be a big problem. We have to look around for already existing projects to see if we can reuse some code.

This page also gives us a real example of what the message on CANBUS looks like and how they are used by the car. He also explains how the information is spread on multiple messages as for the case of the radio display. Definitely an helpful page!