MonoBrick Communication Library Programming Guide

This guide descries how to use the MonoBrick communication library with the EV3. Users of the NXT can also use this guide as inspiration as the syntax for the NXT is very similar. Be sure to check the MonoBrick communication library documentation for a complete list of all functionalities for both the EV3 and NXT. This guide contains the following sections.

Connecting to the Brick

The examples in this sections shows how to establish a connection to both the EV3 and NXT brick. Before proceeding please make sure that you are able to communicate with the brick. Mac Os and Linux uses will also have to make sure that Mono is installed. The set-up guides found here will help you setup communication under Windows, Linux and Mac OS for use with the MonoBrick communication library.

USB connection

To create a USB connection make sure that you have copied the libusb and the hipapi files that came with the MonoBrick communication library download to the same folder as the exe file. If you are using the MonoBrick communication library test project you do not need to worry about this. The following code will open a usb connection and move motor A.

using System;
using MonoBrick.EV3;

public static class Program{
  static void Main(string[] args)
  {
    var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("usb");
    try{
        ev3.Connection.Open();
        ev3.MotorA.On(50);
        System.Threading.Thread.Sleep(3000);
        ev3.MotorA.Off();
    }
    catch(Exception e){
        Console.WriteLine(e.StackTrace);
        Console.WriteLine("Error: " + e.Message);
        Console.WriteLine("Press any key to end...");
        Console.ReadKey();				
    }
    finally{
        ev3.Connection.Close();
    }
  }
}
Bluetooth connection

Before using a bluetooth connection make sure that you have paired your computer with the LEGO Brick. Use the MonoBrick communication library set-up guides to help you with this. On windows the following code will connect to the brick using com6.

var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("com6");

Replace this line with line 7 in the usb example to try it out. On Mac Os the bluetooth path might be called something like “/dev/tty.EV3-SerialPort” where EV3 is the name of the EV3 brick or /dev/tty.Great-DevB where Great is the name of the NXT Brick. The code below will create a brick object that connects to the EV3 brick called EV3. Replace this line with line 7 in the usb example to try it out.

var ev3 = Brick<Sensor,Sensor,Sensor,Sensor>("/dev/tty.EV3-SerialPort");
WiFi connection

WiFi connection only works with the EV3 and should not be confused with the tunnel connection which is described below. To use a WiFi connection you need to connect the EV3 to a WiFi network. This guide shows you how to do this. Once the brick is connected to the network replace the following line with line 7 in the usb example.

var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("wifi");
Tunnel connection

A tunnel connection lets you communicate with your LEGO brick using either the MonoBrick Tunnel or the MonoBrick Tunnel for Android. This allows you to communicate over both LAN and WAN. Currently the tunnels only supports the NXT but support for the EV3 will be added. Once you have the tunnel connected to the brick use the following code to create a connection to the tunnel listening for clients on a computer with IP-Address 192.168.1.11 on port 1500.

using System;
using MonoBrick.NXT;

public static class Program{
  static void Main(string[] args)
	{
	  var nxt = new Brick<Sensor,Sensor,Sensor,Sensor> ("192.168.1.11",1500);
	  try{
		nxt.Connection.Open();
		nxt.MotorA.On(50);
		System.Threading.Thread.Sleep(3000);
		nxt.MotorA.Off();
	  }
	  catch(Exception e){
		Console.WriteLine(e.StackTrace);
		Console.WriteLine("Error: " + e.Message);
		Console.WriteLine("Press any key to end...");
		Console.ReadKey();				
  	  }
  	  finally{
		nxt.Connection.Close();
  	}
  }
}

If you want to have the tunnel connect to the client if for example the tunnel is running behind a firewall the following code will wait for the trunnel to establish a connection. Replace these two lines with line 6 in the tunnel example above

var connection = new MonoBrick.TunnelConnection<Command,Reply>(1500);
var nxt = new Brick<Sensor,Sensor,Sensor,Sensor>(connection);

To make the tunnel connect to the client please consult the MonoBrick Tunnel page or the MonoBrick Tunnel for Android page.

Controlling motors

Controlling motors with the MonoBrick communication library is easy. In this section the different motor movements are shown including motor synchronisation and vehicle control. The code below demonstrates how to make the motor move forwards, backwards, brake and stop.

