Advertisement
Guest User

ReadWrite class

a guest
Nov 27th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.53 KB | None | 0 0
  1. public class ReadWrite
  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)
  24.                 // it will have an IUsbDevice interface. If not (WinUSB) the
  25.                 // variable will be null indicating this is an interface of a
  26.                 // device.
  27.                 IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
  28.                 if (!ReferenceEquals(wholeUsbDevice, null))
  29.                 {
  30.                     // This is a "whole" USB device. Before it can be used,
  31.                     // the desired configuration and interface must be selected.
  32.  
  33.                     // Select config #1
  34.                     wholeUsbDevice.SetConfiguration(1);
  35.  
  36.                     // Claim interface #0.
  37.                     wholeUsbDevice.ClaimInterface(0);
  38.                 }
  39.  
  40.                 // open read endpoint 1.
  41.                 UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
  42.  
  43.                 // open write endpoint 1.
  44.                 UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02);
  45.  
  46.                 // Remove the exepath/startup filename text from the begining of the CommandLine.
  47.                 string cmdLine = Regex.Replace(
  48.                     Environment.CommandLine, "^\".+?\"^.*? |^.*? ", "", RegexOptions.Singleline);
  49.  
  50.                 if (!String.IsNullOrEmpty(cmdLine))
  51.                 {
  52.                     int bytesWritten;
  53.                     ec = writer.Write(Encoding.Default.GetBytes(cmdLine), 2000, out bytesWritten);
  54.                     if (ec != ErrorCode.None) throw new Exception(UsbDevice.LastErrorString);
  55.  
  56.                     byte[] readBuffer = new byte[1024];
  57.                     while (ec == ErrorCode.None)
  58.                     {
  59.                         int bytesRead;
  60.  
  61.                         // If the device hasn't sent data in the last 100 milliseconds,
  62.                         // a timeout error (ec = IoTimedOut) will occur.
  63.                         ec = reader.Read(readBuffer, 100, out bytesRead);
  64.  
  65.                         if (bytesRead == 0) throw new Exception("No more bytes!");
  66.  
  67.                         // Write that output to the console.
  68.                         Console.WriteLine("Writing output to console");
  69.                         Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
  70.                     }
  71.  
  72.                     Console.WriteLine("\r\nDone!\r\n");
  73.                 }
  74.                 else
  75.                     throw new Exception("Nothing to do.");
  76.             }
  77.             catch (Exception ex)
  78.             {
  79.                 Console.WriteLine();
  80.                 Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
  81.             }
  82.             finally
  83.             {
  84.                 if (MyUsbDevice != null)
  85.                 {
  86.                     if (MyUsbDevice.IsOpen)
  87.                     {
  88.                         // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
  89.                         // it exposes an IUsbDevice interface. If not (WinUSB) the
  90.                         // 'wholeUsbDevice' variable will be null indicating this is
  91.                         // an interface of a device; it does not require or support
  92.                         // configuration and interface selection.
  93.                         IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
  94.                         if (!ReferenceEquals(wholeUsbDevice, null))
  95.                         {
  96.                             // Release interface #0.
  97.                             wholeUsbDevice.ReleaseInterface(0);
  98.                         }
  99.  
  100.                         MyUsbDevice.Close();
  101.                     }
  102.                     MyUsbDevice = null;
  103.  
  104.                     // Free usb resources
  105.                     UsbDevice.Exit();
  106.  
  107.                 }
  108.  
  109.                 // Wait for user input..
  110.                 Console.ReadKey();
  111.             }
  112.         }
  113.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement