CCommand TransmitCMD(CCommand Cmd, byte Addr) { byte[] tmpBuffer = new byte[256]; int i = (Cmd.Data[2] != 0) ? (Cmd.Data)[2] : (((UInt16)(Cmd.Data)[4]) << 8) + (Cmd.Data)[5]; Cmd.SetByte(Addr, 1); UInt16 wCRC = CalculateCRC(Cmd.Data); Cmd.SetByte((byte)wCRC, i - 2); Cmd.SetByte((byte)(wCRC >> 8), i - 1); cmdOut = Cmd; int iErrCode = SendCommand(Cmd.Data, ref tmpBuffer); if ((iErrCode == 0) && (Cmd.Data[3] != 0) && (0xFF != Cmd.Data[3])) { wCRC = (UInt16)(tmpBuffer[((tmpBuffer[2] != 0) ? tmpBuffer[2] - 2 : (UInt16)((tmpBuffer)[4] << 8) + tmpBuffer[5] - 2)] + (tmpBuffer[((tmpBuffer[2] != 0) ? tmpBuffer[2] - 1 : (UInt16)(tmpBuffer[4] << 8) + tmpBuffer[5] - 1)] << 8)); if (CalculateCRC(tmpBuffer) != wCRC) { iErrCode = RE_CRC; } cmdIn = new CCommand(tmpBuffer, iErrCode, (tmpBuffer[2] != 0) ? (tmpBuffer)[2] : (((UInt16)(tmpBuffer)[4]) << 8) + (tmpBuffer)[5]); return(cmdIn); } cmdIn = new CCommand(tmpBuffer, iErrCode, 0); return(cmdIn); }
CCommand Transmit(CCommand CMD, byte Addr) { CCommand cmdRes = null; CCommand cmdACK = new CCommand(); for (int i = 0; i < 3; i++) { cmdRes = TransmitCMD(CMD, Addr); cmdACK.SetByte(SYNC, 0); cmdACK.SetByte(6, 2); cmdACK.SetByte(ACK, 3); if (cmdRes.Code == RE_NONE) { if ((ACK == cmdRes.Data[3]) && (cmdRes.Data[2] == 6)) { return(cmdRes); } if ((NAK == cmdRes.Data[3]) && (cmdRes.Data[2] == 6)) { if (iCmdDelay != 0) { Thread.Sleep(iCmdDelay);//5 } } else { cmdACK.SetByte(ACK, 3); TransmitCMD(cmdACK, Addr); if (iCmdDelay != 0) { Thread.Sleep(iCmdDelay);//5 } break; } } else { if (cmdRes.Code != RE_TIMEOUT) { cmdACK.SetByte(NAK, 3); TransmitCMD(cmdACK, Addr); if (iCmdDelay != 0) { Thread.Sleep(iCmdDelay);//5 } } } } return(cmdRes); }