Пример #1
0
    // 关闭
    public void Close()
    {
        if (mIOHandler != IntPtr.Zero)
        {
            //if (Win32Usb.WaitForSingleObjectEx(mIOHandler, 0xffffff, true) == 0)
            {
                Win32Usb.CancelIo(mIOHandler);
                Win32Usb.CloseHandle(mIOHandler);
                mIOHandler = IntPtr.Zero;

                //Debug.Log("Marshal.GetLastWin32Error() = " + Marshal.GetLastWin32Error());
                /* mThreadLoopSend =*/ mThreadLoopRecive = false;

                //if (mThreadSend != null)
                //    mThreadSend.Join();

                if (mThreadRecive != null)
                {
                    mThreadRecive.Join();
                }

                if (EvtClosed != null)
                {
                    EvtClosed();
                }
            }
        }


        //if(!)//该函数会阻塞2秒,
        //{
        //    Debug.LogError("Close SerialThread Erro!");
        //}
    }
Пример #2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            m_hUsbEventHandle = Win32Usb.RegisterForUsbEvents(Handle, Win32Usb.HIDGuid);
            timer1.Enabled    = true;
            Random random = new Random();

            random.NextBytes(pass);
        }
Пример #3
0
 private void Form2_Load(object sender, EventArgs e)
 {
     this.Icon = Properties.Resources.MI;
     progressBar1.Visible = false;
     m_hUsbEventHandle = Win32Usb.RegisterForUsbEvents(Handle, Win32Usb.HIDGuid);
     toolStripStatusLabel5.Text = " ";
     timer1.Enabled = true;
     //this.Enabled = false;
 }
Пример #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            Win32Usb.UnregisterForUsbEvents(m_hUsbEventHandle);
            this.Hide();
            Form2 f = new Form2();

            f.ShowDialog();
            m_hUsbEventHandle = Win32Usb.RegisterForUsbEvents(Handle, Win32Usb.HIDGuid);
            ReHIDUI();
            this.Show();
        }
Пример #5
0
 private void toolStripButton5_Click(object sender, EventArgs e)
 {
     if (HIDDevice.TheHIDDevice == null)
     {
         MessageBox.Show("请连接设备");
         return;
     }
     Win32Usb.UnregisterForUsbEvents(m_hUsbEventHandle);
     BE_ToolForm f = new BE_ToolForm();
     f.ShowDialog();
     Form2_Load(null, null);
 }
Пример #6
0
 private void Form1_Load(object sender, EventArgs e)
 {
     //this.Text = "Mercedes Key Tool ---"+HIDDevice.SoftVersion.ToString("f2");
     this.Icon                  = Properties.Resources.MI;
     progressBar1.Visible       = false;
     m_hUsbEventHandle          = Win32Usb.RegisterForUsbEvents(Handle, Win32Usb.HIDGuid);
     toolStripStatusLabel2.Text = "No Device";
     timer1.Enabled             = true;
     button1.Visible            = false;
     button2.Visible            = false;
     button3.Visible            = false;
 }
Пример #7
0
 private void Form4_Load(object sender, EventArgs e)
 {
     this.Icon = Properties.Resources.MI;
     if (HIDDevice.TheHIDDevice != null)
     {
         toolStripStatusLabel1.Text = WFNetLib.Strings.StringsFunction.byteToHexStr(HIDDevice.TheHIDDevice.HexID, "");
     }
     else
     {
         this.Close();
     }
     progressBar1.Visible = false;
     m_hUsbEventHandle    = Win32Usb.RegisterForUsbEvents(Handle, Win32Usb.HIDGuid);
 }
