private async void button_read_int_Click(object sender, EventArgs e)
 {
     // int
     if (isAsync)
     {
         button_read_int.Enabled = false;
         if (textBox5.Text == "1")
         {
             DemoUtils.ReadResultRender(await readWriteNet.ReadInt32Async(textBox3.Text), textBox3.Text, textBox4);
         }
         else
         {
             DemoUtils.ReadResultRender(await readWriteNet.ReadInt32Async(textBox3.Text, ushort.Parse(textBox5.Text)), textBox3.Text, textBox4);
         }
         button_read_int.Enabled = true;
     }
     else
     {
         if (textBox5.Text == "1")
         {
             DemoUtils.ReadResultRender(readWriteNet.ReadInt32(textBox3.Text), textBox3.Text, textBox4);
         }
         else
         {
             DemoUtils.ReadResultRender(readWriteNet.ReadInt32(textBox3.Text, ushort.Parse(textBox5.Text)), textBox3.Text, textBox4);
         }
     }
 }
Example #2
0
        private async void button_read_int_Click(object sender, EventArgs e)
        {
            // int
            if (isAsync)
            {
                button_read_int.Enabled = false;
                if (textBox5.Text == "1")
                {
                    DateTime            start = DateTime.Now;
                    OperateResult <int> read  = await readWriteNet.ReadInt32Async(textBox3.Text);

                    SetTimeSpend(Convert.ToInt32((DateTime.Now - start).TotalMilliseconds));
                    DemoUtils.ReadResultRender(read, textBox3.Text, textBox4);
                }
                else
                {
                    DateTime start             = DateTime.Now;
                    OperateResult <int[]> read = await readWriteNet.ReadInt32Async(textBox3.Text, ushort.Parse(textBox5.Text));

                    SetTimeSpend(Convert.ToInt32((DateTime.Now - start).TotalMilliseconds));
                    DemoUtils.ReadResultRender(read, textBox3.Text, textBox4);
                }
                button_read_int.Enabled = true;
            }
            else
            {
                if (textBox5.Text == "1")
                {
                    DateTime            start = DateTime.Now;
                    OperateResult <int> read  = readWriteNet.ReadInt32(textBox3.Text);
                    SetTimeSpend(Convert.ToInt32((DateTime.Now - start).TotalMilliseconds));
                    DemoUtils.ReadResultRender(read, textBox3.Text, textBox4);
                }
                else
                {
                    DateTime start             = DateTime.Now;
                    OperateResult <int[]> read = readWriteNet.ReadInt32(textBox3.Text, ushort.Parse(textBox5.Text));
                    SetTimeSpend(Convert.ToInt32((DateTime.Now - start).TotalMilliseconds));
                    DemoUtils.ReadResultRender(read, textBox3.Text, textBox4);
                }
            }
        }
        /// <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));
        }
