现有一个HID 的usb设备,PC端不定时发送控制信号,定时(16次/s)接受信号。
如果用timer接收信号,程序没有问题。流程如下:
 initUSB(查找设备、打开设备获得句柄、获得报文长度等)------打开timer---writeFile----writeFile
                                                            ----,定时读取这样是没有问题的。
尝试到thread中接收信号,发现writeFile和readFile都会返回 句柄无效的错误。
 initUSB(查找设备、打开设备获得句柄、获得报文长度等)-----startThread --writeFile
                                                           ---无限循环读取
但是如果在主线程的writeFile之前  或者在readFile线程中加一断点,调试程序就没有问题。各位大神,有没有人知道这是什么问题?附:creatFile 以 独占/共享模式打开都会这样。
    creatFile  FILE_FLAG_OVERLAPPED  重叠IO   writeFile和 readFile也都是重叠IO

解决方案 »

  1.   


            private HidDevice hidd = new HidDevice();      public bool InitHA100()
            {
                if (hidd.Online )
                    this.CloseHA100();            
                if (!this.initUsb())//this.Open())
                {
                    Util.Logs.WriteLog(this.path, "open comm error!");
                    return false;
                }            for (int i = 0; i < this.buffer.Length; i++)
                    buffer[i] = 0;
     
                hidd.writeData(new byte[2] { Convert.ToByte("0"), Convert.ToByte("1") }, 2);
                Sleep(50);
                int result = 0;
                result = hidd.writeData(new byte[2] { Convert.ToByte("0"), Convert.ToByte("1") }, 2); 
                Sleep(50);
                if (result > 0)   //result =0则success =5挂起  否则 错误
                {
                    result = hidd.writeData(new byte[2] { Convert.ToByte("0"), Convert.ToByte("1") }, 2); //SendImmediate(0x01);
                }
                bool isOK = false;
                for (int i = 0; i < 50; i++)
                {
                    bool b2 = true;
                    Sleep(200);
                    if (this.buffer[0] == 0 && this.buffer[1] == 0 && this.buffer[2] == 0 && this.buffer[3] == 0)     //检查接收信息
                        b2 = false; //system on chip doesn't connect
                    if (result ==0  && b2)
                    {
                        isOK = true;
                        break;
                    }
                    else
                        continue;
                }
                if (!isOK) CloseHA100();
                return isOK;
            }
            public bool initUsb()
            {
                if (hidd.openDevice())
                {
                    hidd.openHandles();                 readThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.rThread));
                     readThread.Priority = System.Threading.ThreadPriority.AboveNormal;
                    readThread.Start();                return true;
                }
                else return false;
            }        private void rThread()
            {
                int readStatus = 0;
                while(true)
                {
                    byte[] readBuffer = new byte[0];
                    readStatus = hidd.readData(ref readBuffer);
                    foreach (byte readstr in readBuffer)
                    {
                        OnRxChar(readstr);
                    }
                    System.Threading.Thread.Sleep(100);
                }        }
      

  2.   


    我加 logs  记录  writeFile和 readFile 返回之 false    getLasterror的值为6,无效句柄。。如果加断点 就成功执行了。。