Sending messages from a PC application in C# to the Lego EV3

I have managed to connect the EV3 to a PC and are now able to send messages back and forth between the PC and the EV3 using bluetooth. Although it is no rocket science, it wasn’t that easy since there is no documentation available on the Internet about the protocol. So, there was no other way then to download the Linux EV3 source (this is the documentation, and it can be compiled too ;-)) and to locate the code that is responsible for the messaging. And I found the following in the ‘c_com.h’ file:


So, that shouldn’t be that difficult to send a string from the PC to the EV3. I wrote a small program that reads out a mailbox on the EV3, and ‘tataa’: it worked. Adding a second mailbox¬† to test if another mailbox name would work as well, was just some additional minutes of work. But… it didn’t work as expected! The first mailbox worked still fine, but after sending a message to the second mailbox (which also worked fine), the first mailbox didn’t work anymore.

To check if the EV3 program was not the problem, I took a second EV3 and wrote a small program to send messages. This worked as expected, so it had to be the C# program that causes the problem. Since I didn’t make any progress, I started to write the part to send messages from the EV3 to the PC. This was far more easy:

  • Just write all the bytes you read?
  • Check if you can locate in the HEX codes the string that I send from the EV3
  • And then figure out how the message is coded

And this is the result:

  • bbbbmmmmttssllaaaLLLLppp


  • bbbb = bytes in the message, little endian
  • mmmm = message counter
  • tt = 0x81
  • ss = 0x9E
  • ll¬† = mailbox name length INCLUDING the \0 terminator
  • aaa… = mailbox name, should be terminated with a \0
  • LLLL = payload length INCLUDING the , little endian
  • ppp… = payload, should be terminated with the \0

So, it seems that the comment in the header file was wrong. It was stupid to trust that the programmer had written flawless comment ;-).

I updated the code to send data to the EV3 with this new information, and now it all works fine!


107 thoughts on “Sending messages from a PC application in C# to the Lego EV3

  1. Hi,
    could you please sent me the code too? I’m doing something wrong in C# – after sending message from PC to EV3 the whole Bluetooth stack seems to crash :).

    Thank you for your work!

Leave a Reply

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

You are commenting using your 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