Пример #1
0
 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);
 }
Пример #2
0
        public bool CmdWrite(byte[] dat, ref byte status)
        {
            uint a = 0;

            byte[] array = new byte[dat.Length + 2];
            array[0] = 0x21;
            array[1] = 0x09;
            Array.Copy(dat, 0, array, 2, dat.Length);
            var iret = FIO.WriteFile(m_intPtrUSB, array, (uint)array.Length, ref a, 0);

            //iret = iret;
            //return true;

            //if (!HID.HidD_SetFeature(m_intPtrUSB, array, (uint)array.Length)) return false;
            Thread.Sleep(5);

            /*
             * var  array2 = new byte[dat.Length + 2]
             * array2[0] = 0x21;
             * array2[1] = 0x09;
             * for (int i = 0; i < array2.Length; i++) array2[i] = 0;
             * int j;
             * for (j = 0; j < CUSB.__USB_RETRY_COUNT__; j++)
             * {
             *              if (HID.HidD_GetFeature(m_intPtrUSB, array2, (uint)array2.Length))
             *              {
             *                              status = array2[7];
             *                              return true;
             *              }
             *              HID.HidD_FlushQueue(m_intPtrUSB);
             *              Thread.Sleep(5);
             * }
             * if (j == CUSB.__USB_RETRY_COUNT__) throw new Exception(CMessages.GetLastWin32ErrorToString());
             *
             * // */
            return(true);
        }
Пример #3
0
        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());
        }
Пример #4
0
        public static string[] EnumPorts(UInt16 vid, UInt16 pid)
        {
            var arrayList       = new List <string>();
            var hidDevInterface = CUSB.GetHidDevInterface(vid, pid);

            if (hidDevInterface == null)
            {
                return(arrayList.ToArray());
            }
            for (int i = 0; i < hidDevInterface.Length; i++)
            {
                var intPtr = FIO.CreateFile(hidDevInterface[i].Trim(), FIO.GENERIC_READ, 3u, IntPtr.Zero, 3u, 0u, IntPtr.Zero);
                if (intPtr.ToInt32() == -1)
                {
                    continue;
                }
                //var a = "";
                //if (CUSB.GetVersion(intPtr, ref a)) if (a == "UF1.0")
                arrayList.Add(hidDevInterface[i]);

                FIO.CloseHandle(intPtr);
            }
            return(arrayList.ToArray());
        }
Пример #5
0
 public void Close()
 {
     FIO.CloseHandle(m_intPtrUSB);
 }