/// <summary> /// 读取2010模块数据 /// </summary> /// <param name="ReturnBuff">返回读取9元素数组,0,1,2为电压,3,4,5为电流,6,7,8为功率</param> /// <returns>返回读取数据是否成功</returns> public bool WbGonglvRead_W(ref double[] ReturnBuff) { if (cMain.isDebug) { ReturnBuff[0] = 220 + 10 * Num.Rand(); ReturnBuff[1] = 220 + 10 * Num.Rand(); ReturnBuff[2] = 220 + 10 * Num.Rand(); ReturnBuff[3] = 4; // +5 * Num.Rand(); ReturnBuff[4] = 6; // +5 * Num.Rand(); ReturnBuff[5] = 8; // +5 * Num.Rand(); ReturnBuff[6] = ReturnBuff[0] * ReturnBuff[3]; ReturnBuff[7] = ReturnBuff[1] * ReturnBuff[4]; ReturnBuff[8] = ReturnBuff[2] * ReturnBuff[5]; return(true); } bool returnValue = false; long[] mReturnBuff = new long[35]; long[] ReturnBuff1 = new long[5]; byte[] WriteBuff = new byte[10]; //发送数据 byte[] ReadBuff = new byte[16]; //接收数据 try { //05 03 24 10 00 06 CF 79 WriteBuff[0] = 0x7E; WriteBuff[1] = 0x01; WriteBuff[2] = 0xFF; WriteBuff[3] = 0x50; WriteBuff[4] = 0xB0; WriteBuff[5] = 0x0D; if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(WriteBuff, 0, 6); } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",WbGonglvRead," + ":" + exc.ToString() + (char)13 + (char)10; } returnValue = false; return(returnValue); } returnValue = true; return(returnValue); }
///// <summary> ///// 标准模块寄存器写入单个数值 ///// </summary> ///// <param name="StartAdd">要写入寄存器地址</param> ///// <param name="mWriteBuff">要写入的值</param> ///// <returns>写入是否成功</returns> //public bool StandarBoardWritePoint(int StartAdd, int mWriteBuff) //{ // long[] ReturnBuff = new long[8]; // byte[] WriteBuff = new byte[8];//发送数据 // byte[] ReadBuff = new byte[21];//接收数据 // int ReturnByte = 0;//返回数据 // bool IsReturn = false;//是否成功返回 // bool IsTimeOut = false;//是否超时 // DateTime NowTime;//当前时间 // TimeSpan ts;//时间差 // byte CrcHi = 0, CrcLo = 0;//CRC校验 // if (cMain.isDebug) // { // return true; // } // if (!mStandarBoardInit)//没有初始化 // { // StandarBoardInit(); // return false; // } // try // { // WriteBuff[0] = _StandarModebusAddress; // WriteBuff[1] = 0x06; // WriteBuff[2] = (byte)((StartAdd & 0xFF00) >> 8); // WriteBuff[3] = (byte)(StartAdd & 0xFF); // WriteBuff[4] = (byte)((mWriteBuff & 0xFF00) >>8); // WriteBuff[5] = (byte)(mWriteBuff & 0xFF); // cMain.CRC_16(WriteBuff, 6, ref CrcLo, ref CrcHi); // WriteBuff[6] = CrcLo; // WriteBuff[7] = CrcHi; // if (!comPort.IsOpen) // { // comPort.Open(); // } // comPort.DiscardInBuffer();//刷新串口 // comPort.Write(WriteBuff, 0, 8); // NowTime = DateTime.Now; // do // { // if (comPort.BytesToRead >= 8)//收到数据 // { // ReturnByte = comPort.BytesToRead; // IsReturn = true; // } // 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; // } // return false; // } // comPort.Read(ReadBuff, 0, ReturnByte); // if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]))//数据检验失败 // { // if (ErrStr.IndexOf("接收数据错误") < 0) // { // ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":读取失败,接收数据错误" + (char)13 + (char)10; // } // return false; // } // } // catch (Exception exc) // { // if (ErrStr.IndexOf(exc.ToString()) < 0) // { // ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":" + exc.ToString() + (char)13 + (char)10; // } // return false; // } // return true; //} /// <summary> /// 标准模块寄存器写入单个数值,专用于LGPLC写M点,功能码0x05,0x06 /// </summary> /// <param name="StartAdd">要写入寄存器地址</param> /// <param name="mWriteBuff">要写入的值</param> /// <param name="code">命令码,5或者6,写线圈或写单个寄存器</param> /// <returns>写入是否成功</returns> public bool StandarBoardWritePoint(int StartAdd, int mWriteBuff, byte code) { long[] ReturnBuff = new long[8]; byte[] WriteBuff = new byte[8]; //发送数据 byte[] ReadBuff = new byte[21]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime; //当前时间 TimeSpan ts; //时间差 byte CrcHi = 0, CrcLo = 0; //CRC校验 if (cMain.isDebug) { return(true); } if (!mStandarBoardInit)//没有初始化 { StandarBoardInit(); return(false); } try { WriteBuff[0] = _StandarModebusAddress; WriteBuff[1] = code; WriteBuff[2] = (byte)((StartAdd & 0xFF00) >> 8); WriteBuff[3] = (byte)(StartAdd & 0xFF); WriteBuff[4] = (byte)((mWriteBuff & 0xFF00) >> 8); WriteBuff[5] = (byte)(mWriteBuff & 0xFF); cMain.CRC_16(WriteBuff, 6, ref CrcLo, ref CrcHi); WriteBuff[6] = CrcLo; WriteBuff[7] = CrcHi; if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(WriteBuff, 0, 8); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(50); if (comPort.BytesToRead >= 8)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",StandarBoardWritePoint," + ":读取失败,接收数据已超时" + (char)13 + (char)10; } return(false); } comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardWritePoint," + ":读取失败,接收数据错误" + (char)13 + (char)10; } return(false); } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardWritePoint," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
public bool ReadData(int StartPoint, ref double ReadData) { bool isOk = false; if (cMain.isDebug) { ReadData = (byte)(20 + (byte)(Num.Rand() * 10)); return(true); } if (!c7017IsInit)//没有初始化 { init(); return(false); } string tmpReadData; //收到数据 string WriteBuff = ""; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff = string.Format("#{0:D2}{1}\r", c7017Address, StartPoint); comPort.DiscardInBuffer(); comPort.Write(WriteBuff); NowTime = DateTime.Now; do { Threading.Thread.Sleep(20); if (comPort.BytesToRead >= 9)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",StandarBoardInit," + "初始失败,接收数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != 0x3E) || (ReadBuff[8] != 0x0D))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":初始失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { tmpReadData = Encoding.ASCII.GetString(ReadBuff, 0, ReturnByte); string[] tmp = tmpReadData.Split(new char[] { '+', '-' }); ReadData = Num.DoubleParse(tmp[1]); } isOk = true; } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":" + exc.ToString() + (char)13 + (char)10; } isOk = false; } return(isOk); }
public bool AiNuo9641Start() { bool returnValue = false; if (!mAiNuo9641Init) { AiNuo9641Init(); return(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(); } WriteBuff[0] = 0x7B; WriteBuff[1] = _AiNuo9641BAddress[0]; WriteBuff[2] = _AiNuo9641BAddress[1]; WriteBuff[3] = _AiNuo9641BAddress[2]; WriteBuff[4] = 0x31; WriteBuff[5] = 0x30; WriteBuff[6] = 0x30; WriteBuff[7] = 0x7D; comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 8); NowTime = DateTime.Now; do { if (comPort.BytesToRead >= 9)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]) || (ReadBuff[2] != WriteBuff[2]))//数据检验失败 { //comPort.Close(); if (ErrStr.IndexOf("停止失败,接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":停止失败,接收数据错误" + (char)13 + (char)10; } return(false); } } returnValue = true; } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":" + exc.ToString() + (char)13 + (char)10; } } return(returnValue); }
public bool AiNuo9641Set(SetData sSetData) { bool returnValue = false; if (!mAiNuo9641Init) { AiNuo9641Init(); return(false); } byte[] WriteBuff = new byte[209]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff[0] = 0x7B; WriteBuff[1] = _AiNuo9641BAddress[0]; WriteBuff[2] = _AiNuo9641BAddress[1]; WriteBuff[3] = _AiNuo9641BAddress[2]; WriteBuff[4] = 0x37; for (int i = 0; i < 8; i++) { switch (sSetData.mSetStepData[i].mTestName) { case TestNameEnum.JieDi: WriteBuff[5 + 18 * i] = 0x31; sSetData.mSetStepData[i].DataHi = (int)sSetData.mSetStepData[i].DataHi; sSetData.mSetStepData[i].DataLo = (int)sSetData.mSetStepData[i].DataLo; WriteBuff[22 + 18 * i] = 0x30; break; case TestNameEnum.JueYuan: WriteBuff[5 + 18 * i] = 0x32; sSetData.mSetStepData[i].DataHi = (int)(sSetData.mSetStepData[i].DataHi * 10); sSetData.mSetStepData[i].DataLo = (int)(sSetData.mSetStepData[i].DataLo * 10); WriteBuff[22 + 18 * i] = 0x30; break; case TestNameEnum.NaiYa: WriteBuff[5 + 18 * i] = 0x33; sSetData.mSetStepData[i].DataHi = (int)(sSetData.mSetStepData[i].DataHi * 100); sSetData.mSetStepData[i].DataLo = (int)(sSetData.mSetStepData[i].DataLo * 100); if (sSetData.mSetStepData[i].isActive) { WriteBuff[22 + 18 * i] = 0x31; } else { WriteBuff[22 + 18 * i] = 0x30; } break; case TestNameEnum.XieLou: WriteBuff[5 + 18 * i] = 0x34; sSetData.mSetStepData[i].DataHi = (int)(sSetData.mSetStepData[i].DataHi * 1000); sSetData.mSetStepData[i].DataLo = (int)(sSetData.mSetStepData[i].DataLo * 1000); if (sSetData.mSetStepData[i].isActive) { WriteBuff[22 + 18 * i] = 0x31; } else { WriteBuff[22 + 18 * i] = 0x30; } break; case TestNameEnum.None: default: WriteBuff[5 + 18 * i] = 0x30; WriteBuff[22 + 18 * i] = 0x30; break; } sSetData.mSetStepData[i].mTime = (int)sSetData.mSetStepData[i].mTime * 10; string[] tempStr = new string[4]; tempStr[0] = string.Format("{0:D4}", (int)sSetData.mSetStepData[i].Data); tempStr[1] = string.Format("{0:D4}", (int)sSetData.mSetStepData[i].DataLo); tempStr[2] = string.Format("{0:D4}", (int)sSetData.mSetStepData[i].DataHi); tempStr[3] = string.Format("{0:D4}", (int)sSetData.mSetStepData[i].mTime); byte[] tempByt0 = Encoding.ASCII.GetBytes(tempStr[0]); byte[] tempByt1 = Encoding.ASCII.GetBytes(tempStr[1]); byte[] tempByt2 = Encoding.ASCII.GetBytes(tempStr[2]); byte[] tempByt3 = Encoding.ASCII.GetBytes(tempStr[3]); if (tempByt2.Length > 4) { tempByt2 = Encoding.ASCII.GetBytes(AiNuoData((int)sSetData.mSetStepData[i].DataHi)); } for (int j = 0; j < 4; j++) { WriteBuff[6 + 18 * i + j] = tempByt0[j]; WriteBuff[10 + 18 * i + j] = tempByt1[j]; WriteBuff[14 + 18 * i + j] = tempByt2[j]; WriteBuff[18 + 18 * i + j] = tempByt3[j]; } } if (sSetData.isGoOn) { WriteBuff[149] = 0x31; } else { WriteBuff[149] = 0x30; } for (int i = 150; i < 205; i++) { WriteBuff[i] = 0x30; } WriteBuff[205] = (byte)(0x30 + sSetData.stepIndex); WriteBuff[206] = 0x30; WriteBuff[207] = 0x30; WriteBuff[208] = 0x7D; comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 209); string tepm = Encoding.ASCII.GetString(WriteBuff); NowTime = DateTime.Now; do { if (comPort.BytesToRead >= 9)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } ts = DateTime.Now - NowTime; if (ts.TotalMilliseconds > 1500)//时间超时 { IsTimeOut = true; } } while (!IsReturn && !IsTimeOut); if (!IsReturn && IsTimeOut)//超时 { if (ErrStr.IndexOf("设置失败,接收数据已超时") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":设置失败,接收数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]) || (ReadBuff[2] != WriteBuff[2]))//数据检验失败 { //comPort.Close(); if (ErrStr.IndexOf("设置失败,接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":设置失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { if (ReadBuff[5] == 0x32) { if (ErrStr.IndexOf("设置失败,安规处于设置状态,或未知错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":设置失败,安规处于设置状态,或未知错误" + (char)13 + (char)10; } return(false); } } } returnValue = true; } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":" + exc.ToString() + (char)13 + (char)10; } } return(returnValue); }
public bool WriteR(int RValue, bool Value) { //if (!mPanasonicPlcIsInit) //{ // Init(); // return false; //} bool isOK = false; string WriteStr = "";//发送数据 byte[] WriteBuff = new byte[17]; byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 string CrcHi = "", CrcLo = ""; TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteStr = string.Format("<{0:D2}#WCSR{1:X4}{2}", PanasonicPlcAddress, RValue, Value ? 1 : 0); //string.Format("<{0:D2}#RCP1R{1:D4}", PanasonicPlcAddress, RValue); Crc(WriteStr, ref CrcLo, ref CrcHi); WriteStr = WriteStr + CrcLo + CrcHi + "\r\n"; comPort.DiscardInBuffer(); WriteBuff = Encoding.ASCII.GetBytes(WriteStr); comPort.Write(WriteBuff, 0, WriteBuff.Length); NowTime = DateTime.Now; do { if (comPort.BytesToRead >= 9)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",PanasonicPlcRead," + "初始失败,接收数据已超时" + (char)13 + (char)10; } isOK = false; } else { comPort.Read(ReadBuff, 0, ReturnByte); if (ReadBuff[0] != WriteBuff[0] || ReadBuff[1] != WriteBuff[1] || ReadBuff[2] != WriteBuff[2] || ReadBuff[3] == 33)//数据检验失败 { cMain.WriteErrorToLog("IsDataError"); if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",PanasonicPlcRead," + ":初始失败,接收数据错误" + (char)13 + (char)10; } isOK = false; } else { isOK = true; } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":" + exc.ToString() + (char)13 + (char)10; } isOK = false; cMain.WriteErrorToLog(string.Format("{0}", isOK)); } return(isOK); }
/// <summary> ///将要发送的命令写入到SN板中 /// </summary> /// <param name="BaudRate">要发送机型的波特率</param> /// <param name="SendCmd">要发送的命令</param> /// <returns>返回发送是否成功</returns> public bool SnWriteCode(int BaudRate, string SendCmd) { if (cMain.isDebug) { return(true); } SendCmd = SendCmd.Trim(); int CurLen = SendCmd.Length; if ((SendCmd.Length % 2) == 1) { if (ErrStr.IndexOf("指令长度不正确") < 0) { ErrStr = ErrStr + DateTime.Now.ToShortDateString() + ":指令长度不正确" + "\n" + "\r"; } return(false); } else { SendCmd = SendCmd + "0000000000000000000000000000000000000000000000000000"; SendCmd = SendCmd.Substring(0, 44); } if (!mSnBoardInit) { SnBoardInit(); return(false); } int i; bool returnValue = false; byte[] WriteBuff = new byte[50]; //发送数据 WriteBuff[0] = 0x00; //写延时 WriteBuff[1] = 0x02; WriteBuff[2] = 0x0A; //读延时 WriteBuff[3] = 0x40; WriteBuff[4] = 0x00; // WriteBuff[5] = 0x10; WriteBuff[6] = 0x00; // WriteBuff[7] = 0x2C; switch (BaudRate) { case 600: if (CurLen == 40) { WriteBuff[8] = 0x2; WriteBuff[9] = 0x14; } else { WriteBuff[8] = 0x2; WriteBuff[9] = 0x12; } WriteBuff[10] = 0x09; WriteBuff[11] = 0xF2; break; case 1023: WriteBuff[8] = 0x02; WriteBuff[9] = 0x06; WriteBuff[10] = 0x05; WriteBuff[11] = 0xC9; break; case 1200: WriteBuff[8] = 0x02; WriteBuff[9] = 0x08; WriteBuff[10] = 0x04; WriteBuff[11] = 0xF5; break; default: WriteBuff[8] = 0x02; WriteBuff[9] = 0x12; WriteBuff[10] = 0x09; WriteBuff[11] = 0xF2; break; } returnValue = mStandarBoard.StandarBoardWritePoint(1, 6, WriteBuff); if (!returnValue) { if (errStr.IndexOf(mStandarBoard.ErrStr) < 0) { errStr = errStr + mStandarBoard.ErrStr; } return(false); } for (i = 0; i < (SendCmd.Length / 2); i++) { WriteBuff[i] = Num.ByteParseFromHex(SendCmd.Substring(2 * i, 2)); } SendSnStr = SendCmd.Trim(); returnValue = mStandarBoard.StandarBoardWritePoint(8, 11, WriteBuff); if (!returnValue) { if (errStr.IndexOf(mStandarBoard.ErrStr) < 0) { errStr = errStr + mStandarBoard.ErrStr; } } return(returnValue); //try //{ // if (!comPort.IsOpen) // { // comPort.Open(); // } // WriteBuff[0] = (byte)(StandarModebusAddress & 0xFF);//物理地址 // WriteBuff[1] = 0x10;//命令码 // WriteBuff[2] = 0x00;//地址高位 // WriteBuff[3] = 0x01;//地址低位 // WriteBuff[4] = 0x00;//数据长度高字节 // WriteBuff[5] = 0x06;//数据长度低字节 // WriteBuff[6] = 0x0C;//数据长度字节数 // WriteBuff[7] = 0x00;//写延时 // WriteBuff[8] = 0x02; // WriteBuff[9] = 0x0A;//读延时 // WriteBuff[10] = 0x40; // WriteBuff[11] = 0x00;// // WriteBuff[12] = 0x10; // WriteBuff[13] = 0x00;// // WriteBuff[14] = 0x2C; // //WriteBuff[15] = 0x00;// // //WriteBuff[16] = 0x10; // //WriteBuff[17] = 0x00;// // //WriteBuff[18] = 0x2C; // cMain.CRC_16(WriteBuff, 19, ref CrcLo, ref CrcHi); // WriteBuff[19] = CrcLo; // WriteBuff[20] = CrcHi; // comPort.DiscardOutBuffer();//刷新串口 // comPort.Write(WriteBuff, 0, WriteBuff[5] * 2 + 9); // NowTime = DateTime.Now; // do // { // if (comPort.BytesToRead >= 8)//收到数据 // { // ReturnByte = comPort.BytesToRead; // IsReturn = true; // } // 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; // } // return false; // } // else // { // comPort.Read(ReadBuff, 0, ReturnByte); // if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]))//数据检验失败 // { // if (ErrStr.IndexOf("写入失败,接收数据错误") < 0) // { // ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":写入失败,接收数据错误" + (char)13 + (char)10; // } // return false; // } // } //} //catch (Exception exc) //{ // if (ErrStr.IndexOf(exc.ToString()) < 0) // { // ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":" + exc.ToString() + (char)13 + (char)10; // } // return false; //} //try //{ // if (!comPort.IsOpen) // { // comPort.Open(); // } // WriteBuff[0] = (byte)(StandarModebusAddress & 0xFF);//物理地址 // WriteBuff[1] = 0x10;//命令码 // WriteBuff[2] = 0x00;//地址高位 // WriteBuff[3] = 0x08;//地址低位 // WriteBuff[4] = 0x00;//数据长度高字节 // WriteBuff[5] = 0x0B;//数据长度低字节 // WriteBuff[6] = 0x16;//数据长度字节数 // for (i = 0; i < (SendCmd.Length / 2); i++) // { // WriteBuff[7 + i] = Num.ByteParseFromHex(SendCmd.Substring(2 * i, 2)); // } // cMain.CRC_16(WriteBuff, (7 + SendCmd.Length / 2), ref CrcLo, ref CrcHi); // WriteBuff[(7 + SendCmd.Length / 2)] = CrcLo; // WriteBuff[(7 + SendCmd.Length / 2) + 1] = CrcHi; // comPort.DiscardOutBuffer();//刷新串口 // comPort.Write(WriteBuff, 0, (9 + WriteBuff[5])); // NowTime = DateTime.Now; // do // { // if (comPort.BytesToRead >= 8)//收到数据 // { // ReturnByte = comPort.BytesToRead; // IsReturn = true; // } // 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; // } // return false; // } // else // { // comPort.Read(ReadBuff, 0, ReturnByte); // if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]))//数据检验失败 // { // if (ErrStr.IndexOf("写入指令失败,接收数据错误") < 0) // { // ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":写入指令失败,接收数据错误" + (char)13 + (char)10; // } // return false; // } // } //} //catch (Exception exc) //{ // if (ErrStr.IndexOf(exc.ToString()) < 0) // { // ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":" + exc.ToString() + (char)13 + (char)10; // } // return false; //} //SendSnStr = SendCmd; //return true; }
public bool ReadIR(int StartPoint, int ReadLength, ref int[] ReturnBuff) { if (cMain.isDebug) { mOmronIsInit = true; return(true); } if (!mOmronIsInit) { init(); return(false); } byte[] WriteBuff = new byte[17]; //发送数据 byte[] ReadBuff = new byte[20 + ReadLength * 4]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 string StartPointStr; string ReadLengthStr; int ResultIndex; //PLC执行结果 ErrorList ResultList; byte CrcHi = 0, CrcLo = 0; //CRC校验 try { if (!comPort.IsOpen) { comPort.Open(); } StartPointStr = string.Format("{0:D4}", StartPoint); ReadLengthStr = string.Format("{0:D4}", ReadLength); WriteBuff[0] = Convert.ToByte('@'); // Encoding.ASCII.GetBytes(new char[] { '@' })[0]; WriteBuff[1] = Encoding.ASCII.GetBytes(string.Format("{0:D2}", OmronAddress))[0]; // Encoding.ASCII.GetBytes(new char[] { '0' }); WriteBuff[2] = Encoding.ASCII.GetBytes(string.Format("{0:D2}", OmronAddress))[1]; // Encoding.ASCII.GetBytes(new char[] { '0' }); WriteBuff[3] = Convert.ToByte('R'); //Encoding.ASCII.GetBytes(new char[] { 'R' })[0]; WriteBuff[4] = Convert.ToByte('R'); //Encoding.ASCII.GetBytes(new char[] { 'D' })[0]; WriteBuff[5] = Encoding.ASCII.GetBytes(StartPointStr)[0]; WriteBuff[6] = Encoding.ASCII.GetBytes(StartPointStr)[1]; WriteBuff[7] = Encoding.ASCII.GetBytes(StartPointStr)[2]; WriteBuff[8] = Encoding.ASCII.GetBytes(StartPointStr)[3]; WriteBuff[9] = Encoding.ASCII.GetBytes(ReadLengthStr)[0]; WriteBuff[10] = Encoding.ASCII.GetBytes(ReadLengthStr)[1]; WriteBuff[11] = Encoding.ASCII.GetBytes(ReadLengthStr)[2]; WriteBuff[12] = Encoding.ASCII.GetBytes(ReadLengthStr)[3]; OmronPlcCrc(WriteBuff, 0, 13, out CrcLo, out CrcHi); WriteBuff[13] = CrcLo; // WriteBuff[14] = CrcHi; // WriteBuff[15] = Convert.ToByte('*'); //*号 WriteBuff[16] = 13; //回车结束符 //string a = Encoding.ASCII.GetString(WriteBuff); comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 17); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); if (comPort.BytesToRead >= (11 + 4 * ReadLength))//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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 + ",ReadIR," + "接收数据已超时" + (char)13 + (char)10; } return(false); } else { //@{地址*2}{命令*2}{结果*2}{返回值*4}*个数{*}{回车} comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1])) { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",ReadIR," + "接收数据错误" + (char)13 + (char)10; } return(false); } else { ResultIndex = Num.IntParse(Encoding.ASCII.GetString(ReadBuff, 5, 2)); if (ResultIndex > 0)//数据检验失败 { ResultList = (ErrorList)ResultIndex; if (ErrStr.IndexOf(ResultList.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",ReadIR," + ResultList.ToString() + (char)13 + (char)10; } return(false); } else { for (int i = 0; i < ReadLength; i++) { string tmpStr = Encoding.ASCII.GetString(ReadBuff, 7 + i * 4, 4); int tmpInt = Convert.ToInt16(tmpStr, 16); ReturnBuff[i] = tmpInt; } } } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",ReadIR," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
public bool FxPlc_ReadX(int StartPoint, int len, out int ReadValue) { byte[] WriteBuff = new byte[11]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime; //当前时间 TimeSpan ts; //时间差 ReadValue = 0; len = Math.Min(len, 4); byte CrcHi = 0, CrcLo = 0;//CRC校验 if (cMain.isDebug) { ReadValue = 0; return(true); } if (!mFXPLCIsInit)//没有初始化 { FxPlcInit(); return(false); } try { StartPoint = 0x80 + StartPoint / 8; WriteBuff[0] = 0x02; WriteBuff[1] = 0x30; WriteBuff[2] = Encoding.ASCII.GetBytes(string.Format("{0:X1}", (int)(StartPoint / 4096) & 0x0F))[0]; WriteBuff[3] = Encoding.ASCII.GetBytes(string.Format("{0:X1}", (int)(StartPoint / 256) & 0x0F))[0]; WriteBuff[4] = Encoding.ASCII.GetBytes(string.Format("{0:X1}", (int)(StartPoint / 16) & 0x0F))[0]; WriteBuff[5] = Encoding.ASCII.GetBytes(string.Format("{0:X1}", (int)(StartPoint / 1) & 0x0F))[0]; WriteBuff[6] = Encoding.ASCII.GetBytes(string.Format("{0:X2}", len))[0]; WriteBuff[7] = Encoding.ASCII.GetBytes(string.Format("{0:X2}", len))[1]; WriteBuff[8] = 0x03; FxPlcCrc(WriteBuff, out CrcLo, out CrcHi); WriteBuff[9] = CrcLo; WriteBuff[10] = CrcHi; if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(WriteBuff, 0, 11); NowTime = DateTime.Now; do { Threading.Thread.Sleep(50); if (comPort.BytesToRead >= (4 + len * 2))//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",FxPlc_ReadM," + ":读取失败,接收数据已超时" + (char)13 + (char)10; } return(false); } comPort.Read(ReadBuff, 0, ReturnByte); ReadValue = Convert.ToInt32(Encoding.ASCII.GetString(ReadBuff, 1, 2 * len), 16); } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",FxPlc_ReadM," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
/// <summary> /// 初始化FXPLC /// </summary> /// <returns>bool,返回初始化结果</returns> public bool FxPlcInit() { if (cMain.isDebug) { mFXPLCIsInit = true; return(true); } byte[] WriteBuff = new byte[1]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff[0] = 5; comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 1); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(50); if (comPort.BytesToRead >= 1)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",FxPlcInit," + "初始失败,接收数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); if (ReadBuff[0] != 6)//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",FxPlcInit," + ":初始失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { mFXPLCIsInit = true; } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
public bool FxPlc_ReadD(int StartPoint, int length, out long[] ReadValue) { byte[] WriteBuff = new byte[11]; //发送数据 byte[] ReadBuff = new byte[100]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime; //当前时间 TimeSpan ts; //时间差 ReadValue = new long[length]; byte CrcHi = 0, CrcLo = 0;//CRC校验 if (cMain.isDebug) { return(true); } if (!mFXPLCIsInit)//没有初始化 { FxPlcInit(); return(false); } try { StartPoint = StartPoint * 2 + 0x1000; string startPoint = string.Format("{0:X4}", StartPoint); byte[] point = Encoding.ASCII.GetBytes(startPoint); WriteBuff[0] = 0x02; WriteBuff[1] = 0x30; WriteBuff[2] = point[0]; WriteBuff[3] = point[1]; WriteBuff[4] = point[2]; WriteBuff[5] = point[3]; WriteBuff[6] = Encoding.ASCII.GetBytes(string.Format("{0:X2}", length * 2))[0]; WriteBuff[7] = Encoding.ASCII.GetBytes(string.Format("{0:X2}", length * 2))[1]; WriteBuff[8] = 0x03; FxPlcCrc(WriteBuff, out CrcLo, out CrcHi); WriteBuff[9] = CrcLo; WriteBuff[10] = CrcHi; if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(WriteBuff, 0, 11); NowTime = DateTime.Now; do { Threading.Thread.Sleep(50); if (comPort.BytesToRead >= 4 + 4 * length)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",FxPlc_ReadD," + ":读取失败,接收数据已超时" + (char)13 + (char)10; } return(false); } comPort.Read(ReadBuff, 0, ReturnByte); for (int i = 0; i < length; i++) { ReadValue[i] = Convert.ToInt32(Encoding.ASCII.GetString(ReadBuff, 1 + i * 4, 2), 16) + 256 * Convert.ToInt32(Encoding.ASCII.GetString(ReadBuff, 3 + i * 4, 2), 16); } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",FxPlc_ReadD," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
/// <summary> /// 写入D点值 /// </summary> /// <param name="StartPoint">int,D点</param> /// <param name="Value"></param> /// <returns></returns> public bool FxPlc_WriteD(int StartPoint, int Value) { byte[] WriteBuff = new byte[15]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime; //当前时间 TimeSpan ts; //时间差 byte CrcHi = 0, CrcLo = 0; //CRC校验 string tempValueStr = ""; byte[] tempValueByte; if (cMain.isDebug) { Value = 0; return(true); } if (!mFXPLCIsInit)//没有初始化 { FxPlcInit(); return(false); } try { if (Value < 0) { Value = (0xFFFF & ((-(Value) - 1) ^ 0xFFFF)); } StartPoint = StartPoint * 2 + 0x1000; WriteBuff[0] = 0x02; WriteBuff[1] = 0x31; tempValueStr = string.Format("{0:X4}", StartPoint); tempValueByte = Encoding.ASCII.GetBytes(tempValueStr); WriteBuff[2] = tempValueByte[0]; WriteBuff[3] = tempValueByte[1]; WriteBuff[4] = tempValueByte[2]; WriteBuff[5] = tempValueByte[3]; WriteBuff[6] = 0x30; WriteBuff[7] = 0x32; tempValueStr = string.Format("{0:X4}", Value); tempValueByte = Encoding.ASCII.GetBytes(tempValueStr); WriteBuff[8] = tempValueByte[2]; WriteBuff[9] = tempValueByte[3]; WriteBuff[10] = tempValueByte[0]; WriteBuff[11] = tempValueByte[1]; WriteBuff[12] = 0x03; FxPlcCrc(WriteBuff, out CrcLo, out CrcHi); WriteBuff[13] = CrcLo; WriteBuff[14] = CrcHi; if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(WriteBuff, 0, 15); NowTime = DateTime.Now; do { Threading.Thread.Sleep(50); if (comPort.BytesToRead >= 1)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",FxPlc_WriteD," + ":写入失败,接收数据已超时" + (char)13 + (char)10; } return(false); } comPort.Read(ReadBuff, 0, ReturnByte); if (ReadBuff[0] != 06) { if (ErrStr.IndexOf("写入数据失败") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",FxPlc_WriteD," + ":写入失败,写入数据失败" + (char)13 + (char)10; } return(false); } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",FxPlc_WriteD," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
/// <summary> /// 写入M点状态 /// </summary> /// <param name="StartPoint">int,M点</param> /// <param name="Value">bool,写入M点值</param> /// <returns>bool,操作是否成功</returns> public bool FxPlc_WriteM(int StartPoint, bool Value) { byte[] WriteBuff = new byte[15]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime; //当前时间 TimeSpan ts; //时间差 byte CrcHi = 0, CrcLo = 0; //CRC校验 int tempAddress = 0x800 + StartPoint; byte[] Address = Encoding.ASCII.GetBytes(string.Format("{0:X4}", tempAddress)); if (cMain.isDebug) { Value = false; return(true); } if (!mFXPLCIsInit)//没有初始化 { FxPlcInit(); return(false); } try { WriteBuff[0] = 0x02; WriteBuff[1] = (byte)(Value ? 0x37 : 0x38); WriteBuff[2] = Address[2]; WriteBuff[3] = Address[3]; WriteBuff[4] = Address[0]; WriteBuff[5] = Address[1]; WriteBuff[6] = 0x03; FxPlcCrc(WriteBuff, out CrcLo, out CrcHi); WriteBuff[7] = CrcLo; WriteBuff[8] = CrcHi; if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(WriteBuff, 0, 9); NowTime = DateTime.Now; do { Threading.Thread.Sleep(50); if (comPort.BytesToRead >= 1)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",FxPlc_WriteD," + ":写入失败,接收数据已超时" + (char)13 + (char)10; } return(false); } comPort.Read(ReadBuff, 0, ReturnByte); if (ReadBuff[0] != 06) { if (ErrStr.IndexOf("写入数据失败") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",FxPlc_WriteD," + ":写入失败,写入数据失败" + (char)13 + (char)10; } return(false); } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",FxPlc_WriteD," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
public bool Stop() { if (cMain.isDebug) { return(true); } if (!mAiNuoIsInit) { Init(); return(false); } byte[] WriteBuff = new byte[10]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff[0] = 0x7B;// Encoding.ASCII.GetBytes(new char[] { '@' }); WriteBuff[1] = 0x07; WriteBuff[2] = (byte)((AiNuoAddress & 0xFF00) >> 8); WriteBuff[3] = (byte)(AiNuoAddress & 0xFF); WriteBuff[4] = 0x43; WriteBuff[5] = 0x53; WriteBuff[6] = 0x50; WriteBuff[7] = 0x2A; WriteBuff[8] = Sum_DianYuan(WriteBuff, 1, 7); WriteBuff[9] = 0x7D; //string a = Encoding.ASCII.GetString(WriteBuff); comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 10); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); if (comPort.BytesToRead >= 13)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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 + ",init," + "初始失败,接收数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[4] != 0x43) || (ReadBuff[7] != 0x3D) || (ReadBuff[8] != 0x3D))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",init," + ":初始失败,接收数据错误" + (char)13 + (char)10; } return(false); } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",init," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
public bool Set(int Vol, int Hz, int Group) { if (cMain.isDebug) { return(true); } if (!mAiNuoIsInit) { Init(); return(false); } bool isReadOk = false; int ReadVol, ReadHz, ReadGroup; byte[] WriteBuff = new byte[29]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 string WriteVolStr; string WriteHzStr; TimeSpan ts;//时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteVolStr = string.Format("{0:D3}", Vol); WriteHzStr = string.Format("{0:D4}", Hz * 10); WriteBuff[0] = 0x7B;// Encoding.ASCII.GetBytes(new char[] { '@' }); WriteBuff[1] = 0x1A; WriteBuff[2] = (byte)((AiNuoAddress & 0xFF00) >> 8); WriteBuff[3] = (byte)(AiNuoAddress & 0xFF); WriteBuff[4] = 0x53; WriteBuff[5] = 0x4E; WriteBuff[6] = 0x4F; WriteBuff[7] = 0x3D; WriteBuff[8] = Encoding.ASCII.GetBytes(WriteVolStr)[0];//电压 WriteBuff[9] = Encoding.ASCII.GetBytes(WriteVolStr)[1]; WriteBuff[10] = Encoding.ASCII.GetBytes(WriteVolStr)[2]; WriteBuff[11] = 0x2C; WriteBuff[12] = Encoding.ASCII.GetBytes(WriteHzStr)[0];//频率 WriteBuff[13] = Encoding.ASCII.GetBytes(WriteHzStr)[1]; WriteBuff[14] = Encoding.ASCII.GetBytes(WriteHzStr)[2]; WriteBuff[15] = Encoding.ASCII.GetBytes(WriteHzStr)[3]; WriteBuff[16] = 0x2C; WriteBuff[17] = 0x33;//上浮 WriteBuff[18] = 0x30; WriteBuff[19] = 0x2C; WriteBuff[20] = 0x33;//下浮 WriteBuff[21] = 0x30; WriteBuff[22] = 0x2C; WriteBuff[23] = Encoding.ASCII.GetBytes(Group.ToString("D1"))[0];//组别 WriteBuff[24] = 0x2C; WriteBuff[25] = 0x30;//高档不锁定 WriteBuff[26] = 0x2A; WriteBuff[27] = Sum_DianYuan(WriteBuff, 1, 26); WriteBuff[28] = 0x7D; comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 29); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); if (comPort.BytesToRead >= 13)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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 + ",init," + "初始失败,接收数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); //string a = Encoding.ASCII.GetString(ReadBuff); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[4] != 0x53) || (ReadBuff[7] != 0x3D) || (ReadBuff[8] != 0x3D))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",init," + ":初始失败,接收数据错误" + (char)13 + (char)10; } return(false); } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",init," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } Threading.Thread.Sleep(300); isReadOk = Read(out ReadVol, out ReadHz, out ReadGroup); if ((ReadVol != Vol) || (ReadHz != Hz) || (ReadGroup != Group)) { isReadOk = false; } return(isReadOk); }
//批量写入D点 /// <summary> /// 批量写入D点 /// </summary> /// <param name="StartPoint">int,写入D点的开始位</param> /// <param name="WriteLength">int,写入D点的数据长度</param> /// <param name="SendBuff">int[],写入D点的数值</param> /// <returns>bool,写入D点是否成功</returns> public bool WriteD(int StartPoint, int WriteLength, int[] SendBuff) { if (cMain.isDebug) { mOmronIsInit = true; return(true); } if (!mOmronIsInit) { init(); return(false); } WriteLength = Math.Min(WriteLength, SendBuff.Length); byte[] WriteBuff = new byte[20 + WriteLength * 4]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 string StartPointStr; string WriteLengthStr; string WriteDataStr; int ResultIndex; ErrorList ResultList; byte CrcHi = 0, CrcLo = 0;//CRC校验 try { if (!comPort.IsOpen) { comPort.Open(); } StartPointStr = string.Format("{0:D4}", StartPoint); WriteLengthStr = string.Format("{0:D4}", WriteLength); WriteBuff[0] = Convert.ToByte('@'); // Encoding.ASCII.GetBytes(new char[] { '@' })[0]; WriteBuff[1] = Encoding.ASCII.GetBytes(string.Format("{0:D2}", OmronAddress))[0]; // Encoding.ASCII.GetBytes(new char[] { '0' }); WriteBuff[2] = Encoding.ASCII.GetBytes(string.Format("{0:D2}", OmronAddress))[1]; // Encoding.ASCII.GetBytes(new char[] { '0' }); WriteBuff[3] = Convert.ToByte('W'); // Encoding.ASCII.GetBytes(new char[] { 'W' })[0]; WriteBuff[4] = Convert.ToByte('D'); // Encoding.ASCII.GetBytes(new char[] { 'D' })[0]; WriteBuff[5] = Encoding.ASCII.GetBytes(StartPointStr)[0]; WriteBuff[6] = Encoding.ASCII.GetBytes(StartPointStr)[1]; WriteBuff[7] = Encoding.ASCII.GetBytes(StartPointStr)[2]; WriteBuff[8] = Encoding.ASCII.GetBytes(StartPointStr)[3]; for (int i = 0; i < WriteLength; i++) { WriteDataStr = string.Format("{0:X4}", SendBuff[i]); byte[] tmpByte = Encoding.ASCII.GetBytes(WriteDataStr); WriteBuff[9 + 4 * i] = tmpByte[0]; WriteBuff[10 + 4 * i] = tmpByte[1]; WriteBuff[11 + 4 * i] = tmpByte[2]; WriteBuff[12 + 4 * i] = tmpByte[3]; } OmronPlcCrc(WriteBuff, 0, 9 + 4 * WriteLength, out CrcLo, out CrcHi); WriteBuff[9 + 4 * WriteLength] = CrcLo; // WriteBuff[10 + 4 * WriteLength] = CrcHi; // WriteBuff[11 + 4 * WriteLength] = Convert.ToByte('*'); //*号 WriteBuff[12 + 4 * WriteLength] = 13; //回车结束符 comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 13 + 4 * WriteLength); NowTime = DateTime.Now; do { Threading.Thread.Sleep(20); if (comPort.BytesToRead >= 11)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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 + ",WriteD," + "写入数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1])) { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",WriteD," + "写入数据错误" + (char)13 + (char)10; } return(false); } else { ResultIndex = Num.IntParse(Encoding.ASCII.GetString(ReadBuff, 5, 2)); if (ResultIndex > 0)//数据检验失败 { ResultList = (ErrorList)ResultIndex; if (ErrStr.IndexOf(ResultList.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",WriteD," + ResultList.ToString() + (char)13 + (char)10; } return(false); } } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",WriteD," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
}//写入W点 /// <summary> /// 写入W点 /// </summary> /// <param name="StartPoint">int,写入的W点,如W1.01,则StartPoint为101</param> /// <param name="SendBuff">bool,写入的值</param> /// <returns>bool,返回写入是否成功</returns> public bool WriteIR(int StartPoint, bool SendBuff) { if (cMain.isDebug) { mOmronIsInit = true; return(true); } if (!mOmronIsInit) { init(); return(false); } int oldData = 0; int StartZi = 0; //点的字 int StartWei = 0; //点的位 StartZi = StartPoint / 100; StartWei = StartPoint % 100; if (!ReadIR(StartZi, ref oldData))//先读原来的数据 { return(false); } else { int tmpWei = 0; tmpWei = (int)(Math.Pow(2, StartWei)); if (!SendBuff) { tmpWei = 65535 - tmpWei; oldData = oldData & tmpWei; } else { oldData = oldData | tmpWei; } } byte[] WriteBuff = new byte[18]; //发送数据 byte[] ReadBuff = new byte[40]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 string StartPointStr; string StartValueStr; int ResultIndex; //PLC执行结果 ErrorList ResultList; byte CrcHi = 0, CrcLo = 0; //CRC校验 try { if (!comPort.IsOpen) { comPort.Open(); } StartPointStr = string.Format("{0:D4}", StartZi); StartValueStr = string.Format("{0:X4}", oldData); WriteBuff[0] = Convert.ToByte('@'); // Encoding.ASCII.GetBytes(new char[] { '@' })[0]; WriteBuff[1] = Encoding.ASCII.GetBytes(string.Format("{0:D2}", OmronAddress))[0]; // Encoding.ASCII.GetBytes(new char[] { '0' }); WriteBuff[2] = Encoding.ASCII.GetBytes(string.Format("{0:D2}", OmronAddress))[1]; // Encoding.ASCII.GetBytes(new char[] { '0' }); WriteBuff[3] = Convert.ToByte('W'); WriteBuff[4] = Convert.ToByte('R'); for (int i = 0; i < StartPointStr.Length; i++) { WriteBuff[5 + i] = Encoding.ASCII.GetBytes(StartPointStr)[i]; } for (int i = 0; i < StartValueStr.Length; i++) { WriteBuff[9 + i] = Encoding.ASCII.GetBytes(StartValueStr)[i]; } OmronPlcCrc(WriteBuff, 0, 13, out CrcLo, out CrcHi); WriteBuff[13] = CrcLo; // WriteBuff[14] = CrcHi; // WriteBuff[15] = Convert.ToByte('*'); //*号 WriteBuff[16] = 13; //回车结束符 //string a = Encoding.ASCII.GetString(WriteBuff); comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 17); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); if (comPort.BytesToRead >= 11)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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 + ",WriteIR," + "接收数据已超时" + (char)13 + (char)10; } return(false); } else { //@{地址*2}{命令*2}{结果*2}{返回值*4}*个数{*}{回车} comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1])) { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",WriteIR," + "接收数据错误" + (char)13 + (char)10; } return(false); } else { //string b = Encoding.ASCII.GetString(ReadBuff); ResultIndex = Num.IntParse(Encoding.ASCII.GetString(ReadBuff, 5, 2)); if (ResultIndex > 0)//数据检验失败 { ResultList = (ErrorList)ResultIndex; if (ErrStr.IndexOf(ResultList.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",WriteIR," + ResultList.ToString() + (char)13 + (char)10; } return(false); } } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",WriteIR," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
/// <summary> /// 将多个数据写入到线圈,功能码为0x0F /// </summary> /// <param name="StartAdd">写入线圈的开始地址</param> /// <param name="mWriteBuff">写入的数据</param> /// <returns>返回写入是否成功</returns> public bool StandarBoardWritePoint(int StartAdd, bool[] mWriteBuff) { int PointCount = (int)Math.Ceiling((double)((double)(mWriteBuff.Length) / 8.00000)); byte[] WriteBuff = new byte[PointCount + 9]; byte[] ReadBuff = new byte[8]; int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 byte[] tWriteBuff = new byte[PointCount]; DateTime NowTime; //当前时间 TimeSpan ts; //时间差 byte CrcHi = 0, CrcLo = 0; //CRC校验 int i; if (cMain.isDebug) { return(true); } if (!mStandarBoardInit)//没有初始化 { StandarBoardInit(); return(false); } try { WriteBuff[0] = _StandarModebusAddress; WriteBuff[1] = 0x0F; WriteBuff[2] = (byte)((StartAdd & 0xFF00) >> 8); WriteBuff[3] = (byte)(StartAdd & 0xFF); WriteBuff[4] = (byte)((mWriteBuff.Length & 0xFF00) >> 8); WriteBuff[5] = (byte)(mWriteBuff.Length & 0xFF); WriteBuff[6] = (byte)(PointCount & 0xFF); for (i = 0; i < PointCount; i++) { tWriteBuff[i] = 0; } for (int j = 0; j < PointCount; j++) { for (i = 0; i < 8; i++) { if ((j * 8 + i) < mWriteBuff.Length) { if (mWriteBuff[j * 8 + i]) { tWriteBuff[j] = (byte)(tWriteBuff[j] + 2 ^ i); } } } } cMain.CRC_16(WriteBuff, PointCount + 7, ref CrcLo, ref CrcHi); WriteBuff[PointCount + 7] = CrcLo; WriteBuff[PointCount + 8] = CrcHi; if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(WriteBuff, 0, PointCount + 9); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); if (comPort.BytesToRead >= 8)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",StandarBoardWritePoint," + ":读取失败,接收数据已超时" + (char)13 + (char)10; } return(false); } comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardWritePoint," + ":读取失败,接收数据错误" + (char)13 + (char)10; } return(false); } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardWritePoint," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
//初始化PLC /// <summary> /// 初始化PLC /// </summary> /// <returns>bool,返回初始化是否成功</returns> public bool init() { if (cMain.isDebug) { mOmronIsInit = true; return(true); } byte[] WriteBuff = new byte[10]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 byte CrcHi = 0, CrcLo = 0; //CRC校验 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff[0] = 64; // Encoding.ASCII.GetBytes(new char[] { '@' }); WriteBuff[1] = Encoding.ASCII.GetBytes(string.Format("{0:D2}", OmronAddress))[0]; // Encoding.ASCII.GetBytes(new char[] { '0' }); WriteBuff[2] = Encoding.ASCII.GetBytes(string.Format("{0:D2}", OmronAddress))[1]; // Encoding.ASCII.GetBytes(new char[] { '0' }); WriteBuff[3] = 77; // Encoding.ASCII.GetBytes(new char[] { 'M' }); WriteBuff[4] = 77; // Encoding.ASCII.GetBytes(new char[] { 'M' }); OmronPlcCrc(WriteBuff, 0, 5, out CrcLo, out CrcHi); WriteBuff[5] = CrcLo; // WriteBuff[6] = CrcHi; // WriteBuff[7] = 42; //*号 WriteBuff[8] = 13; //回车结束符 //string a = Encoding.ASCII.GetString(WriteBuff); comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 9); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); if (comPort.BytesToRead >= 13)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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 + ",init," + "初始失败,接收数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]) || (ReadBuff[5] != 48) || (ReadBuff[6] != 48))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",init," + ":初始失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { mOmronIsInit = true; } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",init," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(ChangeStatue(PlcStatue.JianShi)); }
/// <summary> /// 专用于7188,将指定指令发送到模块,并返回设置是否成功 /// </summary> /// <param name="mWriteBuff">byte[],发送7188的指令</param> /// <returns>bool,返回设置指令是否成功</returns> public bool StandarBoard7188Set(byte[] mWriteBuff) { byte[] mReadBuff = new byte[100]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime; //当前时间 TimeSpan ts; //时间差 if (cMain.isDebug) { return(true); } if (!mStandarBoardInit)//没有初始化 { StandarBoardInit(); return(false); } try { if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(mWriteBuff, 0, mWriteBuff.Length); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); if (comPort.BytesToRead >= mWriteBuff.Length)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",StandarBoardRead," + ":读取失败,接收数据已超时" + (char)13 + (char)10; } return(false); } comPort.Read(mReadBuff, 0, ReturnByte); if ((mReadBuff[0] != mWriteBuff[0]) || (mReadBuff[1] != mWriteBuff[1]))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":读取失败,接收数据错误" + (char)13 + (char)10; } return(false); } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
public bool ReadD(int DStartValue, int Length, ref long[] value) { if (!mPanasonicPlcIsInit) { Init(); return(false); } bool isOK = false; string WriteStr = "";//发送数据 byte[] WriteBuff = new byte[17]; byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 string CrcHi = "", CrcLo = ""; TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteStr = string.Format("<{0:D2}#RDD{1:D5}{2:D5}", PanasonicPlcAddress, DStartValue, DStartValue + Length - 1); Crc(WriteStr, ref CrcLo, ref CrcHi); WriteStr = WriteStr + CrcLo + CrcHi + "\r\n"; comPort.DiscardInBuffer(); WriteBuff = Encoding.ASCII.GetBytes(WriteStr); comPort.Write(WriteBuff, 0, WriteBuff.Length); NowTime = DateTime.Now; do { Thread.Sleep(20); if (comPort.BytesToRead >= 9 + Length * 4)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",PanasonicPlcRead," + "初始失败,接收数据已超时" + (char)13 + (char)10; } isOK = false; } else { comPort.Read(ReadBuff, 0, ReturnByte); if (ReadBuff[0] != WriteBuff[0] || ReadBuff[1] != WriteBuff[1] || ReadBuff[2] != WriteBuff[2] || ReadBuff[3] == 33)//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",PanasonicPlcRead," + ":初始失败,接收数据错误" + (char)13 + (char)10; } isOK = false; } else { for (int i = 0; i < Length; i++) { value[i] = Convert.ToInt32((Encoding.ASCII.GetString(ReadBuff, 8 + i * 4, 2) + Encoding.ASCII.GetString(ReadBuff, 6 + i * 4, 2)), 16); } isOK = true; } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":" + exc.ToString() + (char)13 + (char)10; } isOK = false; } return(isOK); }
/// <summary> /// 标准模块初始化 /// </summary> /// <returns>返回初始化是否成功</returns> public bool StandarBoardInit() { if (cMain.isDebug) { mStandarBoardInit = true; return(true); } byte[] WriteBuff = new byte[10]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 byte CrcHi = 0, CrcLo = 0; //CRC校验 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff[0] = (byte)(_StandarModebusAddress & 0xFF); WriteBuff[1] = 0x03; WriteBuff[2] = 0x00; WriteBuff[3] = 0x00; WriteBuff[4] = 0x00; WriteBuff[5] = 0x01; cMain.CRC_16(WriteBuff, 6, ref CrcLo, ref CrcHi); WriteBuff[6] = CrcLo; WriteBuff[7] = CrcHi; comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 8); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); if (comPort.BytesToRead >= WriteBuff[5] * 2 + 5)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",StandarBoardInit," + "初始失败,接收数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":初始失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { mStandarBoardInit = true; } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
public bool Init() { if (cMain.isDebug) { mPanasonicPlcIsInit = true; return(true); } string WriteBuff = ""; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 string CrcHi = "", CrcLo = ""; TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff = string.Format("<{0:D2}#RCSX0000", PanasonicPlcAddress); Crc(WriteBuff, ref CrcLo, ref CrcHi); WriteBuff = WriteBuff + CrcLo + CrcHi + "\r\n"; comPort.DiscardInBuffer(); comPort.Write(WriteBuff); NowTime = DateTime.Now; do { Thread.Sleep(20); if (comPort.BytesToRead >= 10)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",PanasonicPlcInit," + "初始失败,接收数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); string readStr = Encoding.ASCII.GetString(ReadBuff, 0, ReturnByte); if (readStr.IndexOf('!') > 0)//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",PanasonicPlcInit," + ":初始失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { mPanasonicPlcIsInit = true; } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
/// <summary> /// 专用于7188,将指定指令发送到模块,并返回所须要的数据 /// </summary> /// <param name="mWriteBuff">byte[],须要发送出去的指令</param> /// <param name="mReadBuff">byte[] ,返回数据</param> /// <returns>bool,读取数据是否成功</returns> public bool StandarBoard7188Read(byte[] mWriteBuff, out byte[] ReadBuff) { int i, tempInt; ReadBuff = new byte[1]; byte[] mReadBuff = new byte[100]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime; //当前时间 TimeSpan ts; //时间差 if (!mStandarBoardInit) //没有初始化 { StandarBoardInit(); return(false); } //byte NowByte; try { if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(mWriteBuff, 0, mWriteBuff.Length); tempInt = 0; NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); tempInt = comPort.BytesToRead; if (tempInt >= 9)//收到数据 { Threading.Thread.Sleep(50); ReturnByte = comPort.BytesToRead; if (ReturnByte == tempInt) { IsReturn = true; } } 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() + ",StandarBoardRead," + ":读取失败,接收数据已超时" + (char)13 + (char)10; } return(false); } if (ReturnByte > 100) { if (errStr.IndexOf("数据长度错误") < 0) { errStr = errStr + DateTime.Now.ToString() + ":读取失败,数据长度错误或过长" + (char)13 + (char)10; } } else { comPort.Read(mReadBuff, 0, ReturnByte); } if ((mReadBuff[0] != mWriteBuff[0]) || (mReadBuff[1] != mWriteBuff[1]))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":读取失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { if (mReadBuff[5] > 0) { ReadBuff = new byte[mReadBuff[6]]; for (i = 0; i < mReadBuff[6]; i++) { ReadBuff[i] = mReadBuff[i + 7]; } } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
public bool AiNuo9641Read(out AiNuo9641Data _aiNuo9641Data) { aiNuo9641Data.mAiNuoStatue = TestStatueEnum.None; for (int i = 0; i < aiNuo9641Data.mAiNuo9641Data.Length; i++) { aiNuo9641Data.mAiNuo9641Data[i].mTestData = 0; aiNuo9641Data.mAiNuo9641Data[i].mTestName = TestNameEnum.None; aiNuo9641Data.mAiNuo9641Data[i].mTestResult = TestStepResultEnum.None; } bool returnValue = false; if (!mAiNuo9641Init) { AiNuo9641Init(); _aiNuo9641Data = aiNuo9641Data; return(false); } byte[] WriteBuff = new byte[9]; //发送数据 byte[] ReadBuff = new byte[400]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff[0] = 0x7B; WriteBuff[1] = _AiNuo9641BAddress[0]; WriteBuff[2] = _AiNuo9641BAddress[1]; WriteBuff[3] = _AiNuo9641BAddress[2]; WriteBuff[4] = 0x30; WriteBuff[5] = 0x30; WriteBuff[6] = 0x30; WriteBuff[7] = 0x7D; comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 8); NowTime = DateTime.Now; do { if (comPort.BytesToRead >= 193)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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; } returnValue = false; } else { comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]) || (ReadBuff[2] != WriteBuff[2]))//数据检验失败 { //comPort.Close(); if (ErrStr.IndexOf("读数失败,接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":读数失败,接收数据错误" + (char)13 + (char)10; } returnValue = false; } else { switch (ReadBuff[189]) { case 0x30: aiNuo9641Data.mAiNuoStatue = TestStatueEnum.Before; break; case 0x31: aiNuo9641Data.mAiNuoStatue = TestStatueEnum.Run; break; case 0x32: aiNuo9641Data.mAiNuoStatue = TestStatueEnum.Over; break; case 0x37: aiNuo9641Data.mAiNuoStatue = TestStatueEnum.None; break; default: aiNuo9641Data.mAiNuoStatue = TestStatueEnum.None; break; } if (aiNuo9641Data.mAiNuoStatue == TestStatueEnum.Over) { for (int i = 0; i < 7; i++) { int DataStart = 6; switch (ReadBuff[4 + i * 23 + 1]) { case 0x31: aiNuo9641Data.mAiNuo9641Data[i].mTestName = TestNameEnum.JieDi; break; case 0x32: aiNuo9641Data.mAiNuo9641Data[i].mTestName = TestNameEnum.JueYuan; break; case 0x33: aiNuo9641Data.mAiNuo9641Data[i].mTestName = TestNameEnum.NaiYa; break; case 0x34: aiNuo9641Data.mAiNuo9641Data[i].mTestName = TestNameEnum.XieLou; break; case 0: default: aiNuo9641Data.mAiNuo9641Data[i].mTestName = TestNameEnum.None; break; } switch (ReadBuff[4 + i * 23 + 23]) { case 0x30: aiNuo9641Data.mAiNuo9641Data[i].mTestResult = TestStepResultEnum.None; break; case 0x31: aiNuo9641Data.mAiNuo9641Data[i].mTestResult = TestStepResultEnum.OK; break; case 0x32: aiNuo9641Data.mAiNuo9641Data[i].mTestResult = TestStepResultEnum.NG1; break; case 0x33: aiNuo9641Data.mAiNuo9641Data[i].mTestResult = TestStepResultEnum.PT; break; case 0x34: aiNuo9641Data.mAiNuo9641Data[i].mTestResult = TestStepResultEnum.NG2; break; default: aiNuo9641Data.mAiNuo9641Data[i].mTestResult = TestStepResultEnum.None; break; } byte[] tmpByte = new byte[4]; string tmpStr = ""; for (int j = 0; j < 4; j++) { tmpByte[j] = ReadBuff[4 + i * 23 + DataStart + j]; } tmpStr = Encoding.ASCII.GetString(tmpByte).Trim(); if (NumRex.IsMatch(tmpStr)) { aiNuo9641Data.mAiNuo9641Data[i].mTestData = Num.DoubleParse(tmpStr); switch (aiNuo9641Data.mAiNuo9641Data[i].mTestName) { case TestNameEnum.JieDi: case TestNameEnum.JueYuan: aiNuo9641Data.mAiNuo9641Data[i].mTestData /= 10; break; case TestNameEnum.NaiYa: aiNuo9641Data.mAiNuo9641Data[i].mTestData /= 100; break; case TestNameEnum.XieLou: aiNuo9641Data.mAiNuo9641Data[i].mTestData /= 1000; break; default: aiNuo9641Data.mAiNuo9641Data[i].mTestData /= 10; break; } } else { aiNuo9641Data.mAiNuo9641Data[i].mTestData = 9999; } } } returnValue = true; } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ":" + exc.ToString() + (char)13 + (char)10; } } _aiNuo9641Data = aiNuo9641Data; return(returnValue); }
/// <summary> /// 读取寄存器数据 /// </summary> /// <param name="StartAdd">int,寄存器开始地址</param> /// <param name="PointCount">int,读取个数</param> /// <param name="mReturnBuff">byte[],读取到的数据,mReturnBuff.Length应该为PointCount*2</param> /// <returns></returns> public bool StandarBoardRead(int StartAdd, int PointCount, ref byte[] mReturnBuff) { int i; byte[] WriteBuff = new byte[10]; //发送数据 byte[] ReadBuff = new byte[2 * PointCount + 5]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime; //当前时间 TimeSpan ts; //时间差 byte CrcHi = 0, CrcLo = 0; //CRC校验 if (cMain.isDebug) { for (i = 0; i < PointCount; i++) { mReturnBuff[i] = (byte)(20 + (byte)(Num.Rand() * 10)); } return(true); } if (!mStandarBoardInit)//没有初始化 { StandarBoardInit(); return(false); } //byte NowByte; try { WriteBuff[0] = _StandarModebusAddress; WriteBuff[1] = 0x03; WriteBuff[2] = (byte)((StartAdd & 0xFF00) >> 8); WriteBuff[3] = (byte)(StartAdd & 0x00FF); WriteBuff[4] = (byte)((PointCount & 0xFF00) >> 8); WriteBuff[5] = (byte)(PointCount & 0x00FF); cMain.CRC_16(WriteBuff, 6, ref CrcLo, ref CrcHi); WriteBuff[6] = CrcLo; WriteBuff[7] = CrcHi; if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(WriteBuff, 0, 8); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); if (comPort.BytesToRead >= (2 * WriteBuff[5] + 5))//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",StandarBoardRead," + ":读取失败,接收数据已超时" + (char)13 + (char)10; } return(false); } comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":读取失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { for (i = 0; i < PointCount * 2; i++) { mReturnBuff[i] = ReadBuff[i + 3]; } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
public bool ReadData(ref double[] ReadData) { bool isOk = false; //if (cMain.isDebug) //{ // for (int i = 0; i < ReadData.Length; i++) // { // ReadData[i] = (byte)(20 + i + (byte)(Num.Rand())*10); // } // return true; //} //if (!c7017IsInit)//没有初始化 //{ // init(); // return false; //} string tmpReadData; //收到数据 string WriteBuff = ""; //发送数据 byte[] ReadBuff = new byte[1024]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff = string.Format("#{0:D2}\r", c7017Address); comPort.DiscardInBuffer(); comPort.Write(WriteBuff); NowTime = DateTime.Now; do { Threading.Thread.Sleep(50); if (comPort.BytesToRead == 58 || comPort.BytesToRead == 44)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",StandarBoardInit," + "初始失败,接收数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != 0x3E))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":初始失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { tmpReadData = Encoding.ASCII.GetString(ReadBuff, 0, ReturnByte).Trim(); if (tmpReadData.StartsWith(">")) { tmpReadData = tmpReadData.Substring(1); } string[] tmp = tmpReadData.Split(new char[] { '+', '-' }, StringSplitOptions.RemoveEmptyEntries); int MinIndex = Math.Min(tmp.Length, ReadData.Length); for (int i = 0; i < MinIndex; i++) { ReadData[i] = Num.DoubleParse(tmp[i]); } } isOk = true; } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":" + exc.ToString() + (char)13 + (char)10; } isOk = false; } return(isOk); }
/// <summary> /// 读取模块中多个数据 /// </summary> /// <param name="StartAdd">读取数据开始寄存器</param> /// <param name="PointCount">读取寄存器个数</param> /// <param name="mReturnBuff">读取寄存器数据</param> /// <returns>读取数据是否成功</returns> public bool StandarBoardRead(int StartAdd, int PointCount, ref long[] mReturnBuff) { int i; long[] ReturnBuff = new long[PointCount]; byte[] WriteBuff = new byte[10]; //发送数据 byte[] ReadBuff = new byte[2 * PointCount + 5]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime; //当前时间 TimeSpan ts; //时间差 byte CrcHi = 0, CrcLo = 0; //CRC校验 if (cMain.isDebug) { for (i = 0; i < PointCount; i++) { mReturnBuff[i] = 0 + (long)(Num.Rand() * 10); } return(true); } if (!mStandarBoardInit)//没有初始化 { StandarBoardInit(); return(false); } try { //05 03 24 10 00 06 CF 79 WriteBuff[0] = _StandarModebusAddress; WriteBuff[1] = 0x03; WriteBuff[2] = (byte)((StartAdd & 0xFF00) >> 8); WriteBuff[3] = (byte)(StartAdd & 0x00FF); WriteBuff[4] = (byte)((PointCount & 0xFF00) >> 8); WriteBuff[5] = (byte)(PointCount & 0x00FF); cMain.CRC_16(WriteBuff, 6, ref CrcLo, ref CrcHi); WriteBuff[6] = CrcLo; WriteBuff[7] = CrcHi; if (!comPort.IsOpen) { comPort.Open(); } comPort.DiscardInBuffer();//刷新串口 comPort.Write(WriteBuff, 0, 8); NowTime = DateTime.Now; do { //System.Windows.Forms.Application.DoEvents(); Threading.Thread.Sleep(20); if (comPort.BytesToRead >= (2 * WriteBuff[5] + 5))//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",StandarBoardRead," + ":读取失败,接收数据已超时" + (char)13 + (char)10; } return(false); } comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1]))//数据检验失败 { //string tempStr = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{3},{14},{15},{16},{17},{18},{19},{20}", // ReadBuff[0], ReadBuff[1], ReadBuff[2], ReadBuff[3], ReadBuff[4], ReadBuff[5], ReadBuff[6], ReadBuff[7], ReadBuff[8], // ReadBuff[9], ReadBuff[10], ReadBuff[11], ReadBuff[12], ReadBuff[13], ReadBuff[14], ReadBuff[15], ReadBuff[16], ReadBuff[17] // , ReadBuff[18], ReadBuff[19], ReadBuff[20]); //cMain.WriteErrorToLog(tempStr); if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":读取失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { for (i = 0; i < ReturnBuff.Length; i++) { ReturnBuff[i] = (ReadBuff[i * 2 + 3] & 0xFF) * 256 + (ReadBuff[i * 2 + 4] & 0xFF); //if ((ReadBuff[i * 2 + 3] >> 7) == 1)//如果是负数 //{ // ReturnBuff[i] = -((ReturnBuff[i] ^ 65535) + 1); //} mReturnBuff[i] = ReturnBuff[i]; } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardRead," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
public bool init() { if (cMain.isDebug) { c7017IsInit = true; return(true); } string WriteBuff = ""; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff = string.Format("${0:D2}M\r", c7017Address); comPort.DiscardInBuffer(); comPort.Write(WriteBuff); NowTime = DateTime.Now; do { Threading.Thread.Sleep(20); if (comPort.BytesToRead >= 8)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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() + ",StandarBoardInit," + "初始失败,接收数据已超时" + (char)13 + (char)10; } return(false); } else { ReturnByte = comPort.BytesToRead; comPort.Read(ReadBuff, 0, ReturnByte); //string s = Encoding.ASCII.GetString(ReadBuff); if ((ReadBuff[0] != 0x21) || (ReadBuff[ReturnByte - 1] != 0x0D))//数据检验失败 { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":初始失败,接收数据错误" + (char)13 + (char)10; } return(false); } else { c7017IsInit = true; } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + ",StandarBoardInit," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }
//改变PLC运行状态 /// <summary> /// 改变PLC运行状态 /// </summary> /// <param name="mPlcStatue">PlcStatue,要设置的PLC状态</param> /// <returns>bool,设置是否写入成功</returns> public bool ChangeStatue(PlcStatue mPlcStatue) { if (cMain.isDebug) { return(true); } if (!mOmronIsInit) { init(); return(false); } byte[] WriteBuff = new byte[12]; //发送数据 byte[] ReadBuff = new byte[20]; //接收数据 int ReturnByte = 0; //返回数据 bool IsReturn = false; //是否成功返回 bool IsTimeOut = false; //是否超时 DateTime NowTime = DateTime.Now; //当前时间 TimeSpan ts; //时间差 string WriteDataStr; int ResultIndex; ErrorList ResultList; byte CrcHi = 0, CrcLo = 0;//CRC校验 try { if (!comPort.IsOpen) { comPort.Open(); } WriteBuff[0] = Convert.ToByte('@'); // Encoding.ASCII.GetBytes(new char[] { '@' })[0]; WriteBuff[1] = Encoding.ASCII.GetBytes(string.Format("{0:D2}", OmronAddress))[0]; // Encoding.ASCII.GetBytes(new char[] { '0' }); WriteBuff[2] = Encoding.ASCII.GetBytes(string.Format("{0:D2}", OmronAddress))[1]; // Encoding.ASCII.GetBytes(new char[] { '0' }); WriteBuff[3] = Convert.ToByte('S'); // Encoding.ASCII.GetBytes(new char[] { 'W' })[0]; WriteBuff[4] = Convert.ToByte('C'); // Encoding.ASCII.GetBytes(new char[] { 'D' })[0]; WriteDataStr = string.Format("{0:D2}", (int)mPlcStatue); WriteBuff[5] = Encoding.ASCII.GetBytes(WriteDataStr)[0]; WriteBuff[6] = Encoding.ASCII.GetBytes(WriteDataStr)[1]; OmronPlcCrc(WriteBuff, 0, 7, out CrcLo, out CrcHi); WriteBuff[7] = CrcLo; // WriteBuff[8] = CrcHi; // WriteBuff[9] = Convert.ToByte('*'); //*号 WriteBuff[10] = 13; //回车结束符 comPort.DiscardInBuffer(); comPort.Write(WriteBuff, 0, 11); NowTime = DateTime.Now; do { Threading.Thread.Sleep(20); if (comPort.BytesToRead >= 11)//收到数据 { ReturnByte = comPort.BytesToRead; IsReturn = true; } 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 + ",ChangeStatue," + "写入数据已超时" + (char)13 + (char)10; } return(false); } else { comPort.Read(ReadBuff, 0, ReturnByte); if ((ReadBuff[0] != WriteBuff[0]) || (ReadBuff[1] != WriteBuff[1])) { if (ErrStr.IndexOf("接收数据错误") < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",ChangeStatue," + "写入数据错误" + (char)13 + (char)10; } return(false); } else { ResultIndex = Num.IntParse(Encoding.ASCII.GetString(ReadBuff, 5, 2)); if (ResultIndex > 0)//数据检验失败 { ResultList = (ErrorList)ResultIndex; if (ErrStr.IndexOf(ResultList.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",ChangeStatue," + ResultList.ToString() + (char)13 + (char)10; } return(false); } } } } catch (Exception exc) { if (ErrStr.IndexOf(exc.ToString()) < 0) { ErrStr = ErrStr + DateTime.Now.ToString() + mName + ",ChangeStatue," + ":" + exc.ToString() + (char)13 + (char)10; } return(false); } return(true); }