日期:2014-05-18  浏览次数:20995 次

usb通讯时readfile没有响应。
上位机和单片机使用标准usb协议通讯,上位机的相关代码:
C# code

//这里的详细代码没有给出。
openDevice();
openHandles();


//重点:
        int result = FileIOApiDeclarations.WriteFile(this._WriteHandle,ref command[0], command.Length,ref TxdBytes, 0);
                if (result == 0)
                {
                    int error = Usb.DebuggingDeclarations.GetLastError();
                    string str = "";
                    long size = 0;
                    Usb.DebuggingDeclarations.FormatMessage(Usb.DebuggingDeclarations.FORMAT_MESSAGE_ALLOCATE_BUFFER | Usb.DebuggingDeclarations.FORMAT_MESSAGE_FROM_SYSTEM | Usb.DebuggingDeclarations.FORMAT_MESSAGE_IGNORE_INSERTS, ref size, error, 0, str, 0, 0);
                    Log.WriteLog("error when set read command" + str);
                    this.closeHandles();
                    return retResult;
                }
                //FileIOApiDeclarations.CloseHandle(this._WriteHandle);
                int relReaded = 0;
                Usb.FileIOApiDeclarations.OVERLAPPED overlapped = new Usb.FileIOApiDeclarations.OVERLAPPED();
                this.PrepareForOverlappedTransfer(ref overlapped);
                System.Threading.Thread.Sleep(500);
                result = FileIOApiDeclarations.ReadFile(this._ReadHandle, ref data[0], length, ref relReaded, ref overlapped);
                /*[color=#FF0000]错误出现在此处,每次到这里程序完全没有响应了,拔掉USB设备时程序下行,GetLastError会返回31
                 前边的获取GUID,writeFlie都是正确的  [/color]*/
                if (result == 0) // error handle
                {
                    int error = Usb.DebuggingDeclarations.GetLastError();
                    if (error == FileIOApiDeclarations.ERROR_IO_PENDING)
                    {
                        FileIOApiDeclarations.WaitForSingleObject(overlapped.hEvent, 30000);
                        int readBytes = 0;
                        result = FileIOApiDeclarations.GetOverlappedResult(this._ReadHandle, ref overlapped, ref readBytes, false);
                        if (result != 0)
                            retResult = true;
                    }




单片机相关代码:
C/C++ code

      init_USBused();
      init_TimerA();
         init_TimerB();
         RWsw = P2IN & BIT5;        // for USB Write Data
         if(RWsw == 0)
        {
            FCTL2 = FWKEY + FSSEL0 + FN0;
            FlashErase(ipatient);
            FlashErase(isetup);
        }    

      MCU_D12RST_0;                             //片选usb
      _NOP();
      MCU_D12RST_1;                             //reset usb 芯片

      bEPPflags.value = 0;
      _EINT();
      reconnect_USB();
      P1IE = 0X04;
      opcode = 0xff;
      index = 0;
    

      while(1)
      {
          if (bEPPflags.bits.ep2_rxdone)              //端点2接收响应
          {
            bEPPflags.bits.ep2_rxdone = 0;            //flag set zero

            if(opcode == 0xff)
            {
              //D12_ReadEndpoint(4, 6, EpBuf);
              if(EpBuf[0] == 0x01)//if(EpBuf[0] == 0x01)
              {
                opcode = 1;
                addr = EpBuf[2]*0x100 + EpBuf[3];
                length = EpBuf[4]*0x100 + EpBuf[5];
                cur_addr = addr;
                fileoff = 0;
              }
              else if(EpBuf[0] == 0x00)
              {
                opcode = 0;
                addr = EpBuf[2]*0x100 + EpBuf[3];
                length = EpBuf[4]*0x100 + EpBuf[5];
                cur_addr = addr;
                fileoff = 0;
              }
             }

            else if(opcode == 1)                            //将usb接到的数据写入寄存器           
           {
              if(index == 256)
               {
                index = 0;
                for(ptr = 0; ptr < 256