MonoBrick Communication Library

The MonoBrick communication library is a LEGO Mindstorms communication library written in C# that works with the standard firmware on both the EV3 and NXT. MonoBrick communication library runs on a wide variety of operating systems including Linux, Mac OS and Windows. The library can communicate with the brick using either USB, Bluetooth or WiFi(only EV3). Furthermore it is also possible to communicate with the brick over the Internet using a tunnel connection. Tunnel communication is done using MonoBrick Tunnel or MonoBrick Tunnel for Android. Since the library works with both Mono and Microsoft’s.Net Framework it can be used with either Visual Studio or MonoDevelop/Xamarin Studio. Although most Windows users will prefer Visual Studio – MonoDevelop/Xamarin Studio is definitely worth a try. MonoBrick communication library has the following features:

  • Communicates with the brick using USB, Bluetooth, WiFi or a tunnel connection
  • Works with Linux, Mac OS, Windows, Windows Phone and Android
  • Support for all LEGO sensors that comes with the Retail and Educational sets
  • Support for a wide range of Hitechnic sensors (not yet supported on the EV3)
  • Control the motors individual or as a vehicle
  • Use the mailbox system to send messages/data to the brick
  • Play tones and sound files
  • Use the file system to download and upload files
  • Start and stop on-brick programs
  • Use exceptions to catch sensor and communication errors
  • Open and close connections with multiple bricks
  • and more..

Writing your first program

Before starting please make sure that you are able to communicate with the Mindstorms brick over either USB or Bluetooth. Mac Os and Linux uses will also have to make sure that Mono is installed. These guides will help you setup communication under Windows, Linux and Mac OS for use with the library. Once you have a working connection download the MonoBrick library source files using the download link and add these to your project. Another option is to download the test project and use this as a starting point. The test project includes the source files and the sample program shown below. For more examples have a look at the library guide as well as the documentation.

using System;
using MonoBrick.EV3;//use this to run the example on the EV3
//using MonoBrick.NXT;//use this to run the example on the NXT  
namespace Application  
{  
    public static class Program{  
      static void Main(string[] args)  
      {  
        try{  
            var brick = new Brick<Sensor,Sensor,Sensor,Sensor>("usb");  
            sbyte speed = 0;  
            brick.Connection.Open();  
            ConsoleKeyInfo cki;  
            Console.WriteLine("Press Q to quit");  
            do   
            {  
                cki = Console.ReadKey(true); //press a key  
                switch(cki.Key){    
                    case ConsoleKey.R:    
                        Console.WriteLine("Motor A reverse direction");    
                        brick.MotorA.Reverse = !brick.MotorA.Reverse;    
                    break;                              
                    case ConsoleKey.UpArrow:     
                        if(speed < 100)    
                            speed = (sbyte)(speed + 10);    
                        Console.WriteLine("Motor A speed set to " + speed);    
                        brick.MotorA.On(speed);    
                    break;    
                    case ConsoleKey.DownArrow:     
                        if(speed > -100)    
                            speed = (sbyte)(speed - 10);    
                        Console.WriteLine("Motor A speed set to " + speed);    
                        brick.MotorA.On(speed);    
                    break;    
                    case ConsoleKey.S:     
                        Console.WriteLine("Motor A off");    
                        speed = 0;    
                        brick.MotorA.Off();    
                    break;    
                    case ConsoleKey.B:    
                        Console.WriteLine("Motor A break");    
                        speed = 0;    
                        brick.MotorA.Brake();    
                    break;  
                     case ConsoleKey.T:    
                        int count = brick.MotorA.GetTachoCount();  
                        Console.WriteLine("Motor A tacho count:" +count);    
                    break;  
                    case ConsoleKey.C:    
                        Console.WriteLine("Clear tacho count");    
                        brick.MotorA.ResetTacho();  
                    break;  
                    case ConsoleKey.M:  
                        Console.WriteLine("Enter position to move to.");  
                        string input = Console.ReadLine();  
                        Int32 position;  
                        if(Int32.TryParse(input, out position)){  
                            Console.WriteLine("Move to " + position);  
                            brick.MotorA.MoveTo(50, position, false);  
                        }  
                        else{  
                            Console.WriteLine("Enter a valid number");  
                        }  
                    break;  
                }  
            } while (cki.Key != ConsoleKey.Q);  
        }  
        catch(Exception e){  
            Console.WriteLine("Error: " + e.Message);  
            Console.WriteLine("Press any key to end...");  
            Console.ReadKey();                
        }  
      }  
    }  
} 

Support

The MonoBrick communication library has its own forum where you can find answers and questions related to the library.

13 comments on “MonoBrick Communication Library
  1. Author Image Konne says:

    Hey Anders, it’s me again.

    After the failure with the Monobrick remote connecting to the Ev3, I recently downloaded the Monobrick Test Project.

    I connected the EV3 via USB to the PC, but when I open the Test Application and debug it, I got the message, that the application cannot build a connection to the brick.

    The compiler says:
    An exception of the type “System.BadImageFormatException” appeared in TestApplication.exe.
    An exception of the type “MonoBrick.ConnectionException” appeared in TestApplication.exe.

    So, do you know what the problem could be. I’m using the EV3 (Education set), firmware 1.03E and the EV3 software is running on my PC.

    And by the way, thank you very very much again for the whole Monobrick project and for your help.

    Greets,
    Konne

  2. Author Image S0N_0F_A_S0N says:

    Hey, I have no idea how to set this up. >_< Could you help me?

  3. Author Image minghan Li says:

    Visual Studio (C#) can use Android Library DLL ?
    only MonoDevelop/Xamarin Studio can use?

  4. Author Image CharlieJiang says:

    Hello,I am a Java Android developer…. But now I see your library, think it is good. And it is a DLL library. How I use it??????? But seems the Android cannot use library. How I do?

  5. Author Image Bojun Lin says:

    Is this library works with your MonoBrick firmware as well?

  6. Author Image Guillaume says:

    Hello,
    Can you said me, how the dll (Monobrick.dll) is run (communication, etc) ?
    It’s very urgent !
    Thanks in advance, and thanks ask me the 03/05/2015 !
    Regards

  7. Hi, this library is broken on mac – you need to change the name of the external calls to the hidapi to “hidapi.dylib”

  8. Nice – thanks for using MonoBrick

5 Pings/Trackbacks for "MonoBrick Communication Library"
  1. […] you’re a fan of C#, then you will love Monobrick, a communication library that works with the EV3.  It uses the EV3’s direct commands to remote […]

  2. […] Dr. Vargas originally considered using an Arduino, he discovered that the MonoBrick Communication Libraries let him integrate everything into a single project – receiving data from the Leap Motion SDK and […]

  3. […] code for his project uses the Leap Motion SDK libraries and the MonoBrick communication libraries to control the EV3 via […]

  4. […] need to be using the standard MonoBrick Communication Library if you want to connect remotely to the […]

  5. […] For second task there are many dot.net libraries for Lego NXT API. We choose MonoBrick. It can be found at ww.monobrick.dk. In this fine site you can find multiple samples and documentation. As prefix Mono refers the library is usable also on other platforms than only windows. Also version for latest Lego EV3 platform is available. What we need is  MonoBrick Communication Library, find  on page download Library DLL : […]

Make a donation

Download