var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("usb");  
ev3.Connection.Open();
ev3.MotorA.On(50);
System.Threading.Thread.Sleep(3000);
ev3.MotorA.On(-50);
System.Threading.Thread.Sleep(3000);
ev3.MotorA.Brake();//motor is still on but does not move
System.Threading.Thread.Sleep(3000);
ev3.MotorA.Off();
ev3.Connection.Close();
Position control

The following example demonstrates how to make the motor move to relative and absolute positions. When moving to a position please note that if you do not set the break flag the motor will coast when the position is reached and not stop immediately.

using System;
using MonoBrick.EV3;
using System.Threading;
public static class Program{
	static Brick<Sensor,Sensor,Sensor,Sensor> ev3;  
	static void Main(string[] args)
	{
		ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("usb");
		try{
			ev3.Connection.Open();
			ev3.MotorA.ResetTacho();
			ev3.MotorA.On(50, 6*360,true);
			WaitForMotorToStop();
			Console.WriteLine("Position: " + ev3.MotorA.GetTachoCount());
			ev3.MotorA.On(-50, 9*360, true);
			WaitForMotorToStop ();
			Console.WriteLine("Position: " + ev3.MotorA.GetTachoCount());
			ev3.MotorA.MoveTo(50,0,true);
			WaitForMotorToStop();
			ev3.MotorA.Off();
			Console.WriteLine("Position: " + ev3.MotorA.GetTachoCount());
		}
		catch(Exception e){
			Console.WriteLine(e.StackTrace);
			Console.WriteLine("Error: " + e.Message);
			Console.WriteLine("Press any key to end...");
			Console.ReadKey();				
  		}
  		finally{
			ev3.Connection.Close();
  		}
  	}
  	
  	static void WaitForMotorToStop ()
	{
		Thread.Sleep(500);
		while(ev3.MotorA.IsRunning()){Thread.Sleep(50);}
	}
}
Creating a motor ramp profile

The step profile that is shown in the example makes the motor ramp up in 300 steps, run forward for 600 steps with speed 50, ramps down in 300 steps and brake when done. Profiles that are time based are also supported. See more in the MonoBrick communication library documentation

var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("usb");
ev3.Connection.Open();
ev3.MotorA.SpeedProfileStep(50,300,600,300,true);  
ev3.Connection.Close();
Synchronising motors

The example below shows how to synchronise Motor A and Motor D. If you need to synchronise motors to control a vehicle please see the vehicle section below.

var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("usb");
ev3.Connection.Open();
ev3.MotorSync.BitField = OutputBitfield.OutA | OutputBitfield.OutD;
ev3.MotorSync.On(50, 0);
System.Threading.Thread.Sleep(3000);
ev3.MotorSync.On(50, 50);//Motor A runs twice as fast as motor D 
System.Threading.Thread.Sleep(3000);
ev3.MotorSync.Brake();
System.Threading.Thread.Sleep(1000);
ev3.MotorSync.StepSync(-50,0,6*360,false);//Move to a position
ev3.Connection.Close();

Controlling a vehicle

This section shows you how to control a vehicle. The example below let’s you control a vehicle using the keyboard. When using this example on your own robot please make sure to change the direction of the motors to fit your needs.

using System;
using MonoBrick.EV3;
using System.Threading;
public static class Program{
	static void Main(string[] args)
	{
		var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("usb");
		try{
			ev3.Connection.Open();
			ev3.Vehicle.LeftPort = MotorPort.OutA;
			ev3.Vehicle.RightPort = MotorPort.OutD;
			ev3.Vehicle.ReverseLeft = false;
			ev3.Vehicle.ReverseRight = false;
			sbyte speed = 50;
			ConsoleKeyInfo cki;  
		    Console.WriteLine("Press T to quit");  
		    do   
		    {  
		        cki = Console.ReadKey(true); //press a key  
		        switch(cki.Key){    
		            case ConsoleKey.W:    
		                ev3.Vehicle.Forward(speed);
		            	break;                              
		            case ConsoleKey.S:     
		                ev3.Vehicle.Backward(speed);   
		            	break;    
		            case ConsoleKey.D:     
		                ev3.Vehicle.SpinRight(speed);    
		            	break;    
		            case ConsoleKey.A:     
		                ev3.Vehicle.SpinLeft(speed);    
		            	break;    
		            case ConsoleKey.Q:    
		                ev3.Vehicle.TurnLeftForward(speed, 50);    
		            	break;  
		            case ConsoleKey.E:    
		             	ev3.Vehicle.TurnRightForward(speed,50);       
		            break;  
		            case ConsoleKey.C:    
		                ev3.Vehicle.TurnRightReverse(speed,50);  
		            break;  
		            case ConsoleKey.Z:  
		                ev3.Vehicle.TurnLeftReverse(speed, 50);
		            break; 
		            case ConsoleKey.Spacebar:  
		                ev3.Vehicle.Off();
		            break; 
		        }  
		    } while (cki.Key != ConsoleKey.T);
		}
		catch(Exception e){
			Console.WriteLine(e.StackTrace);
			Console.WriteLine("Error: " + e.Message);
			Console.WriteLine("Press any key to end...");
			Console.ReadKey();				
  		}
  		finally{
			ev3.Connection.Close();
  		}
  	}
}

