コード例 #1
0
        public void SetReadWriteNet(IReadWriteNet readWrite, string address, bool isAsync = false, int strLength = 10)
        {
            this.isAsync  = isAsync;
            this.address  = address;
            textBox3.Text = address;
            textBox8.Text = address;
            textBox1.Text = strLength.ToString( );
            readWriteNet  = readWrite;
            Type type = readWrite.GetType( );

            readByteMethod = type.GetMethod("ReadByte", new Type[] { typeof(string) });
            if (readByteMethod == null)
            {
                button_read_byte.Enabled = false;
            }

            try
            {
                writeByteMethod = type.GetMethod("Write", new Type[] { typeof(string), typeof(byte) });
                if (writeByteMethod == null)
                {
                    button23.Enabled = false;
                }
            }
            catch
            {
                button23.Enabled = false;
            }
        }
コード例 #2
0
 public void SetReadWrite(IReadWriteNet readWrite, string address)
 {
     readWriteNet        = readWrite;
     textBox3.Text       = address;
     thread              = new Thread(ThreadRead);
     thread.IsBackground = false;
     thread.Start( );
 }
コード例 #3
0
ファイル: Form1.cs プロジェクト: nm-1216/PLCServer
        private void ddd(IReadWriteNet abc)
        {
            Timer timer = new Timer();

            timer.Interval = 1000;
            timer.Tick    += new EventHandler((s, e) => timer1_Tick(s, e, abc));
            timer.Start();
        }
コード例 #4
0
ファイル: FrmView.cs プロジェクト: YuanGao66520/ScrewScada1
 public void SetReadWrite(IReadWriteNet readWrite)
 {
     ReadWriteNet = readWrite;
     // 启动后台线程,定时读取PLC中的数据,然后在曲线控件中显示
     if (!isThreadRun)
     {
         if (!int.TryParse("10", out timeSleep))
         {
             MessageBox.Show("Time input wrong!");
             return;
         }
         isThreadRun         = true;
         thread              = new Thread(ThreadReadServer);
         thread.IsBackground = true;
         thread.Start();
     }
     else
     {
         isThreadRun = false;
     }
 }
コード例 #5
0
 public static string LeerBulkPLC(IReadWriteNet readWrite, string Variable, ushort Cantidad)
 {
     try
     {
         OperateResult <byte[]> operateResult = readWrite.Read(Variable, Cantidad);
         if (!operateResult.IsSuccess)
         {
             return("");
         }
         char[] chArray = Encoding.ASCII.GetString(operateResult.Content).ToCharArray();
         string str     = "";
         int    index   = 2;
         while (index < chArray.Length)
         {
             str    = index != 0 ? str + chArray[index + 1].ToString() + chArray[index].ToString() : chArray[index].ToString();
             index += 2;
         }
         return(str);
     }
     catch (Exception ex)
     {
         return("");
     }
 }
コード例 #6
0
ファイル: DataStorage.cs プロジェクト: JustinGZJ/SMD
        public DataStorage([Inject] IReadWriteNet readwriter, [Inject] IByteTransform transform, [Inject] IEventAggregator events)
        {
            this._events    = events;
            this._transform = transform;
            _readwriter     = readwriter;
            Load();
            // Task.Run(() =>
            //{
            //    while (true)
            //    {
            //        lock (_enable)
            //        {
            //         //   对数据进行分组,每组内间隔不超过PDU
            //            int arrIndex = 0;
            //            var sortedVars = DataValues.OrderBy(x => x.StartIndex).ToList();
            //            var arrList = new List<List<VAR>> { new List<VAR>() { sortedVars[0] } };
            //            for (int i = 1; i < sortedVars.Count; i++)
            //            {
            //                var a = sortedVars[i].StartIndex + sortedVars[i].GetLength();
            //                var b = sortedVars[i - 1].StartIndex;
            //                if (a - b < PDU)
            //                {
            //                    arrList[arrIndex].Add(sortedVars[i]);
            //                }
            //                else
            //                {
            //                    arrList.Add(new List<VAR>());
            //                    arrIndex++;
            //                }
            //            }
            //            foreach (var list in arrList)
            //            {
            //             //   获取组内需要请求的字数
            //                int nShorts = 0;
            //                foreach (var s in list)
            //                {
            //                    nShorts += s.GetLength();
            //                }
            //                if (nShorts > 10)
            //                {
            //                    var s = list.First().StartIndex;
            //                    var l = list.Last().GetLength() + list.Last().StartIndex;
            //                    var r = _readwriter.Read($"x:3;{s}", (ushort)(l - s));
            //                    if (r.IsSuccess)
            //                    {
            //                        var buffer = r.Content;
            //                        foreach (var v in list)
            //                        {
            //                            switch (v.Type)
            //                            {
            //                                case TYPE.BOOL:
            //                                    var cs = transform.TransUInt16(buffer, (v.StartIndex - s) * 2);
            //                                    v.Value = (cs & (1 << v.Tag)) > 0;
            //                                    break;
            //                                case TYPE.FLOAT:
            //                                    v.Value = transform.TransSingle(buffer, (v.StartIndex - s) * 2);
            //                                    break;
            //                                case TYPE.INT:
            //                                    v.Value = transform.TransInt32(buffer, (v.StartIndex - s) * 2);
            //                                    break;
            //                                case TYPE.SHORT:
            //                                    v.Value = transform.TransInt16(buffer, (v.StartIndex - s) * 2);
            //                                    break;
            //                                case TYPE.STRING:
            //                                    v.Value = transform.TransString(buffer, (v.StartIndex - s) * 2, v.Tag, Encoding.UTF8);
            //                                    break;
            //                            }
            //                            v.Time = DateTime.Now;
            //                        }
            //                    }
            //                    else
            //                    {
            //                        _events.Publish(new MsgItem()
            //                        {
            //                            Level = "E",
            //                            Time = DateTime.Now,
            //                            Value = "Read from data server fail"
            //                        });
            //                    }
            //                }
            //                else
            //                {
            //                    foreach (var v in list)
            //                    {

            //                        switch (v.Type)
            //                        {
            //                            case TYPE.BOOL:
            //                                var r = _readwriter.ReadUInt16($"x=3;{v.StartIndex}");
            //                                v.Value = (r.Content & (1 << v.Tag)) > 0;
            //                                break;
            //                            case TYPE.FLOAT:
            //                                var f = _readwriter.ReadFloat($"x=3;{v.StartIndex}");
            //                                v.Value = f.Content;
            //                                break;
            //                            case TYPE.INT:
            //                                var i = _readwriter.ReadInt32($"x=3;{v.StartIndex}");
            //                                v.Value = i.Content;
            //                                break;
            //                            case TYPE.STRING:
            //                                var s = _readwriter.ReadString($"x=3;{v.StartIndex}", (ushort)v.Tag);
            //                                v.Value = s.Content;
            //                                break;
            //                            case TYPE.SHORT:
            //                                var us = _readwriter.ReadInt16($"x=3;{v.StartIndex}");
            //                                v.Value = us.Content;
            //                                break;
            //                        }
            //                        v.Time = DateTime.Now;
            //                    }
            //                }
            //            }
            //            Refresh();
            //        }

            //        System.Threading.Thread.Sleep(100);
            //    }
            //});
        }
