Tuesday, April 1, 2014

Listen to CAN-BUS Messages using the Arduino & Seeed CAN-BUS Shield

In the Setting up a CAN-BUS Interface for a Jeep JK, with pin out information. blog post i've shown you the basic way of connecting to the can-bus in your vehicle.

The next step will be to upload a sketch to the Arduino that will listen to all messages on the CAN-BUS and print them, to the serial terminal as it comes in.

Getting right down to business.
In order for the Arduino UNO to interface with the Seeed CAN-BUS Shield you need to download the libraries from here --> GitHub Its also a good idea to get to know how the device works. Head over here and read more. After downloading the libraries you need to import the Zip file into your local library db by clicking.
Sketch --> Import Library --> Add Library  , you don't have to unzip the contents the zip file can be imported as is.

After importing the library you will see the CAN-BUS example sketches under the example sketch menu.

To save you allot of trouble the following is very important.
The example sketch for receiving works perfectly fine when connecting to the CAN-BUS, but it does not include the CAN ID where the messages comes from which will be useless for you.

IE Without the ID you will get. 8 Bytes
235 05 200 123 9 173 000 123 which could be from pressing the Sway Bar Disconnect.
235 05 200 123 9 173 000 123 which could be some general can bus data.

Looking at the above you will never find what you are looking for, because it seems to be the same message, but its not.

With the CAN ID it will look like this.

ID234- 235 05 200 123 9 173 000 123 which could be from pressing the Sway Bar Disconnect.
ID135- 05 200 123 9 173 000 123 which could be some general can bus data.

Hope you catch the drift.

I modified the sketch to include the CAN ID when it prints the message to the terminal. you can download it here
 Working Example CAN-BUS message receiver

Once you have uploaded the above sketch and you have connected to the CAN-BUS in your vehicle, using the Arduino Editor's Serial monitor, you will see the CAN-BUS messages bombarding the terminal, it will be virtually impossible for you to catch messages, such as pressing the Central Locking or switching on lights ect ect.

Instead of using the Arduino serial monitor i'm using putty.exe (Google it you will find it).
putty is a ssh client but it can also be used as a serial hyper-terminal.

Use the following settings.

Once you connect it will save all in-coming data to the file you have you have specified in text format.

From here you are on your own.
But to give you a tip.

You will want to connect the everything up, and let this system run for a few minutes so that you can build a decent size database, I let it run for about 5Mins which generated about 30 000 lines.

I then disconnect everything (Remember the Arduino Can-bus shield remembers some of the traffic and does not flush it when you disconnect putty, so rather stop everything)

Then start over by connecting using putty, then quickly press a button  a few times (It helps to count how many times you pressed it and use the counts as the file name of the log file) in your vehicle for the can bus shield to capture, then as quickly disconnect.
Another tip, repeat the "Button Capture" process twice.

Once you have the 3 files you can then start to Analise it, I removed all messages found in the "Button Press" log files found in the first run, that way you can be sure they are not to be confused with the button presses, Once you have filtered everything out you will be left with not too many different messages.

My way of removing "Idle" messages
I simply  opened each file then Find & Replace all the "Tabs" with nothing and also Find & Replace all spaces with nothing using Notepad++.

I then create a new sheet copy and paste directly from notepad++ into excel.
Putting the idle messages (The long list into column 1) and the Button press messages into column 2
using the below VBA code i find all the messages from col2 in col1 then flag them so i can remove them afterwards.

Dim LineCount1 As Double
Dim LineCount2 As Double

Dim Col1 As String
Dim Col2 As String

Private Sub CommandButton1_Click()
For LineCount1 = 1 To 65000
    If Sheet1.Cells(LineCount1, 2) = "" Then Exit For
    Label1 = "Busy with Line:" & LineCount1
    Col2 = Sheet1.Cells(LineCount1, 2)
    For LineCount2 = 1 To 65000
        Col1 = Sheet1.Cells(LineCount2, 1)
        If Sheet1.Cells(LineCount2, 1) = "" Then Exit For
        If Col1 = Col2 Then
        Sheet1.Cells(LineCount1, 3) = "Match Found"
        Exit For
        End If
    Next LineCount2

Next LineCount1

MsgBox "Done"

End Sub

In my next guide i will show you how to send those messages back into the can but and have your vehicle react to it.

No comments:

Post a Comment