It is also possible to make the vehicle move a given number of steps as shown below.


ev3.Vehicle.Forward(50,12*360);

Reading sensors

The MonoBrick communication library supports the LEGO sensors that comes with both the EV3 retail and educational sets including the IR-Sensor, Color Sensor, Gyro Sensor and Temperature Sensor. In addition the LEGO sensors from the NXT 1.0 and 2.0 retail sets are also supported including the Light Sensor, Sound Sensor and Ultrasonic sensor. For the NXT a number of Hitechnic are also supported but these are not yet supported on the EV3. This guide only shows how to read sensor values using the EV3. For more information on all supported sensors for the EV3 and how to read sensor values with the NXT please consult the MonoBrick communication library documentation.

All sensors inherits from the sensor base class. The example below uses the base class to print out sensor values

using System;
using MonoBrick.EV3;
using System.Threading;
public static class Program{
	static void Main(string[] args)
	{
		var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("usb");
		ev3.Connection.Open();
		ev3.Sensor1 = new IRSensor(IRMode.Proximity);
		ev3.Sensor2 = new TouchSensor();
		ev3.Sensor3 = new ColorSensor(ColorMode.Color);
		ev3.Sensor4 = new GyroSensor(GyroMode.Angle);
		ConsoleKeyInfo cki;  
		Console.WriteLine("Press Q to quit");  
		do   
		{  
	        cki = Console.ReadKey(true); //press a key  
	        switch(cki.Key){    
	            case ConsoleKey.D1://1 is pressed    
	                Console.WriteLine("S1: " + ev3.Sensor1.ReadAsString());
	            	break;                              
	            case ConsoleKey.D2://2 is pressed    
	                Console.WriteLine("S2: " + ev3.Sensor2.ReadAsString());
	            	break;                              
	            case ConsoleKey.D3://3 is pressed    
	                Console.WriteLine("S3: " + ev3.Sensor3.ReadAsString());
	            	break;                              
	            case ConsoleKey.D4://4 is pressed    
	                Console.WriteLine("S4: " + ev3.Sensor4.ReadAsString());
	            	break;                              
	        }  
		} while (cki.Key != ConsoleKey.Q);
		ev3.Connection.Close();
  		
  	}
}
Using generics to set the sensor type

Using generics you can access sensor specific functions as shown in the example below. Here the touch sensor counts bumps and can be cleared by pressing R. The color value is evaluated to determine if the color is in fact green and finally the IR-Sensor is used in seek mode to locate the remote. IR seek mode only works on channel 0.

