Advertisement
Reisyukaku

USB HID driver

Aug 15th, 2014
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. using UsbHid;
  11. using UsbHid.USB.Classes.Messaging;
  12.  
  13. namespace Powersaves {
  14.  
  15.     public partial class Form1 : Form {
  16.  
  17.  
  18.         public UsbHidDevice Device;
  19.         private List<String> responses;
  20.         private StringBuilder strBuild;
  21.  
  22.         public Form1() {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private void Form1_Load(object sender, EventArgs e) {
  27.             Device = new UsbHidDevice(0x1C1A, 0x03D5);
  28.             Device.OnConnected += DeviceOnConnected;
  29.             Device.OnDisConnected += DeviceOnDisConnected;
  30.             Device.DataReceived += DeviceDataReceived;
  31.             Device.Connect();
  32.         }
  33.  
  34.         private void connectBut_Click(object sender, EventArgs e) {
  35.             responses = new List<String>();
  36.             strBuild = new StringBuilder();
  37.             new System.Threading.Thread(delegate() {
  38.                 if (!Device.IsDeviceConnected) return;
  39.  
  40.                 strBuild.Append("ROM ID:\n");
  41.                 Device.SendMessage(new CommandMessage(0x13, new byte[] { 0x08, 0x00, 0x00, 0x00, 0x71, 0xC9, 0x3F, 0xE9, 0xBB, 0x0A, 0x3B, 0x18 }));
  42.                 System.Threading.Thread.Sleep(20);
  43.                 Device.SendMessage(new CommandMessage(0x13, new byte[] { 0x08, 0x00, 0x04, 0x00, 0x90 }));
  44.                 System.Threading.Thread.Sleep(20);
  45.                 foreach (String val in responses) {
  46.                     strBuild.Append(val + "\n");
  47.                 }
  48.  
  49.                 strBuild.Append("NCCH Header:\n");
  50.                 Device.SendMessage(new CommandMessage(0x13, new byte[] { 0x08, 0x00, 0x00, 0x00, 0x3E }));
  51.                 System.Threading.Thread.Sleep(20);
  52.                 Device.SendMessage(new CommandMessage(0x14, new byte[] { 0x10, 0x00, 0x00, 0x02, 0x82 }));
  53.                 System.Threading.Thread.Sleep(20);
  54.                 foreach (String val in responses) {
  55.                     strBuild.Append(val + "\n");
  56.                 }
  57.                 strBuild.Append("-----------------------------");
  58.                 ThreadSafe(() => consoleBox.Text += strBuild.ToString());
  59.             }).Start();
  60.         }
  61.  
  62.         private static string ByteArrayToString(byte[] input) {
  63.             return string.Join(" ", input.Select(b => Convert.ToString(b, 16).ToUpper()));
  64.         }
  65.  
  66.         private void DeviceDataReceived(byte[] data) {
  67.             ThreadSafe(() => consoleBox.Text += "trying!!");
  68.             try {
  69.                 responses.Add(ByteArrayToString(data));
  70.             } catch (NullReferenceException e) {
  71.                 Console.Write(e.StackTrace);
  72.             }
  73.         }
  74.  
  75.         private void ThreadSafe(MethodInvoker method) {
  76.             if (InvokeRequired)
  77.                 Invoke(method);
  78.             else
  79.                 method();
  80.         }
  81.  
  82.         private void DeviceOnDisConnected() {
  83.             ThreadSafe(() => connectLabel.Text = "Disconnected.");
  84.  
  85.         }
  86.  
  87.         private void DeviceOnConnected() {
  88.             ThreadSafe(() => connectLabel.Text = "Connected!");
  89.         }
  90.  
  91.         private void consoleBox_TextChanged(object sender, EventArgs e) {
  92.  
  93.         }      
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement