public bool cHJWriteIO(bool Led1, bool Led2, bool Led3) { bool returnValue = false; byte WriteValue = 0; if (Led1) { WriteValue = (byte)(WriteValue + 1); } if (Led2) { WriteValue = (byte)(WriteValue + 2); } if (Led3) { WriteValue = (byte)(WriteValue + 4); } returnValue = mStandarBoard.StandarBoardWritePoint(8, WriteValue, 6); if (!returnValue) { if (errStr.IndexOf(mStandarBoard.ErrStr) < 0) { errStr = errStr + mStandarBoard.ErrStr; } } return(returnValue); }
/// <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; }