using System;
using MonoBrick.EV3;
using System.Threading;
public static class Program{
	static void Main(string[] args)
	{
		var ev3 = new Brick<TouchSensor,ColorSensor,IRSensor,Sensor> ("usb");
		ev3.Connection.Open();
		ev3.Sensor1.Mode = TouchMode.Count;
		ev3.Sensor2.Mode = ColorMode.Color;
		ev3.Sensor3.Mode = IRMode.Seek;
		ConsoleKeyInfo cki;  
		Console.WriteLine("Press Q to quit");  
		do   
		{  
	        cki = Console.ReadKey(true); //press a key  
	        switch(cki.Key){    
	            case ConsoleKey.R://C is pressed    
	                ev3.Sensor1.Reset();//reset the count
	                Console.WriteLine("Reset touch count");
	            	break;                              
	            case ConsoleKey.D1://1 is pressed    
	                Console.WriteLine("S1: " + ev3.Sensor1.ReadAsString());
	            	break;                              
	            case ConsoleKey.D2://2 is pressed    
	                Color color = ev3.Sensor2.ReadColor();
	                if(color == Color.Green){
	                	Console.WriteLine("Color is green");
	                	ev3.Beep(50,100);
	                }
	                else{
	                	Console.WriteLine("Color is " + color +  
	                	" and this is not green");
	                }
	                break;                              
	            case ConsoleKey.D3://3 is pressed    
	                int value  = ev3.Sensor3.Read();
	                if(value == 0){
	                	ev3.Beep(50,100);
	                	Console.WriteLine("Found target");
	                }
	                else{
	                	if(value < 0){
	                		Console.WriteLine("Target is to the left");
	                	}
	                	else{
	                		Console.WriteLine("Target is to the right");
	                	}
	                }
	            	break;                              
	        }  
		} while (cki.Key != ConsoleKey.Q);
		ev3.Connection.Close();
  	}
}
Auto detecting sensor type

The following example shows how to auto detect the sensor type and assign the sensor to the correct type. This makes it possible to print the value in the correct format.

using System;
using MonoBrick.EV3;
using System.Threading;
public static class Program{
	static void Main(string[] args)
	{
		var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("usb");
		ev3.Connection.Open();
		Sensor newSensor = null;
		string key = SensorHelper.TypeToKey(ev3.Sensor1.GetSensorType());
		if(SensorHelper.SensorDictionary.TryGetValue(key, out newSensor)){
			ev3.Sensor1 = newSensor;
		}
		ConsoleKeyInfo cki;  
		Console.WriteLine("Press Q to quit");  
		do   
		{  
	        cki = Console.ReadKey(true); //press a key  
	        switch(cki.Key){    
	            case ConsoleKey.D1://1 is pressed    
	                Console.WriteLine("S1: " + ev3.Sensor1.ReadAsString());
	            	break;                              
	        }  
		} while (cki.Key != ConsoleKey.Q);
		ev3.Connection.Close();
  		
  	}
}

Using the filesystem

With the filesystem it is possible to start and stop programs, get a list of files and playback sound files. Below some sample code for listing subfolders and files in the project folder is shown.

using System;
using MonoBrick.EV3;
using System.Threading;
public static class Program{
	static void Main(string[] args)
	{
		var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor>("usb");
		ev3.Connection.Open();
		BrickFile[] files;
		string[] subfolders;
		ev3.FileSystem.GetFolderInfo("/home/root/lms2012/prjs/", 
									out files, out subfolders);
		Console.WriteLine("Files:");
		foreach(var file in files){
			Console.WriteLine(file.Name);
		}
		Console.WriteLine(Environment.NewLine + "Subfolders:");
		foreach(var folder in subfolders){
			Console.WriteLine(folder);
		}
		ev3.Connection.Close();
  	}
}
Create a directory and write a file to the EV3
var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor<("usb");
ev3.Connection.Open();
ev3.FileSystem.CreateDirectory("/home/root/lms2012/prjs/MyPrograms");
ev3.FileSystem.WriteFile(@"C:\program.rbf",
                         "/home/root/lms2012/prjs/MyPrograms/program.rbf"); 
ev3.Connection.Close();
Recursively listing program files found in /home or its subfolders

This example require linq to be included in the project. In Visual Studio this is default while Mono Users will have to add a reference to System.Core

var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor<("usb");
ev3.Connection.Open();
var folderStructure = ev3.FileSystem.GetFolderStructure("/home");
var fileQuery = from structure in folderStructure.RunThroughFolders()
                from files in structure.FileList
                where files.FileType == MonoBrick.FileType.Program
                select files;
foreach(var file in fileQuery)
    Console.WriteLine(file.Name + " in " + file.Path);
ev3.Connection.Close();
Starting and stopping the first program found in /home or its subfolders

This example require linq to be included in the project. In Visual Studio this is default while Mono Users will have to add a reference to System.Core

var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor<("usb");
ev3.Connection.Open();
var folderStructure = ev3.FileSystem.GetFolderStructure("/home");
var fileQuery = from structure in folderStructure.RunThroughFolders()
                from files in structure.FileList
                where files.FileType == MonoBrick.FileType.Program
                select files;
