public void CmdRead(ref byte[] dat) { byte[] array = new byte[8]; byte[] array2 = new byte[8]; array[0] = 1; array[1] = 1; Array.Copy(dat, 0, array, 2, dat.Length); for (int i = 0; i < array2.Length; i++) { array2[i] = 0; } array2[0] = 1; array2[1] = 1; int j; for (j = 0; j < CUSB.__USB_RETRY_COUNT__; j++) { if (HID.HidD_SetFeature(m_intPtrUSB, array, (uint)array.Length) && HID.HidD_GetFeature(m_intPtrUSB, array2, (uint)array2.Length)) { Array.Copy(array2, 2, dat, 0, dat.Length); break; } HID.HidD_FlushQueue(m_intPtrUSB); } if (j == CUSB.__USB_RETRY_COUNT__) { throw new Exception(CMessages.GetLastWin32ErrorToString()); } }
public bool DataWrite(byte[] dat) { //Kernel32.wri byte[] array = new byte[8], array2 = new byte[8]; array[0] = 1; array[1] = 2; Array.Copy(dat, 0, array, 2, dat.Length); for (int i = 0; i < array2.Length; i++) { array2[i] = 0; } array2[0] = 1; array2[1] = 2; if (!HID.HidD_SetFeature(m_intPtrUSB, array, (uint)array.Length)) { return(false); } Thread.Sleep(5); int j; for (j = 0; j < CUSB.__USB_RETRY_COUNT__; j++) { if (HID.HidD_GetFeature(m_intPtrUSB, array2, (uint)array2.Length)) { return(true); } HID.HidD_FlushQueue(m_intPtrUSB); Thread.Sleep(5); } if (j == CUSB.__USB_RETRY_COUNT__) { throw new Exception(CMessages.GetLastWin32ErrorToString()); } return(false); }
private static string[] GetAllHIDInterFace() { var arrayList = new List <string>(); try { var sYSTEM_INFO = default(Kernel32.SYSTEM_INFO); Kernel32.GetSystemInfo(out sYSTEM_INFO); bool flag = sYSTEM_INFO.sysInfoEx.wProcessorArchitecture == 9 || sYSTEM_INFO.sysInfoEx.wProcessorArchitecture == 6; Guid guid = default(Guid); HID.HidD_GetHidGuid(ref guid); var sP_DEVINFO_DATA = default(DM.SP_DEVINFO_DATA); sP_DEVINFO_DATA.cbSize = Marshal.SizeOf(sP_DEVINFO_DATA); var deviceInfoSet = DM.SetupDiGetClassDevs(ref guid, null, 0, 18u); if (!Environment.Is64BitOperatingSystem) { throw new Exception(CMessages.GetLastWin32ErrorToString()); } else if (-1L == deviceInfoSet.ToInt64()) { throw new Exception(CMessages.GetLastWin32ErrorToString()); } uint num = 0; while (DM.SetupDiEnumDeviceInfo(deviceInfoSet, num, ref sP_DEVINFO_DATA)) { var sP_DEVICE_INTERFACE_DATA = default(DM.SP_DEVICE_INTERFACE_DATA); sP_DEVICE_INTERFACE_DATA.cbSize = Marshal.SizeOf(sP_DEVICE_INTERFACE_DATA); uint num2 = 0; while (DM.SetupDiEnumDeviceInterfaces(deviceInfoSet, ref sP_DEVINFO_DATA, ref guid, num2++, ref sP_DEVICE_INTERFACE_DATA)) { int num3 = 0; if (!DM.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref sP_DEVICE_INTERFACE_DATA, IntPtr.Zero, 0, ref num3, IntPtr.Zero) && 122 != Marshal.GetLastWin32Error()) { throw new Exception(CMessages.GetLastWin32ErrorToString()); } int num4 = 0; var intPtr = Marshal.AllocHGlobal(num3); Marshal.WriteInt32(intPtr, flag ? 8 : (Marshal.SizeOf(0u) + Marshal.SystemDefaultCharSize)); if (!DM.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref sP_DEVICE_INTERFACE_DATA, intPtr, num3, ref num4, IntPtr.Zero)) { throw new Exception(CMessages.GetLastWin32ErrorToString()); } var ptr = new IntPtr(Environment.Is64BitOperatingSystem ? intPtr.ToInt64() + (long)Marshal.SizeOf(0u) : intPtr.ToInt32() + Marshal.SizeOf(0u)); arrayList.Add(Marshal.PtrToStringAuto(ptr)); Marshal.FreeHGlobal(intPtr); } num += 1; } DM.SetupDiDestroyDeviceInfoList(deviceInfoSet); } catch (Exception e) { throw e; } return(arrayList.ToArray()); }
public void Open(string strPortName) { m_intPtrUSB = FIO.CreateFile(strPortName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (-1 == m_intPtrUSB.ToInt32()) { throw new Exception(CMessages.GetLastWin32ErrorToString()); } HID.HidD_SetNumInputBuffers(m_intPtrUSB, 0x40); }
private static string[] GetHidDevInterface(ushort uVendorId, ushort uProductId) { string[] allHIDInterFace; try { allHIDInterFace = CUSB.GetAllHIDInterFace(); if (allHIDInterFace == null || allHIDInterFace.Length == 0) { return(null); } } catch { return(null); } var arrayList = new List <string>(); for (int i = 0; i < allHIDInterFace.Length; i++) { var intPtr = FIO.CreateFile(allHIDInterFace[i], FIO.GENERIC_READ, 3u, IntPtr.Zero, 3u, 0u, IntPtr.Zero); if (-1 == intPtr.ToInt32()) { var err = CMessages.GetLastWin32ErrorToString(); //Console.WriteLine(err); continue; } var hIDD_ATTRIBUTES = default(HID.HIDD_ATTRIBUTES); hIDD_ATTRIBUTES.Size = (uint)Marshal.SizeOf(hIDD_ATTRIBUTES); if (HID.HidD_GetAttributes(intPtr, ref hIDD_ATTRIBUTES) && uVendorId == hIDD_ATTRIBUTES.VendorID && uProductId == hIDD_ATTRIBUTES.ProductID) { arrayList.Add(allHIDInterFace[i]); } FIO.CloseHandle(intPtr); } return(arrayList.ToArray()); }