Go Down

Topic: Using LibUsbDotNet to drive Arduino (Read 2563 times) previous topic - next topic


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:

Code: [Select]

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

Code: [Select]

* 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 = Serial.read();

  analogWrite(ledPin, inputByte);

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


Thanks for any help

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131