コード例 #7
0
        /// <summary>
        /// 从设备里读取支持Hsl特性的数据内容,该特性为<see cref="HslDeviceAddressAttribute"/>,详细参考论坛的操作说明。
        /// </summary>
        /// <typeparam name="T">自定义的数据类型对象</typeparam>
        /// <param name="data">自定义的数据对象</param>
        /// <param name="readWrite">数据读写对象</param>
        /// <returns>包含是否成功的结果对象</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static OperateResult Write <T>(T data, IReadWriteNet readWrite) where T : class, new()
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var type = typeof(T);
            var obj  = data;

            var properties = type.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

            foreach (var property in properties)
            {
                var attribute = property.GetCustomAttributes(typeof(HslDeviceAddressAttribute), false);
                if (attribute == null)
                {
                    continue;
                }

                HslDeviceAddressAttribute hslAttribute = null;
                for (int i = 0; i < attribute.Length; i++)
                {
                    HslDeviceAddressAttribute tmp = (HslDeviceAddressAttribute)attribute[i];
                    if (tmp.deviceType != null && tmp.deviceType == readWrite.GetType( ))
                    {
                        hslAttribute = tmp;
                        break;
                    }
                }

                if (hslAttribute == null)
                {
                    for (int i = 0; i < attribute.Length; i++)
                    {
                        HslDeviceAddressAttribute tmp = (HslDeviceAddressAttribute)attribute[i];
                        if (tmp.deviceType == null)
                        {
                            hslAttribute = tmp;
                            break;
                        }
                    }
                }

                if (hslAttribute == null)
                {
                    continue;
                }


                Type propertyType = property.PropertyType;
                if (propertyType == typeof(short))
                {
                    short value = (short)property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(short[]))
                {
                    short[] value = (short[])property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(ushort))
                {
                    ushort value = (ushort)property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(ushort[]))
                {
                    ushort[] value = (ushort[])property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(int))
                {
                    int value = (int)property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(int[]))
                {
                    int[] value = (int[])property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(uint))
                {
                    uint value = (uint)property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(uint[]))
                {
                    uint[] value = (uint[])property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(long))
                {
                    long value = (long)property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(long[]))
                {
                    long[] value = (long[])property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(ulong))
                {
                    ulong value = (ulong)property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(ulong[]))
                {
                    ulong[] value = (ulong[])property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(float))
                {
                    float value = (float)property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(float[]))
                {
                    float[] value = (float[])property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(double))
                {
                    double value = (double)property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(double[]))
                {
                    double[] value = (double[])property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(string))
                {
                    string value = (string)property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(byte[]))
                {
                    byte[] value = (byte[])property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(bool))
                {
                    bool value = (bool)property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
                else if (propertyType == typeof(bool[]))
                {
                    bool[] value = (bool[])property.GetValue(obj, null);

                    OperateResult writeResult = readWrite.Write(hslAttribute.address, value);
                    if (!writeResult.IsSuccess)
                    {
                        return(writeResult);
                    }
                }
            }

            return(OperateResult.CreateSuccessResult((T)obj));
        }
