private void _port_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] buffer = new byte[1]; if ((this._port.Read(buffer, 0, 1) != 0) && (this._dcpProtocolEngine.dataByteArrived(buffer[0], this._DCPRXMsg, this._DCPRecTimeout) == DCPRetCodes.DCP_OK)) { if (((this._DCPRXMsg != null) && (this._DCPRXMsg.MsgType != 0x40)) && (this._DCPRXMsg.MsgType != 0x80)) { DCPTelegram telegram = new DCPTelegram(); telegram.Data.copy(this._DCPRXMsg.Data, this._DCPRXMsg.Len - 2); telegram.CmdSet = (CommandSet) this._DCPRXMsg.CmdSet; telegram.MsgType = (MessageTypes) this._DCPRXMsg.MsgType; this.pushTelegram(telegram, this._rxFifo, this._rxFifoMutex); if (this.receiveEvent != null) { this.receiveEvent(DCPRetCodes.DCP_MSG_RECEIVED); } } else { this._taskEvent.Set(); } } }
private void DeviceIOThread() { DCP_DLL.Utils.log(this, "DeviceIOThread ", "started..."); while (!this._terminateRequested) { Thread.Sleep(this._ThreadSleepTime); DCPTelegram telegram = this.popTelegram(this._txFifo, this._txFifoMutex); if (telegram != null) { DCPRetCodes codes; if ((telegram.MsgType == MessageTypes.ACK) || (telegram.MsgType == MessageTypes.Nack)) { codes = this._dcpProtocolEngine.AnswerToDCPMessage(telegram.Data, telegram.MsgType, telegram.CmdSet, this._DCPTXBuf, this._DCPRXMsg); } else { codes = this._dcpProtocolEngine.exchangeDCPMessage(telegram.Data, telegram.MsgType, telegram.CmdSet, this._DCPTXMsg, this._DCPAnswerMsg); } if (((codes == DCPRetCodes.DCP_OK_ACK) || (codes == DCPRetCodes.DCP_OK_NACK)) && (this._DCPRXMsg != null)) { DCPTelegram telegram2 = new DCPTelegram(); telegram2.Data.copy(this._DCPAnswerMsg.Data, this._DCPAnswerMsg.Len - 2); telegram2.CmdSet = (CommandSet) this._DCPAnswerMsg.CmdSet; telegram2.MsgType = (MessageTypes) this._DCPAnswerMsg.MsgType; this.pushTelegram(telegram2, this._respFifo, this._respFifoMutex); } this._transmitFinished = true; this._transmitResult = codes; if (this.transmitEvent != null) { this.transmitEvent(codes); } } else if ((((this._dcpProtocolEngine != null) && (this._dcpProtocolEngine.checkIfSOFArrived() == DCPRetCodes.DCP_OK)) && ((this._dcpProtocolEngine.doRxDCP(this._DCPRecTimeout, this._DCPRXMsg) == DCPRetCodes.DCP_OK) && (this._DCPRXMsg != null))) && ((this._DCPRXMsg.MsgType != 0x40) && (this._DCPRXMsg.MsgType != 0x80))) { DCPTelegram telegram3 = new DCPTelegram(); telegram3.Data.copy(this._DCPRXMsg.Data, this._DCPRXMsg.Len - 2); telegram3.CmdSet = (CommandSet) this._DCPRXMsg.CmdSet; telegram3.MsgType = (MessageTypes) this._DCPRXMsg.MsgType; this.pushTelegram(telegram3, this._rxFifo, this._rxFifoMutex); if (this.receiveEvent != null) { this.receiveEvent(DCPRetCodes.DCP_MSG_RECEIVED); } } } DCP_DLL.Utils.log(this, "DeviceIOThread ", "smoothly exited..."); }
private void pushTelegram(DCPTelegram a_telegram, List<DCPTelegram> a_fifo, Mutex a_mutex) { a_mutex.WaitOne(); a_fifo.Add(a_telegram); a_mutex.ReleaseMutex(); }
public DCPRetCodes transmitSync(CommandSet a_cmd, MessageTypes a_msgType, byte[] a_msg) { try { string str = Encoding.ASCII.GetString(a_msg, 0, a_msg.GetLength(0)); string str2 = a_cmd.ToString() + " " + a_msgType.ToString() + " msg: " + str; DCP_DLL.Utils.log(this, "transmitSync ", str2); this.checkPortAvailability(); this._transmitFinished = false; DCPTelegram telegram = new DCPTelegram { CmdSet = a_cmd, MsgType = a_msgType }; telegram.Data.copy(a_msg); this.pushTelegram(telegram, this._txFifo, this._txFifoMutex); while (!this._transmitFinished) { Thread.Sleep(this._ThreadSleepTime); } return this._transmitResult; } catch (Exception exception) { DCP_DLL.Utils.log(this, "transmitSync ", exception.Message); return DCPRetCodes.DCP_FATAL; } }