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:

c_com.h

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

where

  • 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!

EV3program

141 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!

  2. Hi Hans,
    please can you send me the code, I am working on a project and I need to send and receive messages from/to the EV3 and the PC.
    Thx a lot.

    • Dear Stdenny,

      I’m sorry, but I don’t understand your question. If you mean: how to move files from one PC to another, you can use an USB stick or another external storage device. But this is so obvious, I don’t think that this is what you mean. If you mean: how to move files from one brick to another: just use the Lego programming environment where the originals are stored. If you mean something else: please let me know what you exactly mean with your question.

      Kind regards,
      Hans

  3. Hi, can you please send me the code, i’m currently working on a project using the ev3, and i’m curious about what you’ve done, thanks !

  4. Hi Hans,
    Exactly what I was looking for, thanks for your article, thanks for posting. Can you send me the code also?
    Daniel

  5. Hi,
    could you send me the code ,please?i i’m currently working on a project to send some messages to ev3 but i think i’m doing something wrong…
    Thank you.
    Have a nice day!

  6. Hey, I’m trying to create BT communication using HC-06/05 between an arduino to the EV3 unit but can’t succeed for reasons beyond me, I know this is an old post and not necesserily relevent but after 4 days of failed connection I decided it’s time to ask for some help and was wandering if maybe you could point me to the right direction.

    Im using an Uno, HC-05 and HC-06, I can easily read data sent from the EV3 but was able to send a logic message to it only 2/3 times and the connection lasted only a few seconds, most of the time it just dosen’t work.

    Any help will be greatly appreciated.
    Thanks for your time and have a great week.
    Ido

    • Dear Ido,

      Just two remarks:
      1) BT communication doesn’t work with the Education firmware, we always use the Home Edition firmware
      2) We also had problems with the BT being unreliable, therefore we moved to WiFi connection.

      Hope this helps.

      Best,
      Hans

  7. Hi,Hans
    Could you please send me your code?
    I have a project, I need to send message from pc to nxt, but it has something wrong.
    thanks a lot.

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