public IOPH_DCP(ADTRecord adtRec, IAppMainWindow mainWin) { _adtRec = adtRec; _mainWin = mainWin; _teleFromWin = new Telegram(GlobalServices.maxCommandDataLen); _teleToWin = new Telegram(GlobalServices.maxReplyDataLen); _replyData = new DataBuffer(GlobalServices.maxReplyDataLen); _isSeries = false; _seriesExecStat = SeriesExecutionState.Stop; _taskState = TaskState.TaskReady_OK; int num = int.Parse(ConfigurationManager.AppSettings["DCPMaxDataLen"]); int num2 = int.Parse(ConfigurationManager.AppSettings["DCPMsgRepeatNr"]); int num3 = int.Parse(ConfigurationManager.AppSettings["DCPRecTimeout"]); byte num4 = byte.Parse(ConfigurationManager.AppSettings["DCPSOFByte"]); _dcpProtocolEngine = new DCPProtocol(this, num, num2, num3, 10, num4); _DCPRXMsg = new DCPFrame(num, num4); _DCPAnswerMsg = new DCPFrame(num, num4); _DCPTXMsg = new DCPFrame(num, num4); _DCPTXBuf = new DCPFrame(num, num4); if (adtRec.isUsbFtdiDevice()) { _ddi = DDI_USB.instance(); } else if (adtRec.isRS232Device()) { _ddi = DDI_RS232.instance(); } else if (adtRec.isTESTDevice()) { _ddi = DDI_TEST.instance(); } else if (adtRec.isHIDDevice()) { _ddi = DDI_HID.instance(); } else { GlobalServices.ErrMsg("IOPH_DCP.IOPH_DCP()", "FATAL ERROR: Unknown I/O port type, exit WDS!"); Application.Exit(); } _iot = new Thread(new ThreadStart(doDeviceIO)); _iot.IsBackground = true; _iot.Name = "IOPH_DCP thread"; _isTelegramRequest = false; _iot.Start(); }
public DCPRetCodes AnswerToDCPMessage(DataBuffer a_data, MessageTypes a_msgType, CommandSet a_cmdSet, DCPFrame a_txMsg, DCPFrame a_rxMsg) { DataBuffer buffer = new DataBuffer(a_data.NumItems + 4); byte num = (byte) a_msgType; buffer = a_data; switch (num) { case 0x40: case 0x80: a_txMsg.CmdSet = (byte) a_cmdSet; a_txMsg.SeqNum = a_rxMsg.SeqNum; break; } int num2 = 0; while (num2 < _DCPMsgRepeatNr) { DCPRetCodes codes; if (_dcpVersion == DCPVersion.DCP) { codes = a_txMsg.ConstructDCPmsg(buffer, a_cmdSet, a_msgType); } else { codes = a_txMsg.ConstructDCP2msg(buffer, a_cmdSet, a_msgType); } codes = doExchangeDCP_TX(a_txMsg, a_rxMsg); switch (codes) { case DCPRetCodes.DCP_RESP_SENT: return codes; case DCPRetCodes.DCP_FATAL: return codes; } return DCPRetCodes.DCP_FATAL; } return DCPRetCodes.DCP_FATAL; }
private DCPRetCodes doExchangeDCP_TX(DCPFrame a_dcpTxMsg, DCPFrame a_dcpRxMsg) { int dueTime = _DCPRecTimeout; DataBuffer dataToSend = new DataBuffer(a_dcpTxMsg.Data.Length + 4); DCPRetCodes codes = a_dcpTxMsg.CopyDCPmsgToDataBuffer(dataToSend); int num2 = 0; if (_devInterface != null) num2 = _devInterface.WriteDevice(dataToSend); if (num2 != -1) { if ((a_dcpTxMsg.MsgType == 0x40) || (a_dcpTxMsg.MsgType == 0x80)) return DCPRetCodes.DCP_RESP_SENT; _isSOFTimeOut = false; _rxTimer = new System.Threading.Timer(new TimerCallback(rxSOFTimerTickHandler), null, dueTime, -1); while (!_isSOFTimeOut) if (checkIfSOFArrived() == DCPRetCodes.DCP_OK) { if (_rxTimer != null) { _rxTimer.Dispose(); _rxTimer = null; } return doRxDCP(dueTime, a_dcpRxMsg); } if (_rxTimer != null) { _rxTimer.Dispose(); _rxTimer = null; } } return DCPRetCodes.DCP_FATAL; }
private DCPRetCodes checkIncomingMessage(DCPFrame dcpRxMsg) { int len = dcpRxMsg.Len; if (len > 2) len -= 3; else return DCPRetCodes.DCP_ERR_EDC; dcpRxMsg.EDC = dcpRxMsg.Data[len]; dcpRxMsg.Data[len] = 0; byte num2 = (byte) (dcpRxMsg.Len ^ dcpRxMsg.Ctrl); for (len = 0; len < (dcpRxMsg.Len - 2); len++) num2 ^= dcpRxMsg.Data[len]; if (dcpRxMsg.EDC != num2) return DCPRetCodes.DCP_ERR_EDC; return DCPRetCodes.DCP_OK; }
public DCPRetCodes exchangeDCPMessage(DataBuffer a_data, MessageTypes a_msgType, CommandSet a_cmdSet, DCPFrame a_txMsg, DCPFrame a_rxMsg) { MessageTypes synchReqOrIndication; DataBuffer buffer = new DataBuffer(a_data.NumItems + 4); MessageTypes types = synchReqOrIndication = a_msgType; buffer = a_data; TaskState writingCommand = TaskState.WritingCommand; switch (synchReqOrIndication) { case MessageTypes.ACK: case MessageTypes.Nack: return DCPRetCodes.DCP_FATAL; } for (int i = 0; i < _DCPMsgRepeatNr; i++) { DCPRetCodes codes; switch (_dcpVersion) { case DCPVersion.DCP2: codes = a_txMsg.ConstructDCP2msg(buffer, a_cmdSet, synchReqOrIndication); break; default: codes = a_txMsg.ConstructDCPmsg(buffer, a_cmdSet, synchReqOrIndication); break; } codes = doExchangeDCP_TX(a_txMsg, a_rxMsg); switch (codes) { case DCPRetCodes.DCP_RESP_SENT: return codes; case DCPRetCodes.DCP_FATAL: return codes; case DCPRetCodes.DCP_ERR_TIMEOUT: case DCPRetCodes.DCP_ERR_EDC: break; default: if (codes != DCPRetCodes.DCP_OK) return DCPRetCodes.DCP_FATAL; if (a_rxMsg.SeqNum == a_txMsg.SeqNum) { if ((a_rxMsg.MsgType != 0) && (a_rxMsg.MsgType != 0xc0)) { if (a_rxMsg.MsgType != 0x80) { if (a_rxMsg.MsgType != 0x40) return DCPRetCodes.DCP_FATAL; if (writingCommand != TaskState.SynchReq) { if (a_rxMsg.Len > 2) return DCPRetCodes.DCP_OK_ACK; return DCPRetCodes.DCP_OK; } synchReqOrIndication = types; buffer = a_data; writingCommand = TaskState.WritingCommand; } else { if (a_rxMsg.CmdSet != 0) return DCPRetCodes.DCP_OK_NACK; if (a_rxMsg.Data[0] != 0) return DCPRetCodes.DCP_OK_NACK; } } } else { writingCommand = TaskState.SynchReq; synchReqOrIndication = MessageTypes.SynchReqOrIndication; buffer.NumItems = 0; } break; } } return DCPRetCodes.DCP_FATAL; }
public DCPRetCodes doRxDCP(int initialTimeout, DCPFrame dcpRxMsg) { int num2 = 1; _messageByteIndex = 0; int num3 = 0; DataBuffer buf = new DataBuffer(_DCPMaxDataLen) { NumItems = 0 }; _isPacketTimeOut = false; tmrPacketTimer = new System.Timers.Timer(); tmrPacketTimer.Interval = initialTimeout; tmrPacketTimer.Elapsed += new ElapsedEventHandler(tmrPacketTimer_Elapsed); tmrPacketTimer.Start(); while (!_isPacketTimeOut && (num2 > 0)) { int num; int num5; int num6; if (_terminateRequested) return DCPRetCodes.DCP_TERMINATED; switch (_messageByteIndex) { case 0: { num = 0; if (_devInterface != null) num = _devInterface.ReadAppendDevice(buf, 3); if (num < 0) { buf.NumItems = 0; return DCPRetCodes.DCP_FATAL; } if (num == 0) Thread.Sleep(_ThreadSleepTime); else { dcpRxMsg.Len = buf[_messageByteIndex]; _messageByteIndex++; if (dcpRxMsg.Len < 2) { dcpRxMsg.Len = 0; return DCPRetCodes.DCP_ERR_EDC; } num = (_devInterface == null) ? 0 : _devInterface.ReadAppendDevice(buf, (dcpRxMsg.Len - buf.NumItems) + 1); if (num < 0) { buf.NumItems = 0; return DCPRetCodes.DCP_FATAL; } } continue; } case 1: { if (buf.NumItems < 2) break; dcpRxMsg.Ctrl = buf[_messageByteIndex++]; num2 = dcpRxMsg.Len - 1; continue; } case 2: if (buf.NumItems <= 2) continue; num5 = buf.NumItems - 2; num6 = 0; goto Label_025E; default: goto Label_0269; } num = (_devInterface == null) ? 0 : _devInterface.ReadAppendDevice(buf, (dcpRxMsg.Len - buf.NumItems) + 1); if (num < 0) { buf.NumItems = 0; return DCPRetCodes.DCP_FATAL; } if (num > 0) { dcpRxMsg.Ctrl = buf[_messageByteIndex++]; num2 = dcpRxMsg.Len - 1; } continue; Label_0225: dcpRxMsg.Data[_messageByteIndex - 2] = buf[_messageByteIndex++]; num2--; num3++; num6++; Label_025E: if (num6 < num5) goto Label_0225; continue; Label_0269: if (_messageByteIndex > 2) { num = 0; if (_devInterface != null) num = _devInterface.ReadAppendDevice(buf, (dcpRxMsg.Len - buf.NumItems) + 1); if (num < 0) { buf.NumItems = 0; return DCPRetCodes.DCP_FATAL; } for (int i = 0; i < num; i++) { dcpRxMsg.Data[_messageByteIndex - 2] = buf[_messageByteIndex++]; num2--; num3++; } } } if (_isPacketTimeOut) { _isPacketTimeOut = false; return DCPRetCodes.DCP_ERR_TIMEOUT; } tmrPacketTimer.Stop(); if (_messageByteIndex > 2) { _messageByteIndex -= 3; } else { return DCPRetCodes.DCP_ERR_EDC; } dcpRxMsg.EDC = dcpRxMsg.Data[_messageByteIndex]; dcpRxMsg.Data[_messageByteIndex] = 0; byte num4 = (byte) (dcpRxMsg.Len ^ dcpRxMsg.Ctrl); _messageByteIndex = 0; while (_messageByteIndex < (dcpRxMsg.Len - 2)) { num4 ^= dcpRxMsg.Data[_messageByteIndex]; _messageByteIndex++; } if (dcpRxMsg.EDC != num4) return DCPRetCodes.DCP_ERR_EDC; return DCPRetCodes.DCP_OK; }
public DCPRetCodes doRx(byte a_byte, DCPFrame dcpRxMsg) { switch (_messageByteIndex) { case 0: dcpRxMsg.Len = a_byte; if (dcpRxMsg.Len >= 2) { _expectedMessageLength = dcpRxMsg.Len; _messageByteIndex++; break; } dcpRxMsg.Len = 0; return DCPRetCodes.DCP_ERR_EDC; case 1: dcpRxMsg.Ctrl = a_byte; _messageByteIndex++; break; default: dcpRxMsg.Data[_messageByteIndex - 2] = a_byte; if (_messageByteIndex >= _expectedMessageLength) return checkIncomingMessage(dcpRxMsg); break; } return DCPRetCodes.DCP_RXINPROGRESS; }
public DCPRetCodes dataByteArrived(byte a_byte, DCPFrame a_dcpRxMsg, int a_timeout) { if (!_SOFarrived) { if (a_byte == _DCPSOFByte) { _SOFarrived = true; _messageByteIndex = 0; return DCPRetCodes.DCP_RXINPROGRESS; } return DCPRetCodes.DCP_NO_MSG; } DCPRetCodes codes = doRx(a_byte, a_dcpRxMsg); if (codes != DCPRetCodes.DCP_RXINPROGRESS) { _SOFarrived = false; } return codes; }
private int sendTelegramToMainWin(TelegramType teleType, object[] senderInfo, DCPFrame repmsg) { _teleToWin = new Telegram(_teleFromWin.data.Capacity); _teleToWin.copy(_teleFromWin); _teleToWin.uniqId = GlobalServices.uniqTelegramId(); _teleToWin.parentUniqId = _teleFromWin.uniqId; _teleToWin.type = teleType; _teleToWin.protocol = Protocol.DCP2; if (repmsg.Len >= 2) { _teleToWin.data.copy(repmsg.Data, repmsg.Len - 2); } _teleToWin.replySize = _teleFromWin.replySize; _teleToWin.mainWin = _teleFromWin.mainWin; _teleToWin.portHandler = this; _teleToWin.appFuncId = _teleFromWin.appFuncId; _teleToWin.userData = _teleFromWin.userData; _teleToWin.setNoHeader(); _teleToWin.DCPMsgType = repmsg.MsgType; _teleToWin.DCPCmdSet = repmsg.CmdSet; _teleToWin.SenderInfo = senderInfo; try { if ((teleType == TelegramType.SeriesEndOK) || (teleType == TelegramType.SeriesEndERROR)) { ((Form) _mainWin).BeginInvoke(new TelegramReceiver(_mainWin.telegramFromPortHandler), new object[] { _teleToWin }); } else { ((Form) _mainWin).Invoke(new TelegramReceiver(_mainWin.telegramFromPortHandler), new object[] { _teleToWin }); } } catch (Exception exception) { _log.Error("IOPH_DATA: Invoking telegramFromPortHandler 2 failed: (TelegramType = " + teleType.ToString() + ")" + exception.Message); } return 0; }
public int initialize() { Interlocked.Increment(ref _adtRec.NumUsers); int num = int.Parse(ConfigurationManager.AppSettings["DCPMaxDataLen"]); int num2 = int.Parse(ConfigurationManager.AppSettings["DCP2MsgRepeatNr"]); int num3 = int.Parse(ConfigurationManager.AppSettings["DCPRecTimeout"]); byte num4 = byte.Parse(ConfigurationManager.AppSettings["DCPSOFByte"]); _dcpProtocolEngine = new DCPProtocol(this, num, num2, num3, 10, num4, DCPVersion.DCP2); _DCPRXMsg = new DCPFrame(num, num4); _DCPAnswerMsg = new DCPFrame(num, num4); _DCPTXMsg = new DCPFrame(num, num4); _DCPTXBuf = new DCPFrame(num, num4); if (_adtRec.isUsbFtdiDevice()) { _ddi = DDI_USB.instance(); } else if (_adtRec.isRS232Device()) { _ddi = DDI_RS232.instance(); } else if (_adtRec.isHIDDevice()) { _ddi = DDI_HID.instance(); } else if (_adtRec.isTOOLSTICKDevice()) { _ddi = DDI_TOOLSTICK.instance(); } else if (_adtRec.isTESTDevice()) { _ddi = DDI_TEST.instance(); } else { _log.Error("IOPH_DATA.initialize() - Unknown I/O port type!"); return -1; } _iot = new Thread(new ThreadStart(doDeviceIO)); _iot.IsBackground = true; _iot.Name = "IOPH_DATA thread"; _isRequestAvailible = false; _iot.Start(); return 0; }
public DCPRetCodes open(string a_port, string a_initString) { try { StopBits one; if ((this._port != null) && this._port.IsOpen) { return DCPRetCodes.DCP_ERR_COM; } string[] strArray = a_initString.Split(new char[] { ',' }); int baudRate = int.Parse(strArray[0]); int dataBits = int.Parse(strArray[1]); Parity space = Parity.Space; string str = strArray[2].ToUpper(); if (str != null) { if (!(str == "N")) { if (str == "E") { goto Label_0085; } if (str == "O") { goto Label_0089; } } else { space = Parity.None; } } goto Label_008B; Label_0085: space = Parity.Even; goto Label_008B; Label_0089: space = Parity.Odd; Label_008B: one = StopBits.None; string str2 = strArray[3]; if (str2 != null) { if (!(str2 == "1")) { if (str2 == "5") { goto Label_00C7; } if (str2 == "2") { goto Label_00CC; } } else { one = StopBits.One; } } goto Label_00CF; Label_00C7: one = StopBits.OnePointFive; goto Label_00CF; Label_00CC: one = StopBits.Two; Label_00CF: this._port = new SerialPort(a_port, baudRate, space, dataBits, one); this._port.Open(); this._port.ReadTimeout = 1; this._dcpProtocolEngine = new DCPProtocol(this, this._DCPMaxDataLen, this._DCPMsgRepeatNr, this._DCPRecTimeout, this._ThreadSleepTime, this._DCPSOFByte); this._DCPRXMsg = new DCPFrame(this._DCPMaxDataLen, this._DCPSOFByte); this._DCPTXBuf = new DCPFrame(this._DCPMaxDataLen, this._DCPSOFByte); this._DCPTXMsg = new DCPFrame(this._DCPMaxDataLen, this._DCPSOFByte); this._DCPAnswerMsg = new DCPFrame(this._DCPMaxDataLen, this._DCPSOFByte); this._dcpProtocolEngine.controlReceivePacket(true, this._ThreadSleepTime); this._terminateRequested = false; this._ioThread = new Thread(new ThreadStart(this.DeviceIOThread)); this._ioThread.Name = "DCP_DLL IO thread"; this._ioThread.IsBackground = true; this._ioThread.Start(); DCP_DLL.Utils.log(this, "opened ", a_port.ToString() + " " + a_initString); return DCPRetCodes.DCP_OK; } catch (Exception exception) { this.closePort(); DCP_DLL.Utils.log(this, "open ", exception.Message); return DCPRetCodes.DCP_FATAL; } }