コード例 #8
0
        /// <summary>
        /// 从设备里读取支持Hsl特性的数据内容,该特性为<see cref="HslDeviceAddressAttribute"/>,详细参考论坛的操作说明。
        /// </summary>
        /// <typeparam name="T">自定义的数据类型对象</typeparam>
        /// <param name="readWrite">读写接口的实现</param>
        /// <returns>包含是否成功的结果对象</returns>
        public static OperateResult <T> Read <T>(IReadWriteNet readWrite) where T : class, new()
        {
            var type = typeof(T);
            // var constrcuor = type.GetConstructors( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic );
            var obj = type.Assembly.CreateInstance(type.FullName);

            var properties = type.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

            foreach (var property in properties)
            {
                var attribute = property.GetCustomAttributes(typeof(HslDeviceAddressAttribute), false);
                if (attribute == null)
                {
                    continue;
                }

                HslDeviceAddressAttribute hslAttribute = null;
                for (int i = 0; i < attribute.Length; i++)
                {
                    HslDeviceAddressAttribute tmp = (HslDeviceAddressAttribute)attribute[i];
                    if (tmp.deviceType != null && tmp.deviceType == readWrite.GetType( ))
                    {
                        hslAttribute = tmp;
                        break;
                    }
                }

                if (hslAttribute == null)
                {
                    for (int i = 0; i < attribute.Length; i++)
                    {
                        HslDeviceAddressAttribute tmp = (HslDeviceAddressAttribute)attribute[i];
                        if (tmp.deviceType == null)
                        {
                            hslAttribute = tmp;
                            break;
                        }
                    }
                }

                if (hslAttribute == null)
                {
                    continue;
                }

                Type propertyType = property.PropertyType;
                if (propertyType == typeof(short))
                {
                    OperateResult <short> valueResult = readWrite.ReadInt16(hslAttribute.address);
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(short[]))
                {
                    OperateResult <short[]> valueResult = readWrite.ReadInt16(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(ushort))
                {
                    OperateResult <ushort> valueResult = readWrite.ReadUInt16(hslAttribute.address);
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(ushort[]))
                {
                    OperateResult <ushort[]> valueResult = readWrite.ReadUInt16(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(int))
                {
                    OperateResult <int> valueResult = readWrite.ReadInt32(hslAttribute.address);
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(int[]))
                {
                    OperateResult <int[]> valueResult = readWrite.ReadInt32(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(uint))
                {
                    OperateResult <uint> valueResult = readWrite.ReadUInt32(hslAttribute.address);
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(uint[]))
                {
                    OperateResult <uint[]> valueResult = readWrite.ReadUInt32(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(long))
                {
                    OperateResult <long> valueResult = readWrite.ReadInt64(hslAttribute.address);
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(long[]))
                {
                    OperateResult <long[]> valueResult = readWrite.ReadInt64(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(ulong))
                {
                    OperateResult <ulong> valueResult = readWrite.ReadUInt64(hslAttribute.address);
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(ulong[]))
                {
                    OperateResult <ulong[]> valueResult = readWrite.ReadUInt64(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(float))
                {
                    OperateResult <float> valueResult = readWrite.ReadFloat(hslAttribute.address);
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(float[]))
                {
                    OperateResult <float[]> valueResult = readWrite.ReadFloat(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(double))
                {
                    OperateResult <double> valueResult = readWrite.ReadDouble(hslAttribute.address);
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(double[]))
                {
                    OperateResult <double[]> valueResult = readWrite.ReadDouble(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(string))
                {
                    OperateResult <string> valueResult = readWrite.ReadString(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(byte[]))
                {
                    OperateResult <byte[]> valueResult = readWrite.Read(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(bool))
                {
                    OperateResult <bool> valueResult = readWrite.ReadBool(hslAttribute.address);
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
                else if (propertyType == typeof(bool[]))
                {
                    OperateResult <bool[]> valueResult = readWrite.ReadBool(hslAttribute.address, (ushort)(hslAttribute.length > 0 ? hslAttribute.length : 1));
                    if (!valueResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(valueResult));
                    }

                    property.SetValue(obj, valueResult.Content, null);
                }
            }

            return(OperateResult.CreateSuccessResult((T)obj));
        }
コード例 #9
0
 void abc(IReadWriteNet my)
 {
 }
コード例 #10
0
ファイル: Form1.cs プロジェクト: nm-1216/PLCServer
 private void timer1_Tick(object sender, EventArgs e, IReadWriteNet plc)
 {
     txtAInt.Text = plc.ReadInt16("DB1.124").Content.ToString();
 }
コード例 #11
0
        /// <summary>
        /// 从设备里读取支持Hsl特性的数据内容,该特性为<see cref="T:ProcessControlService.CommunicationStandard.HslDeviceAddressAttribute" />,详细参考论坛的操作说明。
        /// </summary>
        /// <typeparam name="T">自定义的数据类型对象</typeparam>
        /// <param name="data">自定义的数据对象</param>
        /// <param name="readWrite">数据读写对象</param>
        /// <returns>包含是否成功的结果对象</returns>
        /// <exception cref="T:System.ArgumentNullException"></exception>
        public static OperateResult Write <T>(T data, IReadWriteNet readWrite) where T : class, new()
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            Type typeFromHandle = typeof(T);

            PropertyInfo[] properties = typeFromHandle.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            PropertyInfo[] array      = properties;
            foreach (PropertyInfo propertyInfo in array)
            {
                object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(DeviceAddressAttribute), inherit: false);
                if (customAttributes == null)
                {
                    continue;
                }
                DeviceAddressAttribute hslDeviceAddressAttribute = null;
                for (int j = 0; j < customAttributes.Length; j++)
                {
                    DeviceAddressAttribute hslDeviceAddressAttribute2 = (DeviceAddressAttribute)customAttributes[j];
                    if (hslDeviceAddressAttribute2.DeviceType != null && hslDeviceAddressAttribute2.DeviceType == readWrite.GetType())
                    {
                        hslDeviceAddressAttribute = hslDeviceAddressAttribute2;
                        break;
                    }
                }
                if (hslDeviceAddressAttribute == null)
                {
                    for (int k = 0; k < customAttributes.Length; k++)
                    {
                        DeviceAddressAttribute hslDeviceAddressAttribute3 = (DeviceAddressAttribute)customAttributes[k];
                        if (hslDeviceAddressAttribute3.DeviceType == null)
                        {
                            hslDeviceAddressAttribute = hslDeviceAddressAttribute3;
                            break;
                        }
                    }
                }
                if (hslDeviceAddressAttribute == null)
                {
                    continue;
                }
                Type propertyType = propertyInfo.PropertyType;
                if (propertyType == typeof(short))
                {
                    short         value         = (short)propertyInfo.GetValue(data, null);
                    OperateResult operateResult = readWrite.Write(hslDeviceAddressAttribute.Address, value);
                    if (!operateResult.IsSuccess)
                    {
                        return(operateResult);
                    }
                }
                else if (propertyType == typeof(short[]))
                {
                    short[]       values         = (short[])propertyInfo.GetValue(data, null);
                    OperateResult operateResult2 = readWrite.Write(hslDeviceAddressAttribute.Address, values);
                    if (!operateResult2.IsSuccess)
                    {
                        return(operateResult2);
                    }
                }
                else if (propertyType == typeof(ushort))
                {
                    ushort        value2         = (ushort)propertyInfo.GetValue(data, null);
                    OperateResult operateResult3 = readWrite.Write(hslDeviceAddressAttribute.Address, value2);
                    if (!operateResult3.IsSuccess)
                    {
                        return(operateResult3);
                    }
                }
                else if (propertyType == typeof(ushort[]))
                {
                    ushort[]      values2        = (ushort[])propertyInfo.GetValue(data, null);
                    OperateResult operateResult4 = readWrite.Write(hslDeviceAddressAttribute.Address, values2);
                    if (!operateResult4.IsSuccess)
                    {
                        return(operateResult4);
                    }
                }
                else if (propertyType == typeof(int))
                {
                    int           value3         = (int)propertyInfo.GetValue(data, null);
                    OperateResult operateResult5 = readWrite.Write(hslDeviceAddressAttribute.Address, value3);
                    if (!operateResult5.IsSuccess)
                    {
                        return(operateResult5);
                    }
                }
                else if (propertyType == typeof(int[]))
                {
                    int[]         values3        = (int[])propertyInfo.GetValue(data, null);
                    OperateResult operateResult6 = readWrite.Write(hslDeviceAddressAttribute.Address, values3);
                    if (!operateResult6.IsSuccess)
                    {
                        return(operateResult6);
                    }
                }
                else if (propertyType == typeof(uint))
                {
                    uint          value4         = (uint)propertyInfo.GetValue(data, null);
                    OperateResult operateResult7 = readWrite.Write(hslDeviceAddressAttribute.Address, value4);
                    if (!operateResult7.IsSuccess)
                    {
                        return(operateResult7);
                    }
                }
                else if (propertyType == typeof(uint[]))
                {
                    uint[]        values4        = (uint[])propertyInfo.GetValue(data, null);
                    OperateResult operateResult8 = readWrite.Write(hslDeviceAddressAttribute.Address, values4);
                    if (!operateResult8.IsSuccess)
                    {
                        return(operateResult8);
                    }
                }
                else if (propertyType == typeof(long))
                {
                    long          value5         = (long)propertyInfo.GetValue(data, null);
                    OperateResult operateResult9 = readWrite.Write(hslDeviceAddressAttribute.Address, value5);
                    if (!operateResult9.IsSuccess)
                    {
                        return(operateResult9);
                    }
                }
                else if (propertyType == typeof(long[]))
                {
                    long[]        values5         = (long[])propertyInfo.GetValue(data, null);
                    OperateResult operateResult10 = readWrite.Write(hslDeviceAddressAttribute.Address, values5);
                    if (!operateResult10.IsSuccess)
                    {
                        return(operateResult10);
                    }
                }
                else if (propertyType == typeof(ulong))
                {
                    ulong         value6          = (ulong)propertyInfo.GetValue(data, null);
                    OperateResult operateResult11 = readWrite.Write(hslDeviceAddressAttribute.Address, value6);
                    if (!operateResult11.IsSuccess)
                    {
                        return(operateResult11);
                    }
                }
                else if (propertyType == typeof(ulong[]))
                {
                    ulong[]       values6         = (ulong[])propertyInfo.GetValue(data, null);
                    OperateResult operateResult12 = readWrite.Write(hslDeviceAddressAttribute.Address, values6);
                    if (!operateResult12.IsSuccess)
                    {
                        return(operateResult12);
                    }
                }
                else if (propertyType == typeof(float))
                {
                    float         value7          = (float)propertyInfo.GetValue(data, null);
                    OperateResult operateResult13 = readWrite.Write(hslDeviceAddressAttribute.Address, value7);
                    if (!operateResult13.IsSuccess)
                    {
                        return(operateResult13);
                    }
                }
                else if (propertyType == typeof(float[]))
                {
                    float[]       values7         = (float[])propertyInfo.GetValue(data, null);
                    OperateResult operateResult14 = readWrite.Write(hslDeviceAddressAttribute.Address, values7);
                    if (!operateResult14.IsSuccess)
                    {
                        return(operateResult14);
                    }
                }
                else if (propertyType == typeof(double))
                {
                    double        value8          = (double)propertyInfo.GetValue(data, null);
                    OperateResult operateResult15 = readWrite.Write(hslDeviceAddressAttribute.Address, value8);
                    if (!operateResult15.IsSuccess)
                    {
                        return(operateResult15);
                    }
                }
                else if (propertyType == typeof(double[]))
                {
                    double[]      values8         = (double[])propertyInfo.GetValue(data, null);
                    OperateResult operateResult16 = readWrite.Write(hslDeviceAddressAttribute.Address, values8);
                    if (!operateResult16.IsSuccess)
                    {
                        return(operateResult16);
                    }
                }
                else if (propertyType == typeof(string))
                {
                    string        value9          = (string)propertyInfo.GetValue(data, null);
                    OperateResult operateResult17 = readWrite.Write(hslDeviceAddressAttribute.Address, value9);
                    if (!operateResult17.IsSuccess)
                    {
                        return(operateResult17);
                    }
                }
                else if (propertyType == typeof(byte[]))
                {
                    byte[]        value10         = (byte[])propertyInfo.GetValue(data, null);
                    OperateResult operateResult18 = readWrite.Write(hslDeviceAddressAttribute.Address, value10);
                    if (!operateResult18.IsSuccess)
                    {
                        return(operateResult18);
                    }
                }
                else if (propertyType == typeof(bool))
                {
                    bool          value11         = (bool)propertyInfo.GetValue(data, null);
                    OperateResult operateResult19 = readWrite.Write(hslDeviceAddressAttribute.Address, value11);
                    if (!operateResult19.IsSuccess)
                    {
                        return(operateResult19);
                    }
                }
                else if (propertyType == typeof(bool[]))
                {
                    bool[]        value12         = (bool[])propertyInfo.GetValue(data, null);
                    OperateResult operateResult20 = readWrite.Write(hslDeviceAddressAttribute.Address, value12);
                    if (!operateResult20.IsSuccess)
                    {
                        return(operateResult20);
                    }
                }
            }
            return(OperateResult.CreateSuccessResult(data));
        }
コード例 #12
0
        /// <summary>
        /// 从设备里读取支持Hsl特性的数据内容,该特性为<see cref="T:ProcessControlService.CommunicationStandard.HslDeviceAddressAttribute" />,详细参考论坛的操作说明。
        /// </summary>
        /// <typeparam name="T">自定义的数据类型对象</typeparam>
        /// <param name="readWrite">读写接口的实现</param>
        /// <returns>包含是否成功的结果对象</returns>
        public static OperateResult <T> Read <T>(IReadWriteNet readWrite) where T : class, new()
        {
            Type   typeFromHandle = typeof(T);
            object obj            = typeFromHandle.Assembly.CreateInstance(typeFromHandle.FullName);

            PropertyInfo[] properties = typeFromHandle.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            PropertyInfo[] array      = properties;
            foreach (PropertyInfo propertyInfo in array)
            {
                object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(DeviceAddressAttribute), inherit: false);
                if (customAttributes == null)
                {
                    continue;
                }
                DeviceAddressAttribute hslDeviceAddressAttribute = null;
                for (int j = 0; j < customAttributes.Length; j++)
                {
                    DeviceAddressAttribute hslDeviceAddressAttribute2 = (DeviceAddressAttribute)customAttributes[j];
                    if (hslDeviceAddressAttribute2.DeviceType != null && hslDeviceAddressAttribute2.DeviceType == readWrite.GetType())
                    {
                        hslDeviceAddressAttribute = hslDeviceAddressAttribute2;
                        break;
                    }
                }
                if (hslDeviceAddressAttribute == null)
                {
                    for (int k = 0; k < customAttributes.Length; k++)
                    {
                        DeviceAddressAttribute hslDeviceAddressAttribute3 = (DeviceAddressAttribute)customAttributes[k];
                        if (hslDeviceAddressAttribute3.DeviceType == null)
                        {
                            hslDeviceAddressAttribute = hslDeviceAddressAttribute3;
                            break;
                        }
                    }
                }
                if (hslDeviceAddressAttribute == null)
                {
                    continue;
                }
                Type propertyType = propertyInfo.PropertyType;
                if (propertyType == typeof(short))
                {
                    OperateResult <short> operateResult = readWrite.ReadInt16(hslDeviceAddressAttribute.Address);
                    if (!operateResult.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult));
                    }
                    propertyInfo.SetValue(obj, operateResult.Content, null);
                }
                else if (propertyType == typeof(short[]))
                {
                    OperateResult <short[]> operateResult2 = readWrite.ReadInt16(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult2.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult2));
                    }
                    propertyInfo.SetValue(obj, operateResult2.Content, null);
                }
                else if (propertyType == typeof(ushort))
                {
                    OperateResult <ushort> operateResult3 = readWrite.ReadUInt16(hslDeviceAddressAttribute.Address);
                    if (!operateResult3.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult3));
                    }
                    propertyInfo.SetValue(obj, operateResult3.Content, null);
                }
                else if (propertyType == typeof(ushort[]))
                {
                    OperateResult <ushort[]> operateResult4 = readWrite.ReadUInt16(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult4.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult4));
                    }
                    propertyInfo.SetValue(obj, operateResult4.Content, null);
                }
                else if (propertyType == typeof(int))
                {
                    OperateResult <int> operateResult5 = readWrite.ReadInt32(hslDeviceAddressAttribute.Address);
                    if (!operateResult5.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult5));
                    }
                    propertyInfo.SetValue(obj, operateResult5.Content, null);
                }
                else if (propertyType == typeof(int[]))
                {
                    OperateResult <int[]> operateResult6 = readWrite.ReadInt32(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult6.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult6));
                    }
                    propertyInfo.SetValue(obj, operateResult6.Content, null);
                }
                else if (propertyType == typeof(uint))
                {
                    OperateResult <uint> operateResult7 = readWrite.ReadUInt32(hslDeviceAddressAttribute.Address);
                    if (!operateResult7.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult7));
                    }
                    propertyInfo.SetValue(obj, operateResult7.Content, null);
                }
                else if (propertyType == typeof(uint[]))
                {
                    OperateResult <uint[]> operateResult8 = readWrite.ReadUInt32(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult8.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult8));
                    }
                    propertyInfo.SetValue(obj, operateResult8.Content, null);
                }
                else if (propertyType == typeof(long))
                {
                    OperateResult <long> operateResult9 = readWrite.ReadInt64(hslDeviceAddressAttribute.Address);
                    if (!operateResult9.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult9));
                    }
                    propertyInfo.SetValue(obj, operateResult9.Content, null);
                }
                else if (propertyType == typeof(long[]))
                {
                    OperateResult <long[]> operateResult10 = readWrite.ReadInt64(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult10.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult10));
                    }
                    propertyInfo.SetValue(obj, operateResult10.Content, null);
                }
                else if (propertyType == typeof(ulong))
                {
                    OperateResult <ulong> operateResult11 = readWrite.ReadUInt64(hslDeviceAddressAttribute.Address);
                    if (!operateResult11.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult11));
                    }
                    propertyInfo.SetValue(obj, operateResult11.Content, null);
                }
                else if (propertyType == typeof(ulong[]))
                {
                    OperateResult <ulong[]> operateResult12 = readWrite.ReadUInt64(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult12.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult12));
                    }
                    propertyInfo.SetValue(obj, operateResult12.Content, null);
                }
                else if (propertyType == typeof(float))
                {
                    OperateResult <float> operateResult13 = readWrite.ReadFloat(hslDeviceAddressAttribute.Address);
                    if (!operateResult13.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult13));
                    }
                    propertyInfo.SetValue(obj, operateResult13.Content, null);
                }
                else if (propertyType == typeof(float[]))
                {
                    OperateResult <float[]> operateResult14 = readWrite.ReadFloat(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult14.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult14));
                    }
                    propertyInfo.SetValue(obj, operateResult14.Content, null);
                }
                else if (propertyType == typeof(double))
                {
                    OperateResult <double> operateResult15 = readWrite.ReadDouble(hslDeviceAddressAttribute.Address);
                    if (!operateResult15.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult15));
                    }
                    propertyInfo.SetValue(obj, operateResult15.Content, null);
                }
                else if (propertyType == typeof(double[]))
                {
                    OperateResult <double[]> operateResult16 = readWrite.ReadDouble(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult16.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult16));
                    }
                    propertyInfo.SetValue(obj, operateResult16.Content, null);
                }
                else if (propertyType == typeof(string))
                {
                    OperateResult <string> operateResult17 = readWrite.ReadString(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult17.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult17));
                    }
                    propertyInfo.SetValue(obj, operateResult17.Content, null);
                }
                else if (propertyType == typeof(byte[]))
                {
                    OperateResult <byte[]> operateResult18 = readWrite.Read(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult18.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult18));
                    }
                    propertyInfo.SetValue(obj, operateResult18.Content, null);
                }
                else if (propertyType == typeof(bool))
                {
                    OperateResult <bool> operateResult19 = readWrite.ReadBool(hslDeviceAddressAttribute.Address);
                    if (!operateResult19.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult19));
                    }
                    propertyInfo.SetValue(obj, operateResult19.Content, null);
                }
                else if (propertyType == typeof(bool[]))
                {
                    OperateResult <bool[]> operateResult20 = readWrite.ReadBool(hslDeviceAddressAttribute.Address, (ushort)((hslDeviceAddressAttribute.Length <= 0) ? 1 : hslDeviceAddressAttribute.Length));
                    if (!operateResult20.IsSuccess)
                    {
                        return(OperateResult.CreateFailedResult <T>(operateResult20));
                    }
                    propertyInfo.SetValue(obj, operateResult20.Content, null);
                }
            }
            return(OperateResult.CreateSuccessResult((T)obj));
        }