Пример #8
0
    private static string GetDevicePath(IntPtr hInfoSet, ref Win32Usb.DeviceInterfaceData oInterface)
    {
        uint nRequiredSize = 0;

        // Get the device interface details
        if (!Win32Usb.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
        {
            Win32Usb.DeviceInterfaceDetailData oDetail = new Win32Usb.DeviceInterfaceDetailData();
            oDetail.Size = 5;   // hardcoded to 5! Sorry, but this works and trying more future proof versions by setting the size to the struct sizeof failed miserably. If you manage to sort it, mail me! Thx
            if (Win32Usb.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
            {
                return(oDetail.DevicePath);
            }
        }
        return(null);
    }
Пример #9
0
    public static string FindDevicePath(int nVid, int nPid)
    {
        //string strPath = string.Empty;
        string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); // first, build the path search string
        Guid   gHid      = Win32Usb.HIDGuid;
        //HidD_GetHidGuid(out gHid);	// next, get the GUID from Windows that it uses to represent the HID USB interface
        IntPtr hInfoSet = Win32Usb.SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, (uint)(Win32Usb.DIGCF_DEVICEINTERFACE | Win32Usb.DIGCF_PRESENT));   // this gets a list of all HID devices currently connected to the computer (InfoSet)

        try
        {
            Win32Usb.DeviceInterfaceData oInterface = new Win32Usb.DeviceInterfaceData();       // build up a device interface data block
            oInterface.Size = Marshal.SizeOf(oInterface);
            // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
            // to get device details for each device connected
            int nIndex = 0;
            while (Win32Usb.SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface)) // this gets the device interface information for a device at index 'nIndex' in the memory block
            {
                string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);                               // get the device path (see helper method 'GetDevicePath')

                if (strDevicePath.IndexOf(strSearch) >= 0)                                                    // do a string search, if we find the VID/PID string then we found our device!
                {
                    return(strDevicePath);
                }
                nIndex++;       // if we get here, we didn't find our device. So move on to the next one.
            }
        }
        catch (Exception ex)
        {
            Debug.LogWarning(string.Format("[警告]打开usbhid设备错误 :{0}WinError:{1:X8}", ex.ToString(), Marshal.GetLastWin32Error()));
            //Console.WriteLine(ex.ToString());
        }
        finally
        {
            // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
            Win32Usb.SetupDiDestroyDeviceInfoList(hInfoSet);
        }
        return("");      // oops, didn't find our device
    }
Пример #10
0
 private void Form4_FormClosing(object sender, FormClosingEventArgs e)
 {
     Win32Usb.UnregisterForUsbEvents(m_hUsbEventHandle);
 }
