public static bool ReadData(AnGuiData mAnGuiData, AnGuiList anGuiList, ref bool isReadOver) { bool isOk = false; bool isRead7440 = false; bool isRead7623 = false; cHYAnGui.TempAnGuiData tmpData = new TempAnGuiData(); tmpData.isReadOk = new bool[AnGuiData.testCount]; tmpData.ReadData = new float[AnGuiData.testCount]; tmpData.isPass = new bool[AnGuiData.testCount]; tmpData.index = new int[AnGuiData.testCount]; for (int i = 0; i < tempAnGuiData.ReadData.Length; i++) { tmpData.ReadData[i] = 0; tmpData.isReadOk[i] = false; tmpData.isPass[i] = true; tmpData.index[i] = i + 1; } AnGuiTestList tmpAnGuiTestList = AnGuiTestList.JieDi; isReadOver = true; try { switch (anGuiList) { case AnGuiList.HY7440: isRead7440 = true; break; case AnGuiList.HY7623: isRead7623 = true; break; case AnGuiList.BothTwo: isRead7440 = true; isRead7623 = true; break; } if (isRead7623) { if (hy7623 != null) { if (!tmpData.isReadOk[3]) { if (hy7623.HYAnGuiRead(AnGuiList.HY125, 1, ref tmpData.ReadData[3], ref tmpData.isPass[3], out tmpAnGuiTestList)) { tmpData.isReadOk[3] = true; switch (tmpAnGuiTestList) { case AnGuiTestList.JieDi: tmpData.index[3] = 0; break; case AnGuiTestList.JueYuan: tmpData.index[3] = 1; break; case AnGuiTestList.NaiYa: tmpData.index[3] = 2; break; case AnGuiTestList.XieLou: tmpData.index[3] = 3; break; } } } } } if (isRead7440) { if (hy7440 != null) { for (int i = 0; i < 3; i++) { if (!tmpData.isReadOk[i]) { Thread.Sleep(100); if (hy7440.HYAnGuiRead(AnGuiList.HY7440, i + 1, ref tmpData.ReadData[i], ref tmpData.isPass[i], out tmpAnGuiTestList)) { tmpData.isReadOk[i] = true; switch (tmpAnGuiTestList) { case AnGuiTestList.JieDi: tmpData.index[i] = 0; break; case AnGuiTestList.JueYuan: tmpData.index[i] = 1; break; case AnGuiTestList.NaiYa: tmpData.index[i] = 2; break; case AnGuiTestList.XieLou: tmpData.index[i] = 3; break; } } } } } } for (int i = 0; i < tmpData.isReadOk.Length; i++) { if (!tmpData.isReadOk[i]) { isReadOver = false; break; } } if (isReadOver) { mAnGuiData.isPass = true; for (int i = 0; i < tmpData.ReadData.Length; i++) { if (tmpData.index[i] < AnGuiData.testCount) { mAnGuiData.TestData[tmpData.index[i]] = tmpData.ReadData[i]; mAnGuiData.TestResult[tmpData.index[i]] = tmpData.isPass[i]; mAnGuiData.isPass = (mAnGuiData.isPass && tmpData.isPass[i]); } } } isOk = true; } catch (Exception exc) { All.Class.Error.Add(exc); } return(isOk); }
public bool HYAnGuiRead(AnGuiList anGuiList, int testIndex, ref float testData, ref bool testResult, out AnGuiTestList anGuiTestList) { //ACW anGuiTestList = AnGuiTestList.JieDi; bool isOk = false; byte[] WriteBuff = new byte[9]; //发送数据 byte[] ReadBuff = new byte[50]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } switch (anGuiList) { case AnGuiList.HY125: WriteBuff[0] = Convert.ToByte('t'); WriteBuff[1] = Convert.ToByte('d'); WriteBuff[2] = Convert.ToByte('?'); WriteBuff[3] = 0x0D; WriteBuff[4] = 0x0A; comPort.DiscardInBuffer(); ReturnByte = 0; comPort.Write(WriteBuff, 0, 5); break; case AnGuiList.HY7440: case AnGuiList.HY7623: WriteBuff[0] = Convert.ToByte('?'); WriteBuff[1] = Convert.ToByte(Convert.ToChar(testIndex.ToString())); WriteBuff[2] = 0x0D; WriteBuff[3] = 0x0A; comPort.DiscardInBuffer(); ReturnByte = 0; comPort.Write(WriteBuff, 0, 4); break; } NowTime = DateTime.Now; IsTimeOut = false; ReturnByte = comPort.BytesToRead; do { Thread.Sleep(30); if (ReturnByte == comPort.BytesToRead) { if (ReturnByte > 0) { IsReturn = true; } } else { ReturnByte = comPort.BytesToRead; } ts = DateTime.Now - NowTime; if (ts.TotalMilliseconds > timeOut)//时间超时 { IsTimeOut = true; } } while (!IsReturn && !IsTimeOut); if (!IsReturn && IsTimeOut)//超时 { if (errStr.IndexOf("停止失败,接收数据已超时") < 0) { errStr = errStr + DateTime.Now.ToString() + mName + ":停止失败,接收数据已超时" + (char)13 + (char)10; } isOk = false; } else { ReturnByte = comPort.BytesToRead; comPort.Read(ReadBuff, 0, ReturnByte); if (ReadBuff[0] == 0x15) { isOk = false; errStr = "正在检测"; } else { switch (anGuiList) { case AnGuiList.HY125: if (ReturnByte < 23) { isOk = false; errStr = "应答失败"; } else { testResult = false; testData = 0; string tempStr = Encoding.ASCII.GetString(ReadBuff, 0, ReturnByte); if (tempStr.ToLower().IndexOf("ass") >= 0) { testResult = true; } string[] buff = tempStr.Split(','); if (buff.Length >= 7) { int start = 0; for (int i = 0; i < buff[5].Length; i++) { if (buff[5][i] >= 0x30 && buff[5][i] <= 0x39) { start = i; break; } } anGuiTestList = AnGuiTestList.XieLou; testData = All.Class.Num.ToFloat(buff[5].Substring(start)); isOk = true; } } break; case AnGuiList.HY7440: case AnGuiList.HY7623: if (ReturnByte < 40) { isOk = false; errStr = "应答失败"; } else { testResult = false; testData = 0; string tempStr = Encoding.ASCII.GetString(ReadBuff, 0, ReturnByte); switch (anGuiList) { case AnGuiList.HY7440: if (tempStr.ToLower().IndexOf("pass") >= 0) { testResult = true; } if (tempStr.ToLower().IndexOf("acw") >= 0) { anGuiTestList = AnGuiTestList.NaiYa; } if (tempStr.ToLower().IndexOf("ir") >= 0) { anGuiTestList = AnGuiTestList.JueYuan; } if (tempStr.ToLower().IndexOf("gnd") >= 0) { anGuiTestList = AnGuiTestList.JieDi; } break; case AnGuiList.HY7623: if (tempStr.ToLower().IndexOf("ass") >= 0) { testResult = true; } break; case AnGuiList.BothTwo: throw new Exception("anGuiList.BothTwo,此处不能用此参数"); } bool isNumStart = false; int NumStart = 0; int NumEnd = 0; for (int i = 32; i < ReturnByte; i++) { if (!isNumStart) { if ((ReadBuff[i] >= 0x30) && (ReadBuff[i] <= 0x39)) { isNumStart = true; NumStart = i; } } if (isNumStart) { if (((ReadBuff[i] != 46) && (ReadBuff[i] < 0x30)) || (ReadBuff[i] > 0x39)) { isNumStart = false; NumEnd = i; break; } } } testData = All.Class.Num.ToFloat(Encoding.ASCII.GetString(ReadBuff, NumStart, NumEnd - NumStart)); isOk = true; } break; } } } } catch (Exception exc) { if (errStr.IndexOf(exc.ToString()) < 0) { errStr = errStr + DateTime.Now.ToString() + mName + ":" + exc.ToString() + (char)13 + (char)10; } isOk = false; } return(isOk); }