7 years ago.

mbed callbacks and C#

Hi,

I have a small program in mbed running nice.

I would like to connect to the mbed device through a C# based application and register/hook to a function so I would be able to execute callbacks in C# when an event/function is called on the mbed application.

I'm sure there is a way to do this, I just couldn't find a decent example.

Anyone?

Gilad

3 Answers

7 years ago.

Probably easiest is to just use the virtual COM-port of the mbed (so the USB connector), when something needs to happen in your C# application the mbed sends a serial message which your program receives and acts on. Never used C#, but I expect that to be fairly straightforward. Instead of the virtual COM-port you can also use the LPC's USB pins, either also as serial connection or as USB HID connection.

4 years, 11 months ago.

Yes in C# you can keep listening to the serial port with a loop (but be aware is resource consuming). Then you serial facade can easily fire events that you will subscribe to in C#.

Dummy serial class

class SerialReader{
    
    event EventArgs<MyEventArg> MessageReceived;

   public SerialReader(){

      // init your com communication

      SerialPort mySerialPort = new SerialPort("COM1");

     // later an event callback

      mySerialPort.DataReceived += (sender, args) => {

            // if somebody subscribed

            if(MessageReceived != null){

                  // read the serial output

                  SerialPort sp = (SerialPort)sender;

                  string indata = sp.ReadExisting();

                  // raise an event

                  MessageReceived( new MyEventArgs

                                                         {

                                                             Message = data

                                                         });

            }

      }

   }

}

4 years, 11 months ago.

Just to add to what Erik and Raffaele have said already, you could use the HidLibrary which wraps around the Windows USB HID calls https://github.com/mikeobrien/HidLibrary or if you want to put yourself into a little more trouble, you could use LibUsbDotNet which is actually wrapping around the LibUSB library. http://libusbdotnet.sourceforge.net/V2/Index.html I have successfully used LibUsbDotNet in the past and all I had was to follow. Note that LibUsbDotNet can be used with the Mono Framework in Linux too (or Windows)