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; } }
// 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(); }
public bool startAsyncOperation(ushort MyDevVID, ushort MyDevPID) { //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 = -1; for (int i = 0; i < devices.Length; i++) { if ((devices[i].VID == MyDevVID) && (devices[i].PID == MyDevPID)) { selectedDeviceIndex = i; } } if (selectedDeviceIndex == -1) { return(false); } 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 "true" boolean in the constructor tells the class we want asynchronous operation this time device = new HIDDevice(devicePath, true); //OR, the normal usage when you know the VID and PID of the device //HIDDevice device = new HIDDevice(VID, PID, (ushort)SN, true); //next create the event handler for the incoming reports device.dataReceived += new HIDDevice.dataReceivedEvent(device_dataReceived); //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) //The write method is identical to the synchronous mode of operation byte[] writeData = new byte[64]; for (int i = 0; i < writeData.Length; i++) { writeData[i] = Convert.ToByte(i + 100); } device.write(writeData); //Its that easy!! //Send your program off to do other stuff here, wait for UI events etc //When a report occurs, the device_dataReceived(byte[] message) method will be called //System.Threading.Thread.Sleep(100); //close the device to release all handles etc //device.close(); return(true); }
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 = "没有连接触盘"; } }
public void sendData(USB_ConfigReport config) { if (connected) { byte[] writeData = encodeConfigReport(config); try { device.write(241, writeData); // F1 = 241 output reportid } catch (Exception) { Console.WriteLine("Cant send data!"); } } }
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; } } } } } }
// Used to communicate with the MCU. Can send bytes through USB public void SendReport(byte[] Report) { device.write(Report); }
void Send_HID_Data_PC_to_MCU() { byte[] writeData = new byte[64]; //for (int i = 0; i < writeData.Length; i++) //{ // writeData[i] = Convert.ToByte(i + 100); //} writeData[0] = 0x55; if (ckReceiveData.Checked) { writeData[1] = 1; } else { writeData[1] = 0; } if (ckLED1.Checked) { writeData[2] = 1; } else { writeData[2] = 0; } if (ckLED2.Checked) { writeData[3] = 1; } else { writeData[3] = 0; } if (ckLED3.Checked) { writeData[4] = 1; } else { writeData[4] = 0; } writeData[5] = Convert.ToByte(tBarServo.Value); int MyINTVal = 0; if (txtU32ValOut.Text != "") { MyINTVal = Convert.ToInt32(txtU32ValOut.Text); } writeData[6] = (byte)(MyINTVal & 0xff); writeData[7] = (byte)((MyINTVal >> 8) & 0xff); writeData[8] = (byte)((MyINTVal >> 16) & 0xff); writeData[9] = (byte)((MyINTVal >> 24) & 0xff); float MyFloatVal = 0; if (txtfloatValOut.Text != "") { MyFloatVal = float.Parse(txtfloatValOut.Text, CultureInfo.InvariantCulture.NumberFormat); } byte[] FloatBytes = BitConverter.GetBytes(MyFloatVal); writeData[10] = FloatBytes[0]; writeData[11] = FloatBytes[1]; writeData[12] = FloatBytes[2]; writeData[13] = FloatBytes[3]; device.write(writeData); }