Ejemplo n.º 1
0
 /// <summary>
 /// 根据设备名获取设备
 /// </summary>
 /// <param name="devicename">设备名称</param>
 /// <param name="device">设备</param>
 /// <returns></returns>
 public bool GetDeviceByName(string devicename, out ModbusDevice device)
 {
     foreach (ModbusDevice dv in DeviceDict.Values)
     {
         if (dv.Name == devicename)
         {
             device = dv;
             return(true);
         }
     }
     device = null;
     return(false);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 解析协议消息
        /// </summary>
        /// <param name="rcvBytes"></param>
        /// <param name="rspBytes"></param>
        /// <returns></returns>
        private bool ParseMessage(byte[] rcvBytes, out byte[] rspBytes)
        {
            rspBytes = null;
            int len = rcvBytes.Length;

            if (MBType == ModbusType.RTU)
            {
                //判断长度
                if (len < 7)
                {
                    return(false);
                }
                //校验CRC
                if (!CRC.CheckCRC(rcvBytes))
                {
                    return(false);
                }
                //检查地址是否有设备
                if (!DeviceDict.ContainsKey(rcvBytes[0]))
                {
                    return(false);
                }
                //设备处理消息
                ModbusDevice device = DeviceDict[rcvBytes[0]];
                return(device.ParseRTUMessage(rcvBytes, out rspBytes));
            }
            else if (MBType == ModbusType.TCP)
            {
                //判断长度
                if (len < 8)
                {
                    return(false);
                }
                //校验长度
                if (len != (rcvBytes[5] + 6))
                {
                    return(false);
                }
                //检查地址是否有设备
                if (!DeviceDict.ContainsKey(rcvBytes[6]))
                {
                    return(false);
                }
                //设备处理消息
                ModbusDevice device = DeviceDict[rcvBytes[6]];
                return(device.ParseTCPMessage(rcvBytes, out rspBytes));
            }
            return(false);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 增加设备
 /// </summary>
 /// <param name="device"></param>
 /// <returns></returns>
 public bool AddDevice(ModbusDevice device)
 {
     if ((DeviceDict.ContainsKey(device.Address)) || (DeviceNameDict.ContainsValue(device.Name)))
     {
         MsgLogger.PushMsg(device.Name, "Add", "Fail, already exist.");
         return(false);
     }
     else
     {
         DeviceDict.Add(device.Address, device);
         DeviceNameDict.Add(device.Address, device.Name);
         MsgLogger.PushMsg(device.Name, "Add", "Success.");
         return(true);
     }
 }