Example #4
0
        private void ThreadRead( )
        {
            while (true)
            {
                Thread.Sleep(timeSpan);
                if (isQuit)
                {
                    return;
                }

                try
                {
                    if (readType == "bool")
                    {
                        OperateResult <bool> read = readWriteNet.ReadBool(address);
                        if (read.IsSuccess)
                        {
                            if (!isQuit)
                            {
                                Invoke(new Action(() =>
                                {
                                    if (read.Content)
                                    {
                                        hslCurve1.AddCurveData("Test", 1);
                                    }
                                    else
                                    {
                                        hslCurve1.AddCurveData("Test", 0);
                                    }
                                }));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Read Failed");
                            return;
                        }
                    }
                    else if (readType == "short")
                    {
                        OperateResult <short> read = readWriteNet.ReadInt16(address);
                        if (read.IsSuccess)
                        {
                            if (!isQuit)
                            {
                                Invoke(new Action(() =>
                                {
                                    hslCurve1.AddCurveData("Test", read.Content);
                                }));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Read Failed");
                            return;
                        }
                    }
                    else if (readType == "ushort")
                    {
                        OperateResult <ushort> read = readWriteNet.ReadUInt16(address);
                        if (read.IsSuccess)
                        {
                            if (!isQuit)
                            {
                                Invoke(new Action(() =>
                                {
                                    hslCurve1.AddCurveData("Test", read.Content);
                                }));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Read Failed");
                            return;
                        }
                    }
                    else if (readType == "int")
                    {
                        OperateResult <int> read = readWriteNet.ReadInt32(address);
                        if (read.IsSuccess)
                        {
                            if (!isQuit)
                            {
                                Invoke(new Action(() =>
                                {
                                    hslCurve1.AddCurveData("Test", read.Content);
                                }));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Read Failed");
                            return;
                        }
                    }
                    else if (readType == "uint")
                    {
                        OperateResult <uint> read = readWriteNet.ReadUInt32(address);
                        if (read.IsSuccess)
                        {
                            if (!isQuit)
                            {
                                Invoke(new Action(() =>
                                {
                                    hslCurve1.AddCurveData("Test", read.Content);
                                }));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Read Failed");
                            return;
                        }
                    }
                    else if (readType == "long")
                    {
                        OperateResult <long> read = readWriteNet.ReadInt64(address);
                        if (read.IsSuccess)
                        {
                            if (!isQuit)
                            {
                                Invoke(new Action(() =>
                                {
                                    hslCurve1.AddCurveData("Test", read.Content);
                                }));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Read Failed");
                            return;
                        }
                    }
                    else if (readType == "ulong")
                    {
                        OperateResult <ulong> read = readWriteNet.ReadUInt64(address);
                        if (read.IsSuccess)
                        {
                            if (!isQuit)
                            {
                                Invoke(new Action(() =>
                                {
                                    hslCurve1.AddCurveData("Test", read.Content);
                                }));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Read Failed");
                            return;
                        }
                    }
                    else if (readType == "float")
                    {
                        OperateResult <float> read = readWriteNet.ReadFloat(address);
                        if (read.IsSuccess)
                        {
                            if (!isQuit)
                            {
                                Invoke(new Action(() =>
                                {
                                    hslCurve1.AddCurveData("Test", read.Content);
                                }));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Read Failed");
                            return;
                        }
                    }
                    else if (readType == "double")
                    {
                        OperateResult <double> read = readWriteNet.ReadDouble(address);
                        if (read.IsSuccess)
                        {
                            if (!isQuit)
                            {
                                Invoke(new Action(() =>
                                {
                                    hslCurve1.AddCurveData("Test", (float)read.Content);
                                }));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Read Failed");
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (isQuit)
                    {
                        break;
                    }
                    ;
                }

                if (isQuit)
                {
                    break;
                }
            }
        }
Example #5
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));
        }
Example #6
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);
 }
        /// <summary>
        /// 采集数据的方法
        /// </summary>
        /// <returns></returns>
        public IEnumerable <IOTData> GetData()
        {
            List <IOTData> iOTs = new List <IOTData>();

            foreach (NetworkDeviceResult result in DriveConfig.Results)
            {
                try
                {
                    string sResult;
                    switch (result.DataType.ToUpper())
                    {
                    case "BOOL":
                        sResult = NetworkDevice.ReadBool(result.DB).Content.ToString();
                        break;

                    case "STRING":
                        sResult = NetworkDevice.ReadString(result.DB, Convert.ToUInt16(result.Len)).Content;
                        break;

                    case "INT":
                        sResult = NetworkDevice.ReadInt32(result.DB).Content.ToString();
                        break;

                    case "FLOAT":
                        sResult = NetworkDevice.ReadFloat(result.DB).Content.ToString(result.Format);
                        break;

                    case "DOUBLE":
                        sResult = NetworkDevice.ReadDouble(result.DB).Content.ToString(result.Format);
                        break;

                    case "SHORT":
                        sResult = NetworkDevice.ReadInt16(result.DB).Content.ToString();
                        break;

                    case "USHORT":
                        sResult = NetworkDevice.ReadUInt16(result.DB).Content.ToString();
                        break;

                    case "UINT":
                        sResult = NetworkDevice.ReadUInt32(result.DB).Content.ToString();
                        break;

                    case "LONG":
                        sResult = NetworkDevice.ReadInt64(result.DB).Content.ToString();
                        break;

                    case "ULONG":
                        sResult = NetworkDevice.ReadUInt64(result.DB).Content.ToString();
                        break;

                    default:
                        sResult = NetworkDevice.ReadString(result.DB, 1).Content;
                        break;
                    }
                    iOTs.Add(new IOTData
                    {
                        DataCode  = result.Address,
                        DataValue = sResult,
                        DataName  = result.Name,
                        DriveCode = DriveConfig.DriveCode,
                        DriveType = DriveConfig.DriveType,
                        GTime     = DateTime.Now,
                        Unit      = result.Unit
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            return(iOTs);
        }