private int sendTelegramToMainWin(TelegramType teleType, Protocol protocol, DataBuffer dataPacket) { this._teleToWin.uniqId = GlobalServices.uniqTelegramId(); this._teleToWin.parentUniqId = -1; this._teleToWin.type = teleType; this._teleToWin.protocol = protocol; this._teleToWin.data.copy(dataPacket); this._teleToWin.replySize = -1; this._teleToWin.mainWin = null; this._teleToWin.portHandler = this; this._teleToWin.appFuncId = -1; this._teleToWin.userData = this._dllFuncRetVal; this._teleToWin.setNoHeader(); if (teleType == TelegramType.DeviceFailure) { GlobalServices.ErrMsg("sendTelegramToMainWin('DeviceFailure', " + dataPacket.objectInfo(true) + ")", "DeviceFailure telegram will be sent !"); } try { if (this._isSeries) { if ((teleType == TelegramType.SeriesEndOK) || (teleType == TelegramType.SeriesEndERROR)) { ((Form) this._mainWin).BeginInvoke(new TelegramReceiver(this._mainWin.telegramFromPortHandler), new object[] { this._teleToWin }); } else { ((Form) this._mainWin).Invoke(new TelegramReceiver(this._mainWin.telegramFromPortHandler), new object[] { this._teleToWin }); } } else { ((Form) this._mainWin).BeginInvoke(new TelegramReceiver(this._mainWin.telegramFromPortHandler), new object[] { this._teleToWin }); } } catch { GlobalServices.ErrMsg("IOPH_EZLinkDongle", "Invoking telegramFromPortHandler failed"); } return 0; }
public int write(DeviceHandle handle, DataBuffer data) { string reply = ""; byte[] buffer = new byte[15]; if (handle != null) { if (handle.isInvalid()) { GlobalServices.ErrMsg("DDI_TEST.write()", "called with INVALID handle"); return -1; } if (data.NumItems == 0) { return 0; } if (data.DType == Data_Type.Binary) { GlobalServices.calculateExpextedReplyLen(data, ref buffer); handle.TestDevHnd.readBuff.copy(buffer); } else { GlobalServices.calculateExpextedReplyLen(data.objectInfo(false), ref reply); handle.TestDevHnd.readBuff.copy(reply); } handle.TestDevHnd.readBuffIdx = 0; Thread.Sleep(50); } return 0; }
private void ButtonWrite1_Click(object sender, EventArgs e) { string str = "\n\rOK\n\r>"; string str2 = "\n\rERROR\n\r>"; string writeStr = this.textBoxWrite1.Text + "\r"; DataBuffer data = new DataBuffer(0x1000); Telegram telegram = this.composeCommandTele(writeStr, 0); this.textBoxWrite1Result.Text = "Flush readBuffer... "; base.Update(); if (this._ddi.flushRead(this._devHandle1, data) < 0) { this.textBoxWrite1Result.Text = this.textBoxWrite1Result.Text + "device is not opened !!!"; base.Update(); } else { string text = this.textBoxWrite1Result.Text; this.textBoxWrite1Result.Text = text + "_ddi.flushRead() bytesReturned: '" + data.NumItems.ToString() + "' replyBuffer:\r\n---|" + data.objectInfo(true) + "|---\r\n"; int num3 = this._ddi.write(this._devHandle1, telegram.data); string str5 = this.textBoxWrite1Result.Text; this.textBoxWrite1Result.Text = str5 + "_ddi.write() writeBuffer---|" + telegram.data.objectInfo(true) + "|--- status:" + num3.ToString() + "\r\n"; base.Update(); int num = (telegram.data.NumItems + telegram.replySize) + str.Length; int bytesToRead = (num < str2.Length) ? str2.Length : num; num3 = this._ddi.read(this._devHandle1, data, bytesToRead); string str6 = this.textBoxWrite1Result.Text; this.textBoxWrite1Result.Text = str6 + "_ddi.read() bytesToRead: '" + bytesToRead.ToString() + "' returnedBytes: '" + num3.ToString() + "' readBuffer:\r\n---|" + data.objectInfo(true) + "|---\r\n"; this.textBoxWrite1.Clear(); } }
private void btnLED1_Click(object sender, EventArgs e) { string str3; string str = "\n\rOK\n\r>"; string str2 = "\n\rERROR\n\r>"; DataBuffer data = new DataBuffer(0x1000); if (this.btnLED1.Text == "LED") { str3 = "D1\r"; } else { str3 = "D0\r"; } Telegram telegram = this.composeCommandTele(str3, 1); this.textBoxWrite1Result.Text = "Flush readBuffer... "; base.Update(); this._ddi.flushRead(this._devHandle1, data); string text = this.textBoxWrite1Result.Text; this.textBoxWrite1Result.Text = text + "_ddi.flushRead() bytesReturned: '" + data.NumItems.ToString() + "' replyBuffer:\r\n---|" + data.objectInfo(true) + "|---\r\n"; int num3 = this._ddi.write(this._devHandle1, telegram.data); string str5 = this.textBoxWrite1Result.Text; this.textBoxWrite1Result.Text = str5 + "_ddi.write() writeBuffer---|" + telegram.data.objectInfo(true) + "|--- status:" + num3.ToString() + "\r\n"; base.Update(); int num = (telegram.data.NumItems + telegram.replySize) + str.Length; int bytesToRead = (num < str2.Length) ? str2.Length : num; num3 = this._ddi.read(this._devHandle1, data, bytesToRead); string str6 = this.textBoxWrite1Result.Text; this.textBoxWrite1Result.Text = str6 + "ReadWait() bytesReturned: '" + data.NumItems.ToString() + "' readBuffer:\r\n---|" + data.objectInfo(true) + "|--- status:" + num3.ToString() + "'\r\n"; if (num3 > 0) { if (this.btnLED1.Text == "LED") { this.btnLED1.Text = "LED C"; } else { this.btnLED1.Text = "LED"; } } }
private TelegramType readRxPacket(DataBuffer replyData) { string str = "Received packet ( 6 bytes):"; string str2 = "\n\rOK\n\r>"; replyData.NumItems = 0; while (this._readRxPacketEnabled && (replyData.NumItems < (str.Length * 2))) { int num5 = this._ddi.readAppend(this._adtRec.PortAddress.handle, replyData, 1); if (num5 < 0) { replyData.NumItems = 0; return TelegramType.DeviceFailure; } if ((num5 > 0) && (replyData[replyData.NumItems - 1] == 0x3a)) { break; } Thread.Sleep(10); } if (replyData.NumItems == 0) { return TelegramType.DataAcknowledgeOK; } if (replyData[replyData.NumItems - 1] != 0x3a) { replyData.objectInfo(true); replyData.NumItems = 0; return TelegramType.DeviceFailure; } int num3 = replyData.indexOf('('); if (num3 == -1) { replyData.NumItems = 0; return TelegramType.DataAcknowledgeERROR; } int bytesToRead = int.Parse(replyData.objectInfo(num3 + 1, replyData.NumItems - 9, true)) + str2.Length; replyData.NumItems = 0; int num4 = 0; for (int i = 0; (i <= 5) || ((num4 > 0) && (i <= 50)); i++) { num4 = this._ddi.readAppend(this._adtRec.PortAddress.handle, replyData, bytesToRead); if (num4 < 0) { replyData.NumItems = 0; return TelegramType.DeviceFailure; } bytesToRead -= num4; if (bytesToRead <= 0) { bytesToRead = 1; } if (replyData.endsWith(str2)) { replyData.NumItems -= str2.Length; return TelegramType.DataAcknowledgeOK; } } return TelegramType.DataAcknowledgeERROR; }
private TelegramType readReply(Telegram commandTele, DataBuffer replyData) { TelegramType deviceFailure = TelegramType.DeviceFailure; bool flag = false; int dueTime = int.Parse(ConfigurationManager.AppSettings["ASCIIRecTimeout"]); string str = "\n\rOK\n\r>"; string str2 = "\n\rERROR\n\r>"; int num2 = (commandTele.data.NumItems + commandTele.replySize) + str.Length; int num3 = commandTele.data.NumItems + str2.Length; int bytesToRead = (num2 < num3) ? num3 : num2; int num5 = 0; int num6 = 0; this._isReadReplyTimeOut = false; this._ReadReplyTimer = new System.Threading.Timer(this.ReadReplytimerDelegate, null, dueTime, -1); replyData.NumItems = 0; while (!this._isReadReplyTimeOut && !flag) { if ((num6 > 0) && ((bytesToRead - num6) > 9)) { bytesToRead -= num6; } num6 = this._ddi.readAppend(this._adtRec.PortAddress.handle, replyData, bytesToRead); if (num6 < 0) { flag = true; deviceFailure = TelegramType.DeviceFailure; continue; } switch (commandTele.protocol) { case Protocol.Write: if (replyData.endsWith(str)) { flag = true; deviceFailure = TelegramType.AcknowledgeOK; } if (replyData.endsWith(str2)) { flag = true; deviceFailure = TelegramType.AcknowledgeERROR; } break; case Protocol.WriteRead: if (replyData.endsWith(str)) { flag = true; deviceFailure = TelegramType.DataAcknowledgeOK; } if (replyData.endsWith(str2)) { flag = true; deviceFailure = TelegramType.DataAcknowledgeERROR; } break; default: flag = true; deviceFailure = TelegramType.Invalid; break; } Thread.Sleep(1); num5++; } if (this._isReadReplyTimeOut) { _log.Warn("reply data neither ends with OK or ERROR !!! GIVE UP !!!\r\n retryCnt: '" + num5.ToString() + " commandTele.protocol: '" + commandTele.protocol.ToString() + "' replyData.NumItems: '" + replyData.NumItems.ToString() + "' replyData:--|" + replyData.objectInfo(true) + "|--n"); deviceFailure = TelegramType.DeviceFailure; } if (this._ReadReplyTimer != null) { this._ReadReplyTimer.Dispose(); this._ReadReplyTimer = null; } return deviceFailure; }