Пример #11
0
 private static string GetDevicePath(IntPtr hInfoSet, ref Win32Usb.DeviceInterfaceData oInterface)
 {
     uint nRequiredSize = 0;
     // Get the device interface details
     if (!Win32Usb.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
     {
         Win32Usb.DeviceInterfaceDetailData oDetail = new Win32Usb.DeviceInterfaceDetailData();
         oDetail.Size = 5;	// hardcoded to 5! Sorry, but this works and trying more future proof versions by setting the size to the struct sizeof failed miserably. If you manage to sort it, mail me! Thx
         if (Win32Usb.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
         {
             return oDetail.DevicePath;
         }
     }
     return null;
 }
Пример #12
0
    private void Thread_Recive()
    {
        uint readCount = 0;

        Win32Usb.ReadFile(mIOHandler, mReadBuffer, READ_BUFFER_SIZE, ref readCount, ref mReadOL);
        while (mThreadLoopRecive && mIOHandler != IntPtr.Zero)
        {
            try
            {
                //Debug.Log("d1," + mReadBuffer.Length+"  mReadBufCount="+mReadBufCount+" READ_BUFFER_SIZE="+READ_BUFFER_SIZE);

                //接收包
                if (                                                                             /*Win32Usb.WaitForSingleObjectEx(mIOHandler,0,true) ==0&&*/
                    Win32Usb.GetOverlappedResult(mIOHandler, ref mReadOL, ref readCount, false)) //有信号
                {
                    if (HandleSetupPackage != null)
                    {
                        byte[] paramDatas = new byte[readCount];
                        Array.Copy(mReadBuffer, paramDatas, readCount);
                        List <Package> packages = HandleSetupPackage(paramDatas);
                        //Debug.Log(ByteArrayToString(paramDatas));
                        if (packages.Count != 0)
                        {
                            Monitor.Enter(mRecivePack_ThreadLock);
                            foreach (Package p in packages)
                            {
                                mPackToReciveST.Add(p);
                            }
                            Monitor.Exit(mRecivePack_ThreadLock);
                        }
                    }


                    mReadOL.Clear();
                    Win32Usb.ReadFile(mIOHandler, mReadBuffer, READ_BUFFER_SIZE, ref readCount, ref mReadOL);
                }


                // 发送包
                mPackToSendST.Clear();
                List <Package> tempList = mPackToSendST;

                if (Monitor.TryEnter(mSendPack_ThreadLock))
                {
                    mPackToSendST = mPackToSendMT;
                    mPackToSendMT = tempList;
                    Monitor.Exit(mSendPack_ThreadLock);
                }

                uint numWrite = 0;
                foreach (Package dtw in mPackToSendST)
                {
                    mWriteOL.Clear();
                    //mWriteOL.Event = Win32Usb.CreateEvent(IntPtr.Zero, true, false, IntPtr.Zero);
                    byte[] writeData = new byte[mOutputReportLen];//可能会有内存泄露
                    Array.Copy(dtw.data, 0, writeData, 1, dtw.data.Length);

                    //if (!Win32Usb.WriteFile(mIOHandler, dtw.data, (uint)dtw.data.Length, ref numWrite, ref mWriteOL))
                    Win32Usb.WriteFile(mIOHandler, writeData, (uint)writeData.Length, ref numWrite, ref mWriteOL);


                    //if (Win32Usb.GetOverlappedResult(mIOHandler, ref mWriteOL, ref numWrite, true))
                    //{
                    //    //Debug.Log(ByteArrayToString(dtw.data) + "  numWrite = " + numWrite);
                    //}
                    //Win32Usb.WaitForSingleObjectEx(mWriteOL.Event, 0xffffffff, true);
                    //Debug.Log("11");
                }
            }
            catch (System.TimeoutException)
            {
            }
            catch (System.Exception ex)
            {
                Debug.LogError("SerialThreadUpdate Exception:" + ex.Message);
            }
        }
    }
Пример #13
0
    /// <summary>
    /// 打开 portNo串口号
    /// </summary>
    /// <param name="portNo">串口号</param>
    /// <returns>是否打开成功</returns>
    public bool Open()
    {
        if (mIOHandler != IntPtr.Zero)
        {
            return(true);
        }
        InitVals();
        try
        {
            string devicePath = FindDevicePath(VID, PID);

            if (devicePath == null || devicePath == "")
            {
                return(false);
            }


            mIOHandler = Win32Usb.CreateFile(devicePath, Win32Usb.GENERIC_READ | Win32Usb.GENERIC_WRITE
                                             , Win32Usb.FILE_SHARE_READ | Win32Usb.FILE_SHARE_WRITE, IntPtr.Zero, Win32Usb.OPEN_EXISTING, Win32Usb.FILE_FLAG_OVERLAPPED, IntPtr.Zero);

            if (mIOHandler != Win32Usb.InvalidHandleValue)      // if the open worked...
            {
                IntPtr lpData;
                if (Win32Usb.HidD_GetPreparsedData(mIOHandler, out lpData))     // get windows to read the device data into an internal buffer
                {
                    try
                    {
                        Win32Usb.HidCaps oCaps;
                        Win32Usb.HidP_GetCaps(lpData, out oCaps);        // extract the device capabilities from the internal buffer
                        //mInputReportLen = oCaps.InputReportByteLength;	// get the input...
                        mOutputReportLen = oCaps.OutputReportByteLength; // ... and output report lengths
                        //Debug.Log(string.Format("mInputReportLen = {0} mOutputReportLen = {1}",mInputReportLen,mOutputReportLen));
                        //m_oFile = new FileStream(m_hHandle, FileAccess.Read | FileAccess.Write, true, m_nInputReportLength, true);
                        //m_oFile = new FileStream(new SafeFileHandle(m_hHandle, false), FileAccess.Read | FileAccess.Write, m_nInputReportLength, true);

                        //BeginAsyncRead();	// kick off the first asynchronous read
                    }
                    catch (Exception)
                    {
                        //throw HIDDeviceException.GenerateWithWinError("Failed to get the detailed data from the hid.");
                        Debug.LogWarning("[HidUsb]得不到hid报文长度.");
                        return(false);
                    }
                    finally
                    {
                        Win32Usb.HidD_FreePreparsedData(ref lpData);    // before we quit the funtion, we must free the internal buffer reserved in GetPreparsedData
                    }
                }
                else    // GetPreparsedData failed? Chuck an exception
                {
                    Debug.LogWarning("[HidUsb]获取不到hid属性.");
                    return(false);
                }
            }
            else        // File open failed? Chuck an exception
            {
                mIOHandler = IntPtr.Zero;
                Debug.LogWarning("[HidUsb]打不开hid设备.");
                return(false);
            }


            /*mThreadLoopSend = */ mThreadLoopRecive = true;

            mReadBuffer = new byte[READ_BUFFER_SIZE];

            //mThreadSend = new Thread(Thread_Send);
            //mThreadSend.Start();

            mThreadRecive = new Thread(Thread_Recive);
            mThreadRecive.Start();


            if (EvtOpened != null)
            {
                EvtOpened();
            }
            return(true);
        }
        catch (System.Exception e)
        {
            Debug.LogWarning("Open Serialport Error." + e);
            return(false);
        }
    }
Пример #14
0
 private void Form1_Load(object sender, EventArgs e)
 {
     m_hUsbEventHandle = Win32Usb.RegisterForUsbEvents(Handle, Win32Usb.HIDGuid);
     timer1.Enabled    = true;
 }
Пример #15
0
 protected override void OnHandleCreated(EventArgs e)
 {
     Win32Usb.RegisterForUsbEvents(this.Handle, Win32Usb.HIDGuid);
 }
Пример #16
0
 protected override void OnHandleCreated(EventArgs e)
 {
     base.OnHandleCreated(e);
     usbEventHandle = Win32Usb.RegisterForUsbEvents(Handle, deviceClassGuid);
 }