コード例 #13
0
 public bool Connect()
 {
     _rw = readWriteFactory.GetReadWriteNet();
     Task.Factory.StartNew(() =>
     {
         while (true)
         {
             var rop     = _rw.ReadInt16(addr);
             IsConnected = rop.IsSuccess;
             if (!rop.IsSuccess)
             {
                 Events.Publish(new MsgItem {
                     Time = DateTime.Now, Level = "E", Value = rop.Message
                 });
             }
             if (IsConnected)
             {
                 var RI = _rw.ReadInt32(readWriteFactory.AddressB, 10);
                 if (RI.IsSuccess)
                 {
                     for (int i = 0; i < 10; i++)
                     {
                         if (RI.Content[i] != Ints[i])
                         {
                             Ints[i] = RI.Content[i];
                         }
                     }
                 }
                 var rs = _rw.ReadFloat(readWriteFactory.CameraDataAddr, 5);
                 if (rs.IsSuccess)
                 {
                     for (int i = 0; i < 5; i++)
                     {
                         KvFloats[i].Value = rs.Content[i];
                     }
                 }
                 if (Datas[0] != rop.Content)
                 {
                     Datas[0] = rop.Content;
                     for (int i = 0; i < 16; i++)
                     {
                         bool v = (Datas[0] & (1 << i)) > 0;
                         if (Bits[i] != v)
                         {
                             Bits[i]         = v;
                             KVBits[i].Value = v;
                             if (new[] { 0, 1, 6 }.Contains(i))
                             {
                                 Events.Publish(new MsgItem
                                 {
                                     Level = "D",
                                     Time  = DateTime.Now,
                                     Value = $"Bit[{i}]:" + (v ? "Rising edge" : "Failing edge")
                                 });
                                 Events.Publish(new EventIO
                                 {
                                     Index = i,
                                     Value = v
                                 });
                             }
                         }
                     }
                     if (Bits[15] == true)
                     {
                         WriteBool(15, false);
                     }
                 }
             }
             Thread.Sleep(10);
         }
     });
     return(IsConnected);
 }