/// <summary> /// 同步发送数据 /// </summary> /// <param name="cBaseCommand">传入通信命令</param> /// <returns>成功返回true,否则返回false</returns> public override CommunicationState SendSymetric(CBase cSendCommand, CBase cDecodeCommand) { int nLen = 0; if (!isUSBAvailable) { ProcessError(cSendCommand, "USB is unavailable !"); return(CommunicationState.CommSendError); } byte[] bBuf = cSendCommand.encode(); int nTotalDataLength = bBuf.Length; if (cDecodeCommand == null) { cDecodeCommand = cSendCommand; } lock (this) { try { if (!outEndpoint.XferData(ref bBuf, ref nTotalDataLength)) { Thread.Sleep(_delayms * 3); if (inEndpoint.XferData(ref _receiveBufTemp, ref nTotalDataLength)) { bBuf = cSendCommand.encode(); nTotalDataLength = bBuf.Length; Thread.Sleep(_delayms * 3); if (!outEndpoint.XferData(ref bBuf, ref nTotalDataLength)) { ProcessError(cSendCommand, "Re-send data to usb error!" + GetUSBStatus()); return(CommunicationState.CommSendError);; } } else { ProcessError(cSendCommand, "Send data to usb error and cannot re-read the data from usb error!" + GetUSBStatus()); return(CommunicationState.CommSendError);; //send data error. } } else { if (nTotalDataLength <= 0) { ProcessError(cSendCommand, "Send data to usb error,the send byte is zero!" + GetUSBStatus()); return(CommunicationState.CommSendError);; //send data error. } // 在log里记录发送的命令 if (IsCommandNeedRecordLog(cDecodeCommand)) { RecordCommandLog(bBuf, bBuf.Length, true); } } } catch (System.Exception ex) { return(CommunicationState.CommSendError); } Thread.Sleep(_delayms); int nTimes = 0; ReadAsysnchonous(); //while (readDataSynchronous2(cSendCommand, out nLen)) //{ // // 在log里记录接收的命令 // if (IsCommandNeedRecordLog(cDecodeCommand)) // { // RecordCommandLog(_receiveBuf, nLen, false); // } // bBuf = BitConverter.GetBytes(nLen); // Array.Copy(bBuf, 0, _receiveBuf, _receiveBuf.Length - 5, 4);//write data length at end // cDecodeCommand.decode(_receiveBuf); // if (cDecodeCommand.ErrorCode == (ushort)ReturnCode.CommandDecodeError) // { // nTimes++; // } // else if (cDecodeCommand.ErrorCode == (ushort)ReturnCode.CommandReceivedError) // { // return CommunicationState.CommReceivedError; // } // else if (cDecodeCommand.ErrorCode == (ushort)ReturnCode.CommandReject) // { // return CommunicationState.CommReject; // } // else // { // return CommunicationState.CommOK; // } // if (nTimes >= 9) // { // ProcessError(cDecodeCommand, "This command decode error,and it have re-try 9 times read,and it also error !"); // return CommunicationState.CommDecodeError; // } // Thread.Sleep(_delayms); //} } return(CommunicationState.CommTimeOut); }