Advertisement
Guest User

Read class

a guest
Nov 27th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. public class ReadOnly
  2.     {
  3.         public static UsbDevice MyUsbDevice;
  4.  
  5.         #region SET YOUR USB Vendor and Product ID!
  6.  
  7.         public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0x8564, 0x1000);
  8.  
  9.         #endregion
  10.  
  11.         public static void Operation()
  12.         {
  13.             ErrorCode ec = ErrorCode.None;
  14.  
  15.             try
  16.             {
  17.                 // Find and open the usb device.
  18.                 MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
  19.  
  20.                 // If the device is open and ready
  21.                 if (MyUsbDevice == null) throw new Exception("Device Not Found.");
  22.  
  23.                 // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
  24.                 // it exposes an IUsbDevice interface. If not (WinUSB) the
  25.                 // 'wholeUsbDevice' variable will be null indicating this is
  26.                 // an interface of a device; it does not require or support
  27.                 // configuration and interface selection.
  28.                 IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
  29.                 if (!ReferenceEquals(wholeUsbDevice, null))
  30.                 {
  31.                     // This is a "whole" USB device. Before it can be used,
  32.                     // the desired configuration and interface must be selected.
  33.  
  34.                     // Select config #1
  35.                     wholeUsbDevice.SetConfiguration(1);
  36.  
  37.                     // Claim interface #0.
  38.                     wholeUsbDevice.ClaimInterface(0);
  39.                 }
  40.  
  41.                 // open read endpoint 1.
  42.                 UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
  43.  
  44.  
  45.                 byte[] readBuffer = new byte[1024];
  46.                 while (ec == ErrorCode.None)
  47.                 {
  48.                     int bytesRead;
  49.  
  50.                     // If the device hasn't sent data in the last 5 seconds,
  51.                     // a timeout error (ec = IoTimedOut) will occur.
  52.                     ec = reader.Read(readBuffer, 5000, out bytesRead);
  53.  
  54.                     if (bytesRead == 0) throw new Exception(string.Format("{0}:No more bytes!", ec));
  55.                     Console.WriteLine("{0} bytes read", bytesRead);
  56.  
  57.                     // Write that output to the console.
  58.                     Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
  59.                 }
  60.  
  61.                 Console.WriteLine("\r\nDone!\r\n");
  62.             }
  63.             catch (Exception ex)
  64.             {
  65.                 Console.WriteLine();
  66.                 Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
  67.             }
  68.             finally
  69.             {
  70.                 if (MyUsbDevice != null)
  71.                 {
  72.                     if (MyUsbDevice.IsOpen)
  73.                     {
  74.                         // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
  75.                         // it exposes an IUsbDevice interface. If not (WinUSB) the
  76.                         // 'wholeUsbDevice' variable will be null indicating this is
  77.                         // an interface of a device; it does not require or support
  78.                         // configuration and interface selection.
  79.                         IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
  80.                         if (!ReferenceEquals(wholeUsbDevice, null))
  81.                         {
  82.                             // Release interface #0.
  83.                             wholeUsbDevice.ReleaseInterface(0);
  84.                         }
  85.  
  86.                         MyUsbDevice.Close();
  87.                     }
  88.                     MyUsbDevice = null;
  89.  
  90.                     // Free usb resources
  91.                     UsbDevice.Exit();
  92.  
  93.                 }
  94.  
  95.                 // Wait for user input..
  96.                 Console.ReadKey();
  97.             }
  98.         }
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement