private bool isDeviceAlive(IOPortAddress iopaddr, out bool bootLoaderMode, out DeviceTypeID deviceType) { string command_CR = "\r"; DataBuffer data = new DataBuffer(100); bootLoaderMode = false; deviceType = DeviceTypeID.Unknown; if (iopaddr.handle == null) return false; if (iopaddr.handle.isInvalid()) { _log.Warn("Called with INVALID handle"); return false; } int writeRetryCnt = int.Parse(ConfigurationManager.AppSettings["WriteRetryCnt"]); int readRetryCnt = int.Parse(ConfigurationManager.AppSettings["ReadRetryCnt"]); while (writeRetryCnt > 0) { data.copy(command_CR); if (write(iopaddr.handle, data) < 0) { Thread.Sleep(10); writeRetryCnt--; } else { _log.Debug("[CR] was sent"); data.NumItems = 0; int readCount = readRetryCnt; while (readCount > 0) { if (readAppend(iopaddr.handle, data, data.Capacity - data.NumItems) < 0) readCount--; else { if (data.endsWith("OK\n\r>")) { _log.Debug("Device looks to be alive"); return true; } Thread.Sleep(20); readCount--; } } Thread.Sleep(20); writeRetryCnt--; } } _log.Debug("Device to be checked with RTS control enabled"); close(iopaddr.handle); iopaddr.handle = open(iopaddr, 19200, 'E'); _log.Debug("Port opened by isDeviceAlive()@19200:" + iopaddr.comAddr.COMPortId + " DTR Enabled"); writeRetryCnt = int.Parse(ConfigurationManager.AppSettings["WriteRetryCnt"]); readRetryCnt = int.Parse(ConfigurationManager.AppSettings["ReadRetryCnt"]); while (writeRetryCnt > 0) { data.copy(command_CR); if (write(iopaddr.handle, data) < 0) { Thread.Sleep(10); writeRetryCnt--; } else { _log.Debug("isDeviceAlive():: '\r' sent"); data.NumItems = 0; int num4 = readRetryCnt; while (num4 > 0) { if (readAppend(iopaddr.handle, data, data.Capacity - data.NumItems) < 0) { Thread.Sleep(10); num4--; } else { _log.Debug("isDeviceAlive():: response to '\r':" + data.BufToStr); if (data.BufToStr.Contains("OK\n\r>")) return true; Thread.Sleep(20); num4--; } } Thread.Sleep(20); writeRetryCnt--; } } close(iopaddr.handle); _log.Debug("Port closed by isDeviceAlive()@19200:" + iopaddr.comAddr.COMPortId); iopaddr.handle = open(iopaddr, 115200, 'D'); _log.Debug("Port opened by isDeviceAlive()@115200:" + iopaddr.comAddr.COMPortId); for (int i = 0; i < 4; i++) { data.copy("J"); if (write(iopaddr.handle, data) < 0) Thread.Sleep(10); Thread.Sleep(50); _log.Debug("isDeviceAlive():: 'J' sent"); data.NumItems = 0; int readCnt = readRetryCnt; while (readCnt > 0) { if (readAppend(iopaddr.handle, data, data.Capacity - data.NumItems) < 0) { Thread.Sleep(10); readCnt--; } else { _log.Debug("isDeviceAlive():: response to 'J':" + data.BufToStr); if (data.endsWith("OK\r\n>\r\n")) { deviceType = DeviceTypeID.SDBC_8; bootLoaderMode = true; return true; } Thread.Sleep(20); readCnt--; } } } writeRetryCnt = int.Parse(ConfigurationManager.AppSettings["WriteRetryCnt"]); readRetryCnt = int.Parse(ConfigurationManager.AppSettings["ReadRetryCnt"]); while (writeRetryCnt > 0) { data.copy("OK\n\r>"); if (write(iopaddr.handle, data) < 0) { Thread.Sleep(10); writeRetryCnt--; } else { _log.Debug("[CRLF] sent"); data.NumItems = 0; int num7 = readRetryCnt; while (num7 > 0) { if (readAppend(iopaddr.handle, data, data.Capacity - data.NumItems) < 0) { Thread.Sleep(10); num7--; } else { _log.Debug("Response to [CRLF]:" + data.BufToStr); if (data.endsWith("OK\n\r>")) return true; Thread.Sleep(20); num7--; } } Thread.Sleep(20); writeRetryCnt--; } } return false; }
private DeviceDescriptor queryDeviceInfo(DeviceHandle handle, DeviceDescriptor devInfo) { DataBuffer data = new DataBuffer(128); int writeRetryCnt = int.Parse(ConfigurationManager.AppSettings["WriteRetryCnt"]); int pollDeviceTypeProcDelay = int.Parse(ConfigurationManager.AppSettings["PollDeviceTypeProcDelay"]); int readRetryCnt = int.Parse(ConfigurationManager.AppSettings["ReadRetryCnt"]); if (handle.isInvalid()) { _log.Error("QueryDeviceInfo() - Called with INVALID argument"); return devInfo; } data.copy("\r"); write(handle, data); Thread.Sleep(pollDeviceTypeProcDelay); flushRead(handle, data); while (writeRetryCnt > 0) { if (devInfo.BootloaderMode) { data.copy("J"); _log.Debug("queryDeviceInfo():: 'J'"); } else { data.copy("I\r"); _log.Debug("queryDeviceInfo():: 'I\r'"); } if (write(handle, data) < 0) { _log.Warn("Device was probably disconnected. write() returned with -1"); return devInfo; } Thread.Sleep(pollDeviceTypeProcDelay); data.NumItems = 0; for (int i = readRetryCnt; i > 0; i--) { if (readAppend(handle, data, data.Capacity - data.NumItems) < 0) { _log.Warn("Device was probably disconnected. readAppend() returned with -1"); return devInfo; } if (data.endsWith("OK\n\r>")) break; Thread.Sleep(20); } string response = data.BufToStr; _log.Info("queryDeviceInfo():: response:" + response); try { if (response.Length < 10) return devInfo; string strTmp = response.Substring(0, 10); if (response.Contains("SRW")) { #region SRW... string[] strArray = response.Remove(0, response.IndexOf("SRW")).Split(new char[] { ',' }); string subType = strArray[3].Substring(0, 3).Trim(); switch (strArray[0].Trim()) { case "SRW001": devInfo.DeviceType = new DeviceType(DeviceTypeID.SDBC_8); switch (subType) { case "NW2": devInfo.FirmwareType = new FirmwareType("ENW_Master"); break; case "BLD": devInfo.FirmwareType = new FirmwareType("Bootloader"); break; case "PED": devInfo.FirmwareType = new FirmwareType("PER_Demo"); break; } devInfo.DevicePhoto = SDBC.SDBC_8; devInfo.MarketingName = SDBC.MarketingName; devInfo.UnAssignedDeviceText = SDBC.UnAssignedDeviceText; devInfo.SwitchPhoto = SDBC.SDBC_switch; devInfo.WarningText = SDBC.WarningText; devInfo.PartNumber = SDBC.PartNumber; break; case "SRW002": devInfo.DeviceType = new DeviceType(DeviceTypeID.SRW002); if (subType != null && subType == "LBF") devInfo.FirmwareType = new FirmwareType("LBF"); devInfo.DevicePhoto = SRW002.SRW002_400; devInfo.MarketingName = SRW002.MarketingName; devInfo.UnAssignedDeviceText = SRW002.UnAssignedDeviceText; devInfo.SwitchPhoto = SRW002.SRW002_400; devInfo.WarningText = SRW002.WarningText; devInfo.PartNumber = SRW002.PartNumber; break; case "SRW003": devInfo.DeviceType = new DeviceType(DeviceTypeID.EZLink); switch (subType) { case "NW1": devInfo.FirmwareType = new FirmwareType("NWD_Slave"); break; case "NW2": devInfo.FirmwareType = new FirmwareType("ENW_Slave"); break; case "BLD": devInfo.FirmwareType = new FirmwareType("Bootloader"); break; case "ED": devInfo.FirmwareType = new FirmwareType("EZLink"); break; } devInfo.DevicePhoto = EzLink.EZLink_D_EP; devInfo.MarketingName = EzLink.MarketingName; devInfo.UnAssignedDeviceText = EzLink.UnAssignedDeviceText; devInfo.SwitchPhoto = EzLink.EZLink_switch; devInfo.WarningText = EzLink.WarningText; devInfo.PartNumber = EzLink.PartNumber; break; case "SRW004": devInfo.DeviceType = new DeviceType(DeviceTypeID.SRW004); switch (subType) { case "LBL": devInfo.FirmwareType = new FirmwareType("LoadBoard_Lite"); break; case "BLD": devInfo.FirmwareType = new FirmwareType("Bootloader"); break; } devInfo.DevicePhoto = SRW004.Si1000DK; devInfo.MarketingName = SRW004.MarketingName; devInfo.UnAssignedDeviceText = SRW004.UnAssignedDeviceText; devInfo.WarningText = SRW004.WarningText; devInfo.PartNumber = SRW004.PartNumber; break; case "SRW005": devInfo.DeviceType = new DeviceType(DeviceTypeID.EZLink); switch (subType) { case "NW1": devInfo.FirmwareType = new FirmwareType("NWD_Slave"); break; case "NW2": devInfo.FirmwareType = new FirmwareType("ENW_Slave"); break; case "BLD": devInfo.FirmwareType = new FirmwareType("Bootloader"); break; case "ED": devInfo.FirmwareType = new FirmwareType("EZLink"); break; } devInfo.DevicePhoto = EzLink.EZLink_D_EP; devInfo.MarketingName = EzLink.MarketingName; devInfo.UnAssignedDeviceText = EzLink.UnAssignedDeviceText; devInfo.SwitchPhoto = EzLink.EZLink_switch; devInfo.WarningText = EzLink.WarningText; devInfo.PartNumber = EzLink.PartNumber; break; case "SRW007": devInfo.DeviceType = new DeviceType(DeviceTypeID.SRW007); switch (subType) { case "RKD": devInfo.FirmwareType = new FirmwareType("RKE_Master"); break; case "BLD": devInfo.FirmwareType = new FirmwareType("Bootloader"); break; } devInfo.DevicePhoto = SRW007.SRW007_100x84; devInfo.MarketingName = SRW007.MarketingName; devInfo.UnAssignedDeviceText = SRW007.UnAssignedDeviceText; devInfo.SwitchPhoto = SRW007.SRW007_switch; devInfo.WarningText = SRW007.WarningText; devInfo.PartNumber = SRW007.PartNumber; break; case "SRW008": devInfo.DeviceType = new DeviceType(DeviceTypeID.SRW008); switch (subType) { case "RKD": devInfo.FirmwareType = new FirmwareType("RKE_Master"); break; case "BLD": devInfo.FirmwareType = new FirmwareType("Bootloader"); break; } devInfo.DevicePhoto = SRW008.SRW008_100x84; devInfo.MarketingName = SRW008.MarketingName; devInfo.UnAssignedDeviceText = SRW008.UnAssignedDeviceText; devInfo.SwitchPhoto = SRW008.SRW008_switch; devInfo.WarningText = SRW008.WarningText; devInfo.PartNumber = SRW008.PartNumber; break; case "SRW009": devInfo.DeviceType = new DeviceType(DeviceTypeID.SRW009); switch (subType) { case "RKD": devInfo.FirmwareType = new FirmwareType("RKE_Master"); break; case "BLD": devInfo.FirmwareType = new FirmwareType("Bootloader"); break; } devInfo.DevicePhoto = SRW009.SRW009_100x84; devInfo.MarketingName = SRW009.MarketingName; devInfo.UnAssignedDeviceText = SRW009.UnAssignedDeviceText; devInfo.SwitchPhoto = SRW009.SRW009_switch; devInfo.WarningText = SRW009.WarningText; devInfo.PartNumber = SRW009.PartNumber; break; default: break; } devInfo.DeviceVersion = new DeviceVersion(strArray[1]); devInfo.FirmwareVersion = new FirmwareVersion(strArray[2]); return devInfo; #endregion } if (strTmp.Contains("SDBC-8")) { #region SDBC-8 int idx = strTmp.IndexOf("SDBC-8"); // SDBC-8......VVVVV......FFFFF..PED // SDBC-8......VVVVV......FFFFF..NWD // dev.version fw.version devInfo.DeviceType = new DeviceType(DeviceTypeID.SDBC_8); strTmp = response.Substring(idx + 30, 3); if (strTmp == "NWD") { devInfo.DeviceVersion = new DeviceVersion(response.Substring(idx + 12, 5)); devInfo.FirmwareType = new FirmwareType("NWD_Master"); devInfo.FirmwareVersion = new FirmwareVersion(response.Substring(idx + 23, 5)); devInfo.DevicePhoto = SDBC.SDBC_8; devInfo.SwitchPhoto = SDBC.SDBC_switch; devInfo.WarningText = SDBC.WarningText; return devInfo; } if (strTmp == "PED") { devInfo.DeviceVersion = new DeviceVersion(response.Substring(idx + 12, 5)); devInfo.FirmwareType = new FirmwareType("PER_Demo"); devInfo.FirmwareVersion = new FirmwareVersion(response.Substring(idx + 23, 5)); devInfo.DevicePhoto = SDBC.SDBC_8; devInfo.SwitchPhoto = SDBC.SDBC_switch; devInfo.WarningText = SDBC.WarningText; return devInfo; } #endregion } else if (strTmp.Contains("D-EP")) { #region EZLink int idx = strTmp.IndexOf("D-EP"); devInfo.DeviceType = new DeviceType(DeviceTypeID.EZLink); if (response.Substring(idx + 27, 3) == "NWD") { strTmp = response.Substring(idx + 9, 5); devInfo.DeviceVersion = new DeviceVersion(strTmp); devInfo.FirmwareType = new FirmwareType("NWD_Slave"); strTmp = response.Substring(idx + 20, 5); devInfo.FirmwareVersion = new FirmwareVersion(strTmp); return devInfo; } #endregion } else { if (response.Contains("V1.10W")) { devInfo.DeviceType = new DeviceType(DeviceTypeID.SDBC_8); devInfo.DeviceVersion = new DeviceVersion("01.1r"); devInfo.FirmwareType = new FirmwareType("Bootloader"); devInfo.FirmwareVersion = new FirmwareVersion("V1.10W"); devInfo.DevicePhoto = SDBC.SDBC_8; devInfo.SwitchPhoto = SDBC.SDBC_switch; devInfo.WarningText = SDBC.WarningText; return devInfo; } if (response.Length >= 26) { #region LoadBoard if (response.Substring(4, 26) == "Wireless Development Suite") { devInfo.DeviceType = new DeviceType(DeviceTypeID.LoadBoard); devInfo.DeviceVersion = new DeviceVersion("02.20"); devInfo.FirmwareType = new FirmwareType("LoadBoard"); devInfo.FirmwareVersion = new FirmwareVersion(response.Substring(0x23, 4)); devInfo.DevicePhoto = LoadBoard.HWBoard_LoadBoard; return devInfo; } if (response.Substring(4, 16) == "LoadBoard Loader") { devInfo.DeviceType = new DeviceType(DeviceTypeID.LoadBoard); devInfo.DeviceVersion = new DeviceVersion("02.20"); devInfo.FirmwareType = new FirmwareType("BootLoader"); devInfo.FirmwareVersion = new FirmwareVersion(response.Substring(0x17, 4)); return devInfo; } #endregion } } } catch (Exception exception) { _log.Error("QueryDeviceInfo() - " + exception.Message); return devInfo; } writeRetryCnt--; } return devInfo; }
private bool ExchCmdWithDev(DeviceHandle a_Handle, DataBuffer a_Data) { int writeRetryCnt = int.Parse(ConfigurationManager.AppSettings["WriteRetryCnt"]); DataBuffer data = new DataBuffer(512) { DType = Data_Type.ASCII }; while (writeRetryCnt > 0) { data.copy(a_Data); int readRetryCnt = int.Parse(ConfigurationManager.AppSettings["ReadRetryCnt"]); if (write(a_Handle, data) < 0) { _log.Warn("Device was probably disconnected. write() returned with -1"); return false; } Thread.Sleep(100); data.NumItems = 0; while (readRetryCnt > 0) { if (readAppend(a_Handle, data, data.Capacity - data.NumItems) < 0) { _log.Warn("Device was probably disconnected. readAppend() returned with -1"); Thread.Sleep(50); readRetryCnt--; } else { if (data.endsWith("OK\n\r>")) { a_Data.copy(data); return true; } Thread.Sleep(100); readRetryCnt--; } } writeRetryCnt--; } a_Data.NumItems = 0; return false; }
public bool isDeviceAlive(DeviceHandle handle) { DataBuffer data = new DataBuffer("\r".Length + "\n\rOK\n\r>".Length); if (handle.isInvalid()) { GlobalServices.ErrMsg("DDI_TEST.isDeviceAlive()", "called with INVALID handle"); return false; } data.copy("\r"); if (this.write(handle, data) < 0) { return false; } if (this.read(handle, data, data.Capacity) < 0) { return false; } return data.endsWith("\n\rOK\n\r>"); }
private TelegramType _readReply(Telegram commandTele, DataBuffer replyData) { int replySize; string str = "\n\rOK\n\r>"; string str2 = "\n\rERROR\n\r>"; replyData.NumItems = 0; if (this._isTestDevice) { if (commandTele.protocol == Protocol.Write) { this._ddi.read(this._adtRec.PortAddress.handle, replyData, 7); return TelegramType.AcknowledgeOK; } if (commandTele.protocol == Protocol.WriteRead) { this._ddi.read(this._adtRec.PortAddress.handle, replyData, commandTele.replySize + 7); return TelegramType.DataAcknowledgeOK; } GlobalServices.ErrMsg("readReply()", "FATAL ERROR: Unexpected telegram type to execute: " + ((TelegramType) this._teleFromWin.type).ToString() + ". Exit WDS!"); Application.Exit(); return TelegramType.Invalid; } switch (commandTele.protocol) { case Protocol.Write: replySize = 0; break; case Protocol.WriteRead: replySize = commandTele.replySize; break; default: GlobalServices.ErrMsg("readReply()", "FATAL ERROR: Unexpected telegram type to execute: " + ((TelegramType) this._teleFromWin.type).ToString() + ". Exit WDS!"); GlobalServices.msgBox("FATAL ERROR: Unexpected telegram type to execute: " + ((TelegramType) this._teleFromWin.type).ToString() + ". Exit WDS!", "readReply()"); Application.Exit(); return TelegramType.Invalid; } int bytesToRead = ((str.Length + replySize) < str2.Length) ? (str.Length + replySize) : str2.Length; int num3 = ((str.Length + replySize) < str2.Length) ? str2.Length : (str.Length + replySize); int numItems = commandTele.data.NumItems; int commandIdx = 0; bool flag = false; do { if (this._ddi.read(this._adtRec.PortAddress.handle, replyData, bytesToRead) < 0) { return TelegramType.DeviceFailure; } if (bytesToRead != replyData.NumItems) { flag = true; } this.filterEcho(commandTele.data, ref commandIdx, replyData); } while ((replyData.NumItems == 0) && !flag); int num5 = replyData.NumItems; int num4 = bytesToRead - num5; if (this._ddi.readAppend(this._adtRec.PortAddress.handle, replyData, num4) >= 0) { if (num4 != (replyData.NumItems - num5)) { flag = true; } switch (commandTele.protocol) { case Protocol.Write: if (!replyData.endsWith(str)) { if (replyData.endsWith(str2)) { return TelegramType.AcknowledgeERROR; } break; } return TelegramType.AcknowledgeOK; case Protocol.WriteRead: if (!replyData.endsWith(str)) { if (replyData.endsWith(str2)) { return TelegramType.DataAcknowledgeERROR; } break; } return TelegramType.DataAcknowledgeOK; default: return TelegramType.Invalid; } if (flag) { return TelegramType.DeviceFailure; } num4 = num3 - replyData.NumItems; if (this._ddi.readAppend(this._adtRec.PortAddress.handle, replyData, num4) < 0) { return TelegramType.DeviceFailure; } if (num4 != replyData.NumItems) { flag = true; } switch (commandTele.protocol) { case Protocol.Write: if (!replyData.endsWith(str)) { if (replyData.endsWith(str2)) { return TelegramType.AcknowledgeERROR; } break; } return TelegramType.AcknowledgeOK; case Protocol.WriteRead: if (!replyData.endsWith(str)) { if (replyData.endsWith(str2)) { return TelegramType.DataAcknowledgeERROR; } break; } return TelegramType.DataAcknowledgeOK; default: return TelegramType.Invalid; } } return TelegramType.DeviceFailure; }
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; }