Ejemplo n.º 1
0
 public bool SendMsg(HoldingRegister register, int Value)
 {
     try
     {
         InputModule input = new InputModule();
         input.nStartAddr  = register.Addr;
         input.nDataLength = register.Size;
         if (register.Size > 1)
         {
             input.byFunction  = Modbus.byWRITE_MULTI_HOLDING_REG;
             input.byWriteData = ModbusTool.HostToNetOrder32(Value);
         }
         else
         {
             input.byFunction  = Modbus.byWRITE_SINGLE_HOLDING_REG;
             input.byWriteData = ModbusTool.HostToNetOrder16((short)Value);
         }
         OutputModule rev = null;
         rev = m_Modbus.SendMessage_Sync(input);
         if ((rev == null) || (rev.byFunction != input.byFunction))
         {
             throw new Exception("发送寄存器消息通信异常!");
         }
         else
         {
             return(true);
         }
     }
     catch (Exception ex)
     {
         log.AddERRORLOG("发送寄存器信息异常:" + ex.Message);
         return(false);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 修改单个保持寄存器的值
        /// </summary>
        /// <param name="register">保持寄存器</param>
        /// <param name="Value">修改的值(包含小数的值,将数值*100后写入)</param>
        internal void AddMsgList(HoldingRegister register, int Value)
        {
            InputModule input = new InputModule();

            input.nStartAddr  = register.Addr;
            input.nDataLength = register.Size;
            if (register.Size > 1)
            {
                input.byFunction  = Modbus.byWRITE_MULTI_HOLDING_REG;
                input.byWriteData = ModbusTool.HostToNetOrder32(Value);
            }
            else
            {
                input.byFunction  = Modbus.byWRITE_SINGLE_HOLDING_REG;
                input.byWriteData = ModbusTool.HostToNetOrder16((short)Value);
            }

            AddMsgList(input);
        }