/// <summary> /// 发送线圈消息-同步 /// </summary> /// <param name="coil">线圈</param> /// <param name="Press">是否按下 值【0:False;1:True】</param> internal bool CoilMsgSync(Coil coil, bool Press) { lock (WaitSync) { try { InputModule input = new InputModule(); input.bySlaveID = m_SlaveID; input.byFunction = Modbus.byWRITE_SINGLE_COIL; input.nStartAddr = coil.Addr; input.nDataLength = Coil.Size; input.byWriteData = new byte[] { Press ? (byte)255 : (byte)0, 0x00 }; return(SendListMsg(input)); } catch (Exception ex) { log.AddERRORLOG("同步消息发送异常:" + ex.Message); return(false); } finally { } } }
/// <summary> /// 修改单个线圈的值 /// </summary> /// <param name="coil">线圈</param> /// <param name="Press">是否按下 值【0:True;1:False】</param> internal void AddMsgList(Coil coil, bool Press) { if (coil.Value == Press) { return; //相等则不修改 } InputModule input = new InputModule(); input.byFunction = Modbus.byWRITE_SINGLE_COIL; input.nStartAddr = coil.Addr; input.nDataLength = Coil.Size; input.byWriteData = new byte[] { Press ? (byte)0 : (byte)255, 0x00 }; AddMsgList(input); }
public bool SendMsg(Coil coil, bool Press) { try { InputModule input = new InputModule(); input.byFunction = Modbus.byWRITE_SINGLE_COIL; input.nStartAddr = coil.Addr; input.nDataLength = Coil.Size; input.byWriteData = new byte[] { Press ? (byte)0 : (byte)255, 0x00 }; OutputModule rev = null; rev = m_Modbus.SendMessage_Sync(input); int count = 0; for (; ;) { if (rev != null) { return(true); } if ((rev == null) || (rev.byFunction != input.byFunction)) { rev = m_Modbus.SendMessage_Sync(input); count++; } if (count > 3) { throw new Exception("发送线圈消息通信异常!"); } Thread.Sleep(200); } } catch (Exception ex) { log.AddERRORLOG("发送线圈信息异常:" + ex.Message); return(false); } }
/// <summary> /// 检查线圈的值 【1:False;0:True】 /// </summary> /// <param name="coil">判断的线圈</param> private int CheckCoil(Coil coil) { return(coil.Value ? 0 : 1); }