I am writing this article in response to a question posted on my YouTube channel. Here I would be talking about communicating to an OBD-II device (ELM327 chip with Bluetooth) hooked into your car’s OBD-II port. The OS I am using is Windows 10 IoT core. This information is important because it makes a difference in terms of implementation of communication standards. Windows (since long time) maps Bluetooth connections to COM ports (I am not talking about all of them but where you have an interface which supports RS232 standard – example Bluetooth tethering of Mobile phones for internet connection sharing or OBD-II for real time data). The language that you use to program your solution doesn’t matter because the communication related stuff is available as extensions to Windows UWP SDK but I would be using C#. The code for this article can be found here. Let’s get started.
What is Serial Communication?
Serial communication is different from parallel communication in the sense that you send one bit at time as opposed to a group of bits. Moreover there is no multiplexing and you are either sending or receiving data on a single stream at any point in time. This removes the overhead of synchronization and complexities related to multiplexing of connections. For this reason it is the a good choice for simple point to point communication. You can read more about it in this Wikipedia article.
How do I connect and talk to a device?
In our setup (Windows 10 IoT core on Raspberry Pi 3 using C# – you can use any other language as well which is supported for UWP applications). In Windows 10 UWP platform SDK you have classes that expose information about connected devices to your machine (in our case Raspberry Pi 3). You query the “DeviceInformation” class for devices of a particular class (in our case devices on a Serial Port). The below code snippet shows you how to do that (it connects to the first device found on Serial Port – you can however use device ID if you know it):
Once you have a device service object, next step is to get input (read) and output (write) streams for the interface to read and write data. You do so by connecting to using a StreamSocket class (ConnectAsync method shown above using the deviceService object that we created earlier).
Once you have the StreamSocket instance you can create instances of DataReader and DataWriter classess (passing the references of Input and Output streams from StreamSocket object), the below snippet explains you how to do it:
Now you are all set to communicate to your ELM 327 chip using the RS 232 communication specifications (there are initialization commands that you need to send to your adapter to setup the communication channel and format). The below snippet does that:
and here is the code for the underlying SendCommand Method that writes and reads the response back:
This entire code is published on GitHub and you can clone a copy from there and play with it. You can find this here.
Happy Coding..
See you soon!
Leave a Reply