public static void FindNanoPad() { if (UtilValues.PublicValue.nanoPads.Length != 0) { Array.Clear(UtilValues.PublicValue.nanoPads, 0, UtilValues.PublicValue.nanoPads.Length); Array.Resize(ref UtilValues.PublicValue.nanoPads, 0); } HIDDevice.interfaceDetails[] Pads = HIDDevice.getConnectedPads(); foreach (HIDDevice.interfaceDetails Pad in Pads) { SayobotNanoPad pad = new SayobotNanoPad { devicePath = Pad.devicePath, ProductID = Pad.PID }; pad.ReadingPadSettings(); HIDDevice PadDevice = new HIDDevice(pad.devicePath, false); byte[] writeData = CalcSHA(new byte[] { 0x02, 0x00, 0x00 }); PadDevice.write(writeData); System.Threading.Thread.Sleep(100); byte[] readData = PadDevice.read(); if (readData[1] == 0) { pad.OSVersion = readData[4].ToString(); } else { pad.OSVersion = "Unknown"; } writeData = CalcSHA(new byte[] { 0x02, 0x08, 0x01, 0x00 }); PadDevice.write(writeData); System.Threading.Thread.Sleep(100); readData = PadDevice.read(); if (readData[0] != 0xff) { byte[] nameEncode = new byte[readData[2]]; Array.Copy(readData, 3, nameEncode, 0, readData[2]); nameEncode = ChangeToSystemUnicodeEncoding(nameEncode); pad.Name = Encoding.Unicode.GetString(nameEncode); } else { pad.Name = Pad.product; } PadDevice.close(); Array.Resize(ref UtilValues.PublicValue.nanoPads, UtilValues.PublicValue.nanoPads.Length + 1); UtilValues.PublicValue.nanoPads[UtilValues.PublicValue.nanoPads.Length - 1] = pad; } }
static void ReadDJHDevice(string DevicePath) { // Create handle to the device HIDDevice DJH = new HIDDevice(DevicePath, false); // Read bytes & map DJH DJHControllerMap Map = new DJHControllerMap(); while (true) { byte[] DJHBytes = DJH.read(); // Map Map.Buttons = DJHBytes[1]; Map.DPAD = DJHBytes[3]; Map.PS = DJHBytes[2]; Map.TT_normal = DJHBytes[7]; Map.Filter_normal = DJHBytes[20]; Map.Filter_step = DJHBytes[21]; Map.Slider_normal = DJHBytes[22]; Map.Slider_step = DJHBytes[23]; // Print Console.Write("\rButton: {0}\t DPAD: {1}\t PS: {2}\t TT: {3}\t Filter: {4}|{5}\tSlider: {6}|{7}\t", Map.Buttons, Map.DPAD, Map.PS, Map.TT_normal, Map.Filter_normal, Map.Filter_step, Map.Slider_normal, Map.Slider_step); // Report vJoy UpdateGamepad(Map); } }
// Apologies for the repeated code, however i feel it provides a better demonstration // of the functionality of this code. public void useSynchronousOperation() { //Get the details of all connected USB HID devices HIDDevice.interfaceDetails[] devices = HIDDevice.getConnectedDevices(); //Arbitrarily select one of the devices which we found in the previous step //record the details of this device to be used in the class constructor int selectedDeviceIndex = 0; ushort VID = devices[selectedDeviceIndex].VID; ushort PID = devices[selectedDeviceIndex].PID; int SN = devices[selectedDeviceIndex].serialNumber; string devicePath = devices[selectedDeviceIndex].devicePath; //create a handle to the device by calling the constructor of the HID class //This can be done using either the VID/PID/Serialnumber, or the device path (string) //all of these details are available from the HIDDevice.interfaceDetails[] struct array created above //The "false" boolean in the constructor tells the class we only want synchronous operation HIDDevice device = new HIDDevice(devicePath, false); //OR, the normal usage when you know the VID and PID of the device //HIDDevice device = new HIDDevice(VID, PID, (ushort)SN, false); //Write some data to the device (the write method throws an exception if the data is longer than the report length //specified by the device, this length can be found in the HIDDevice.interfaceDetails struct) byte[] writeData = { 0x00, 0x01, 0x02, 0x03, 0x04 }; device.write(writeData); //Its that easy!! //Read some data synchronously from the device. This method blocks the calling thread until the data //is returned. This takes 1-20ms for most HID devices byte[] readData = device.read(); //again, that easy! //close the device to release all handles etc device.close(); }
private void button1_Click(object sender, EventArgs e) { if (OpenedPad != null) { OpenedPad.write(PadDevUtil.StringToByte(skinTextBox1.Text)); System.Threading.Thread.Sleep(100); skinTextBox2.Text = PadDevUtil.ToHexString(OpenedPad.read()); } else { skinTextBox2.Text = "没有连接触盘"; } }
protected bool receiveThread() { if (!isDeviceConnect()) { return(true); } const int MaxRecvCount = 32; if (mRecvBytes == null) { mRecvBytes = new byte[MaxRecvCount]; } // 只接收23个字节,如果不是23个字节,则丢弃该数据 int readCount = mHIDDevice.read(ref mRecvBytes, 23); if (readCount != 0) { BinaryUtility.memmove(ref mRecvBytes, 0, 1, MaxRecvCount - 1); readCount -= 4; // 去除第一个字节和最后3个字节 mInputBufferLock.waitForUnlock(); addDataToInputBuffer(mRecvBytes, readCount); mInputBufferLock.unlock(); } // 先同步发送列表 mSendPacketLock.waitForUnlock(); int count = mSendPacket.Count; for (int i = 0; i < count; ++i) { mOutputBufferList.Add(mSendPacket[i].toBytes()); } mSendPacket.Clear(); mSendPacketLock.unlock(); // 发送所有需要发送的数据 int sendCount = mOutputBufferList.Count; for (int i = 0; i < sendCount; ++i) { if (mOutputBufferList[i] != null) { mHIDDevice.write(mOutputBufferList[i]); } } mOutputBufferList.Clear(); return(true); }
public void ReadingPadSettings() { HIDDevice Pad = new HIDDevice(devicePath, false); //读取按键信息 const int countKey = 6; padKeys = new PadKeys[countKey]; for (byte keyNo = 0; keyNo < countKey; keyNo++) { byte[] sendInformation = { 02, 06, 02, 00, keyNo }; Pad.write(StaticUtilFunctions.CalcSHA(sendInformation)); System.Threading.Thread.Sleep(100); byte[] receiveInformation = Pad.read(); byte ControlKey = receiveInformation[7]; byte CharKey = receiveInformation[8]; this.padKeys[keyNo].charKeys = (PadSetConstValue.Keys.CharKeys)CharKey; //解析Control按键 for (byte exp = 7; exp >= 0; exp--) { if (ControlKey >= (1 << exp)) { ControlKey -= (byte)(1 << exp); this.padKeys[keyNo].controlKeys.SetValue(true, exp); } } } //读取灯光信息 const int countLight = 5; padLights = new PadLights[countLight]; for (byte lightNo = 0; lightNo < countLight; lightNo++) { byte[] sendInformation = { 02, 07, 02, 00, lightNo }; Pad.write(StaticUtilFunctions.CalcSHA(sendInformation)); System.Threading.Thread.Sleep(100); byte[] receiveInformation = Pad.read(); byte LightData = receiveInformation[7]; this.padLights[lightNo].Speed = (PadSetConstValue.Light.Speed)(LightData << 6); byte LightMode = (byte)(LightData - (LightData << 6)); if (LightMode >= 0x3a) { padLights[lightNo].Mode = PadSetConstValue.Light.Mode.Unknown; padLights[lightNo].ColorMode = PadSetConstValue.Light.ColorMode.Unknown; padLights[lightNo].CustomColors = new Color[0]; } else { var PadSystemLightModes = Enum.GetValues(typeof(PadSetConstValue.Light.Mode)); for (int tmp = PadSystemLightModes.Length; tmp > 0; tmp--) { if (LightMode > (byte)PadSystemLightModes.GetValue(tmp - 1)) { padLights[lightNo].Mode = (PadSetConstValue.Light.Mode)PadSystemLightModes.GetValue(tmp - 1); try { padLights[lightNo].ColorMode = (PadSetConstValue.Light.ColorMode) LightMode - (byte)padLights[lightNo].Mode; } catch (Exception) { padLights[lightNo].ColorMode = PadSetConstValue.Light.ColorMode.Unknown; } } } } } }