ev3.StartProgram(fileQuery.First());
System.Threading.Thread.Sleep(5000);
ev3.StopProgram();
ev3.Connection.Close();

Miscellaneous

Sending data to the mailbox

To make the EV3 receive data from the mailbox a program must be running on the brick that listens for data from the mailbox. Use the download link to get a hold of the EV3 program shown below. The program simply takes the input string from the mailbox named "mailbox1" and prints it on the LCD.
ev3mailbox
To send text to the EV3 the following code can be used.

using System;
using MonoBrick.EV3;
using System.Threading;
public static class Program{
  static void Main(string[] args)
  {
      var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor<("usb");
      ev3.Connection.Open();
      ConsoleKeyInfo cki;
      Console.WriteLine("Press Q to quit");    
      do     
      {    
          cki = Console.ReadKey(true); //press a key    
          switch(cki.Key){
            case ConsoleKey.M:
                Console.WriteLine("String to send to mailbox");
                string input = Console.ReadLine();
                ev3.Mailbox.Send("mailbox1", input, false);
                break;                                 
          }    
      } while (cki.Key != ConsoleKey.Q);  
      ev3.Connection.Close();
  }
}
Controlling bricks connected in a daisychain
using System;
using MonoBrick.EV3;
using System.Threading;
public static class Program{
  static void Main(string[] args)
  {
      var ev3 = new Brick<Sensor,Sensor,Sensor,Sensor<("usb");
      ev3.Connection.Open();
      ev3.MotorA.DaisyChainLayer = DaisyChainLayer.First;
      ev3.MotorA.On(50);
      Thread.Sleep(2000);
      ev3.MotorA.Off();
      ev3.Sensor1.DaisyChainLayer = DaisyChainLayer.Second;
      Console.WriteLine(ev3.Sensor1.ReadAsString());
      ev3.Connection.Close();
  }
}
45 comments on “MonoBrick Communication Library Programming Guide
  1. Thank you so much. I will test this right now!

  2. Author Image Volker says:

    Hi Anders, i tried the Monobrick Test Project with EV3 Firmware V1.03H and Visual Studio. EV3 is connected by USB. When the HidApiNative.hid_open() is called, the Programm jumps to ConnectionException. Any Ideas where my Problem is ? If I use the Original Lego Software the Connection to the EV3 is no Problem.

    • Hi

      Thanks for trying out MonoBrick. I am guessing that you are on Windows! The hidapi.dll that comes with the test project needs to be in the same folder as the exe file that is compiled. The test project should copy this file to the output folder but maybe this is not the case. The outpur folder is normally c:\yourprojectfolder\bin\Debug or c:\yourprijectfolder\bin\Release depending on your configuration. Check that this folder contains the dll. Are you using Visual Studio?

      • Author Image Volker says:

        Hi,
        Yes I use Windows and Visual Studio. The Debug Folder contains the Dll. Still it does not work with USB Connection when it Comes to line
        device = HidApiNative.hid_open(this.vendorId, this.productId, IntPtr.Zero);
        The Programm jumps to ConnectionException (VendorID = 1684, productID = 5, Zero = 0) . Any Idea ?
        I tried Wifi Connection and then it works great. Really great Job what you did. Would be great if we couled solve the Problem with USB Connection. Another thing how can I print something on the Display, could not find the command ? Thanks a lot for your effort.

        • Author Image Volker says:

          I assume to print something on the Display I need to use the Mailbox example shown on this page.

        • Hi Again

          I just did some testing on my Windows machine and it seems that the problem with loading the dll is related to Visual Studio. From Xamarin Studio the test program runs fine. I will have a look at it tonight. In the meantime try to use Xamarin Studio.

        • I fixed the problem with the USB and Visual studio. In the usb.cs file find all the places where the hipapi dll is imported
          [DllImport(“hidapi)]
          and replace this with
          [DllImport(“hidapi”, CallingConvention = CallingConvention.Cdecl)]

          I will upload a new version of MonoBrick with the fix later tonight.

          Could you please confirm that this fixes the problem.

  3. Author Image Max says:

    For those getting the ‘incorrect format’ problem with the test solution in Visual Studio…
    Right click the Project in the project Explorer window and select Properties.
    IN the Properties window, select the Build tab
    Change the Platform Target to x86

    Sorted!

    • Thanks for pointing this out. I will upload a new test project tonight.

      • Author Image Volker says:

        Yes I still got exactly the same Problem ? Sorry but I have no Idea what to do. It works fine if I use Wifi.

        • Ok but what is you problem – could you please write the exception that you are getting. I just uploaded a new test project. I have tested this on two different machines at work. One that runs Visual Studio 2010 and another that runs Visual Studio 2012. Works like a charm on both…

          • Author Image Volker says:

            I use Visual Studio Express 2012. I tried on 2 Computers and always get “Error: Failed to open Connection”. I also get exactly the same error if I unplug the EV3 ? Looks like the program cannot find the EV3? Do I maybe have to specify somewhere a Com-Port or anything ? Any other sugestions how I could find out where the Problem is ?

          • Find out what exception you are getting. Go to the usb.cs file and find out what exception is thrown (and caught) when connection fails.

            Anders

  4. Author Image JpEncausse says:

    Hi,

    1. With a basic setup using LEGO application, it works

    2. With TestApplication I got the following error:

    at MonoBrick.HidLib`2.Open() in TestApplicationusb.cs:line 186
    at MonoBrick.USB`2.Open() in TestApplicationusb.cs:line 84
    at Program.Main(String[] args) in TestApplicationProgram.cs:line 9
    Error: Failed to open connection
    Press any key to end…

    I’m using:
    – Windows 7 64bit
    – Visual Studio Express 2012
    – dll files are copied in the debug folder
    – I changed compile option to x86

    Any Idea ?

    • First of all be sure not to be connected to the brick with the LEGO Software – you might even try to reboot your machine. If you are still not able to connect to the brick over USB try to locate the following code in USB.cs
      ///

      /// Open the connection
      ///

      public override void Open(){
      bool hasError = false;
      try
      {
      device = HidApiNative.hid_open(this.vendorId, this.productId, IntPtr.Zero);
      if(device != IntPtr.Zero){
      HidApiNative.hid_set_nonblocking(device,0);
      }
      else{
      hasError = true;
      }
      }
      catch(Exception e) {
      throw new ConnectionException(ConnectionError.OpenError, e);
      }
      if(hasError)
      throw new ConnectionException(ConnectionError.OpenError);
      isConnected = true;
      ConnectionWasOpened();
      }

      What is the value of the device pointer? Are you getting an exception when you call the hid_open function – if yes what exception are you getting?

      One last attempt could be to locate the hidapi.dll file in the lego software install directory. Copy/Replace that file with the one used by the MonoBrick communication library to see if it is a 64bit issue…

      That is all I can think off right now. Please let me know how it goes

      Anders

      • Author Image JpEncausse says:

        Well device == IntPtr.Zero so it set hasError to true.

        I bought the wifi dongle, configure it, replacing “usb” by “wifi” in C# but it do not works. Same issue.

        BTW, the wifi dongle is really crapy, I don’t understand LEGO, the Brick some times lost password after a reboot, the windows app is buggy, etc … was it the same with NTX ?

        I don’t understand why they don’t care the community

        • Hi Again

          Concerning USB:

          If you are getting a zero pointer and not an exception the DLL import of hidApi is working – are you sure nothing else is connected to the LEGO brick? Are you running as administrator? Anything else that you can think of that might prevent you from connecting to the EV3

          Concerning WiFi:

          Have you disabled your firewall? Is the EV3 connected to your WiFi network? Are you able to connect to the EV3 over WiFi using the LEGO Software? Can you ping the EV3?

          If you are able to connect to the EV3 using the Lego Software – then try connecting with MonoBrick while running Wireshark to see what is happening on the network. This guide will help you understand what you are suppose to see.

          One last question are you able to connect over Bluetooth?

          Anders

      • Author Image Frank says:

        Hello Anders,

        I have the same problem with the USB connection and after:
        device = HidApiNative.hid_open(this.vendorId, this.productId, IntPtr.Zero);
        is called in usb.cs
        I get the exception:

        System.Exception{System.BadImageFormatException}
        HRESULT: 0x8007000B
        “It was tried to load a file with a wrong format.”

        Do you have any solution?

        Regards
        -Frank

        • Hi there

          Please ask your question in the forum. Make sure that you don’t ask a question that has already been asked and please provide some information on what you did and what system you are using…

          /Anders

  5. Author Image Richard says:

    This is fantastic, I have already played quite a bit with this and love it so far! Just one thing – it looks like in the monobrick download the GyroSensor isn’t public. (I’m using Visual Studio on Windows).
    If I write ‘var gyro = new GyroSensor(GyroMode.Angle);’, I get no intellisense on GyroSensor and a compilation error ‘Error 3 ‘MonoBrick.EV3.GyroSensor’ is inaccessible due to its protection level ‘.
    It does show in the demo project though, so I’m guessing something is a little out of sync in the source?

    • Hi Richard – thanks for noticing. I will fix this later this weekend. However the problem is not that the versions are out of sync, since they come from the same build. The problem is that the Gyro class has no access modifier (it should of course be public). In .net the default access modifier for a class is Internal. This means that the Gyro class is only “visible”/public within the same assembly. Since the test project includes the source files and is therefore build when you build the test project, it is in the same assembly, hence the Gyro class is visible. When you use a reference for MonoBrick.dll you are trying to use the Gyro class located within the MonoBrick.dll assembly, and since your program is not in the same assembly as the MonoBrick.dll the Gyro class is not visible.

      Again thanks for pointing out this problem and I will upload a new release. In the meantime as you point out simply use the source files found in the test project as a workaround.

      Anders

  6. Author Image Sasanka Gottimukkala says:

    Hi Anders,

    I really appreciate your work. Its really wonderful. I currently using your library.

    I have a small question. I would like to know if it is possible to run the program on brick itself. I am asking this question since, I am trying to use your libraries for a robot maze competition. I am using Bluetooth for connectivity. It would have been really benifical (I know I am asking a lot) if I can transfer the entire program on to EV3. I hope I am making sense.

    Thanks once again,

    Best regards,
    Sasanka.

    • Hi Sasanka

      You are in luck. I am just about to release a firmware replacement for the EV3 that allows you to run on-brick programs written in C#/.NET. Please subscribe to facebook or twitter to get the latest news on when the firmware is released.

      Anders

  7. Author Image Jony says:

    Hi Anders, wonderful project! Could you describe the input and output data are transmitted and received from the controller NXT.

  8. Author Image Eduard says:

    Hello,

    I am looking for some joystick samples !!

    WKR,
    Eduard

  9. Author Image Milo says:

    Hi Anders, how can I read from Infrared Sensor Beacon Mode – Proximity? In mode Seek method read() reads only Data – Heading and I need read Data – Proximity (distance from Sensor to beacon)
    Thank You

  10. Author Image Carsten says:

    Hello,

    i got xamarin running, after a problem with copy the monoBrick.dll to the project.
    I opened a Csharp – Mono Projekt in Xamarin, right?

    Copy the example code above and in the Release Option i got a Code signing error, with debug it works, but how can i start that on the Brick, nothing happend after pressing “play”.

    Brick is connected via usb because bluetooth directly quit.

    How can i say xamarin to start the program on the Brick?
    Thanks

  11. Author Image Andi says:

    Hello man, why do you never say, that to use the communication libary, we don’t need to use your fireware, we need to use the normal fireware, we where trying now since more than hours and now we find that solution …
    Man this information would be very great :(((

  12. Author Image Sarang says:

    Can this library with with Visual C++? Or is it only compatible with Visual C#?

  13. Author Image Kees says:

    Hi,
    First of all, your communication library works fine! But now I’m trying to use the mailbox example. Problem is the EV3-program won’t run on my EV3-brick. When using the mailbox programming-block in receiving mode, the progamm halts on my brick (bluetooth connection is fine). What can be the problem?

    Greetings, Kees

  14. Thank you for this awesome code you have shared. I am using it to build a C# program and teach the kids a bit about programming and robotics.

    Just about everything works great for me, except one thing. I can not get the file system commands to work. It ‘sort of’ works, but it does not get all the files on the system. It seems to find all the sub directories just fine, but it fails to properly get some or all of the files from each sub directory.

    Also, of the files I do get, I am able to send command to start only one program; I can start the Demo.rpf that comes as the default program. However, my own programs (*.rbf) files do not play.

    I have tried two approaches: the Brick.Filesystem.GetFolderStructure function and writing my own recursive function using a call to Brick.FileSystem.GetFolderInfo( ). Neither one works without some defects?

    Does anyone have advice about possible problems and solutions with using file system commands?

    Thank you

    GB

Make a donation

Download



MailBox Sample