Talking to your car – with OpenXC, Android, Xamarin & F#

Virtually all vehicles today operate an internal network called the CAN bus. Different modules (or ECUs – electronic control units) in the car communicate with each other via this network (e.g. engine, transmission, dashboard, etc.). In this post I reference a CAN bus reader built with F# and Xamarin.Android based on the OpenXC platform (http://openxcplatform.com/).

Update: Also see this post.

Dealing with CAN bus signals is a vast subject area that cannot be justly dealt with in this brief post. However a good place to start is the OpenXC portal (http://openxcplatform.com/). OpenXC is an open source (hardware & software) project to allow developers to build their own apps based on reading and writing of CAN messages.

In this brief post I will just reference the F# source on GIT (https://github.com/fwaris/XaOpenXC) and detail some of my experiences on building the project.

First some screen shots (Nexus 7 device):

xaopenxc0xaopenxc1xaopenxc2

Here’s how the different pieces are connected:

xaopenxc3

The OpenXC hardware is connected to the car via a special connector called OBD II port. The Android device is connected via a USB (host mode) cable. A service on the app opens an interface on the attached USB device (OpenXC) and receives and transmits messages. For now the app mostly reads and displays messages. Bluetooth support is planned.

Please note that there are many such aftermarket devices available. OpenXC is by no means the only one. Also the Android app store has many CAN bus reader apps (some even for OpenXC).

For me, this is the first time that I have built a substantial app for Android and first significant use of F# in conjunction with Xamarin.Android tooling. F# support is relatively new in Xamarin however the fact that I was successfully able to build much more than a toy app shows the potential of F# and Xamarin for Android. The entire app was built with Xamarin Studio which while not as polished as Visual Studio is still very useable. See http://fsharp.org on how to get started with F# on Android.

I had to painstakingly study the OpenXC Java source for Android apps to recreate similar functionality in F#. The F# code talks directly with the hardware. None of the Android open source OpenXC components were used. One of the most challenging parts was getting the USB hardware interface sorted out. Anyone looking to interface with USB devices with Xamarin.Android should take a look at UsbConnector.fs.

It is interesting to note that the Java code (accessible from the OpenXC portal) relies heavily on threads and locks whereas the F# code mostly leverages functional programming idioms such as Actors (MailboxProcessor) and the Async monad (and is therefore much cleaner IMHO).

At the time of this writing F# was about 600 lines of code. The Java and F# code bases are not directly comparable as OpenXC offers more features. For reference only, the very rough equivalent of F# on the Java side is about 7,700 lines of code (as measured by this technique)

About these ads

3 thoughts on “Talking to your car – with OpenXC, Android, Xamarin & F#

  1. Pingback: F# Weekly #36 2013 | Sergey Tihon's Blog

  2. Pingback: OpenXC based Android App | Faisal's space

  3. Pingback: Xamarin F# – Experience Report | Faisal's space

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s