Quantcast
Channel: Objective Development Forums
Viewing all articles
Browse latest Browse all 4524

Re: Noob Confusion....

$
0
0
Hi

I followed this tutorial: http://codeandlife.com/2012/01/22/avr-a ... al-part-1/
It is not C# but it gave me good start at understanding how the basic USB works (Control Messages). - See my (Roelf) comment on the third part of this tutorial if you do not understand the direction thing

I have started writing my own custom c# class for my own use. I will share it, but keep in mind I do not code for a living and all judgments on my coding style and methods should be educational and not just criticism.

USBClass.cs
using System;
using System.Collections.Generic;
using LibUsbDotNet;
using LibUsbDotNet.Info;
using LibUsbDotNet.Main;

namespace USBTest
{
    /// <summary>
    /// The general class to obtain the TinyCon devices.
    /// </summary>
    static class TinyConUSBClass
    {
        const int VENDORID = 0x16C0;
        const int PRODUCTID = 0x05DC;

        public static List<TinyConDeviceClass> TinyConDevices = new List<TinyConDeviceClass>();

        /// <summary>
        /// Gets all the connected TinyCon devices and store them in TinyConDevices
        /// </summary>
        /// <returns>void</returns>
        public static void GetUSBDevices()
        {
            //Find devices matching the vendor and product ID
            UsbDeviceFinder USBFinder = new UsbDeviceFinder(VENDORID, PRODUCTID);
            //Using the registry create a list of those devices
           
            UsbRegDeviceList USBRegistryDevices = UsbDevice.AllDevices.FindAll(USBFinder);

            foreach (UsbRegistry USBReg in USBRegistryDevices)
            {
               TinyConDevices.Add(new TinyConDeviceClass(USBReg));
            }
        }

        /// <summary>
        /// Closes all the TinyCon devices and exits the USB driver
        /// </summary>
        public static void ExitUSB()
        {
            foreach (TinyConDeviceClass TinyCon in TinyConDevices)
                TinyCon.Close();

            UsbDevice.Exit();
        }
    }

    /// <summary>
    /// Using my own USB Request type as libusbdotnet's version seems to be incomplete
    /// </summary>
    public enum USBRequestType{
        RECIPIENT_DEVICE = 0,
        RECIPIENT_INTERFACE = 1,
        RECIPIENT_ENDPOINT = 2,
        RECIPIENT_OTHER = 3,
        TYPE_STANDARD = 0,
        TYPE_CLASS = 32,
        TYPE_VENDOR = 64,
        TRANSFER_DIRECTION_OUT = 0,
        TRANSFER_DIRECTION_IN = 128
    }

    /// <summary>
    /// The class for individual TinyCon devices.
    /// </summary>
    class TinyConDeviceClass
    {
        private UsbRegistry _USBRegistry;
        private UsbDevice _USBDevice = null;
        private char[] _USBBuffer = new char[64];

        private enum TinyConRequest
        {
            LED_STATE = 1
        }

        /// <summary>
        /// Is the TinyCon device open. (Read-only)
        /// </summary>
        public bool IsOpen
        {
            get
            {
                if (_USBDevice == null)
                    return false;
                return _USBDevice.IsOpen;
            }
        }

        /// <summary>
        /// Read TinyCon serial number. (Read-only)
        /// </summary>
        public string GetSerialNumber
        {
            get
            {
                if (_USBDevice == null)
                    return "Device not open.";
                return _USBDevice.Info.SerialString;
            }
        }

        public TinyConDeviceClass(UsbRegistry USBRegistry)
        {
            _USBRegistry = USBRegistry;
        }

        /// <summary>
        /// Open the TinyCon device.
        /// </summary>
        /// <returns>bool</returns>
        public bool Open()
        {
            if (!_USBRegistry.Open(out _USBDevice))
                return false;

            if (_USBDevice == null)
                return false;

            IUsbDevice wholeUsbDevice = _USBDevice 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
                wholeUsbDevice.SetConfiguration(1);

                // Claim interface #0.
                wholeUsbDevice.ClaimInterface(0);
            }

            return true;
        }

        /// <summary>
        /// Set the LED1 State.
        /// </summary>
        /// <param name="State">bool</param>
        /// <returns>bool</returns>
        public bool LEDState(bool State)
        {
            //object and length can be null/0 if no data packet is involved.
            UsbSetupPacket SetupPacket = new UsbSetupPacket((byte)(USBRequestType.RECIPIENT_DEVICE | USBRequestType.TYPE_VENDOR), (byte)TinyConRequest.LED_STATE, (short)(State ? 1 : 0), 0, 0);
            int LengthTransfered = 0;

            if (_USBDevice == null)
                return false;

            return _USBDevice.ControlTransfer(ref SetupPacket, null, 0, out LengthTransfered);
        }

        /// <summary>
        /// Closes the TinyCon device.
        /// </summary>
        public void Close()
        {
            if (_USBDevice != null)
            {
                _USBDevice.Close();
                _USBDevice = null;
            }
        }
    }
}



Please note the class is not complete, but it will give you an idea of what to to.

As for implementation:

TinyConUSBClass.GetUSBDevices();

if (TinyConUSBClass.TinyConDevices.Count > 0)
{
    TinyConUSBClass.TinyConDevices[0].Open();
     Console.WriteLine("Serial Number: " + TinyConUSBClass.TinyConDevices[0].GetSerialNumber);
     TinyConUSBClass.TinyConDevices[0].LEDState(true);
}
//No need to call  TinyConUSBClass.TinyConDevices[0].Close(); as ExitUSB closes all devices
TinyConUSBClass.ExitUSB();


As you will note the class is set up so that if you have multiple same devices plugged into one host, they are easily enumerated.

PS Thinking of making the TinyConUSBClass not static and singleton, then I could implement a destructor that would call USBExit() automatically.

Viewing all articles
Browse latest Browse all 4524

Trending Articles