Using LibUsbDotNet to drive Arduino

Hi there, I'm trying to use C#.Net to deliver serial data to my Arduino.

I've written a simple sketch that turns an LED on when it gets 222 and off when it receives 0.

I can use C# to send to the Arduino if I know the Com number of the connection.

I'm trying to take this up a level and use the correct USB methods instead of Com ports. However, I can't get the code to work.

I've installed LibUsbDotNet and set the drivers to Filter on my Arduino drivers.

Is there anyone who can advise what I'm doing wrong.

Here is my C# code:

using LibUsbDotNet;
using LibUsbDotNet.Main;

namespace Examples
    internal class ReadWrite
        public static UsbDevice MyUsbDevice;

        public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(9025, 1);

        public static void Main(string[] args)
            ErrorCode ec = ErrorCode.None;

                // Find and open the usb device.
                MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

                // If the device is open and ready
                if (MyUsbDevice == null) throw new Exception("Device Not Found.");

                IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                    // This is a "whole" USB device. Before it can be used, 
                    // the desired configuration and interface must be selected.

                    // Select config #1

                    // Claim interface #0.

                // open read endpoint 1.
                UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);

                // open write endpoint 1.
                UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);

                int bytesWritten = 1;

                int value = 255;

                byte[] buffer = new byte[1];
                buffer[0] = Convert.ToByte(value);
                ec = writer.Write(buffer, 2000, out bytesWritten);

And here is my sketch

 * USB Test 
 * This is to see if I can send data to the USB port of the UNO 

int ledPin = 9;
int count = 1;

int inputByte;

void setup()
  pinMode(ledPin, OUTPUT);

void loop()

  //Read Buffer
  if (Serial.available() > 0) 
      inputByte =;

  analogWrite(ledPin, inputByte);

  //Set LED state
  if(inputByte == 255)
      digitalWrite(ledPin, HIGH);
  if(inputByte == 0)
      digitalWrite(ledPin, LOW);


Thanks for any help Richard

you can do this in C# :

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports;

namespace LED1 {

public partial class Form1 : Form { System.IO.Ports.SerialPort ArduinoPort; //define el puerto serial de entrada.

public Form1() { InitializeComponent();

//Crea el serial port ArduinoPort = new System.IO.Ports.SerialPort(); ArduinoPort.PortName = "COM9"; // puerto del Arduino. ArduinoPort.BaudRate = 9600; ArduinoPort.Open(); //abre serial del Arduino.

//Vincular eventos de los botones this.FormClosing += Form1_FormClosing; //cierra el puerto Arduino cuando termina la clase this.btOFF.Click += btOFF_Click; // cuando se presiona el boton de apagado this.btON.Click += btON_Click; //cuando se presiona el boton de encendido } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { //Cerrar el puerto serial if (ArduinoPort.IsOpen) ArduinoPort.Close(); }

private void btON_Click(object sender, EventArgs e) { ArduinoPort.Write("b"); // escribe una b si se oprime el boton ON }

private void btOFF_Click(object sender, EventArgs e) { ArduinoPort.Write("a"); //escribe una a si se oprime el boton OFF } } }

Code Arduino:

const int led=13;

void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(led,OUTPUT); }

void loop() { // put your main code here, to run repeatedly: if (Serial.available() >0) { int option =; if (option == 'a') { digitalWrite(led,LOW); } if (option == 'b') { digitalWrite(led, HIGH); } } }

this maybe help you hehe xD

Capitan_EO9: this maybe help you hehe xD

The OP is 6 years old ! If he is on any type of schedule, he would be way over due! :roll_eyes: