RegTypeConverter.cs 5.64 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FLY.Modbus
{
    /// <summary>
    /// PLC寄存器 与 C# 类型数据 转换, 不含bool
    /// </summary>
    public class RegTypeConverter
    {

        /// <summary>
15
        /// PLC寄存器 或线圈 转 C# 类型数据
潘栩锋's avatar
潘栩锋 committed
16
        /// </summary>
17
        /// <param name="plc_value">PLC 寄存器数据 ushort[] 或 bool </param>
潘栩锋's avatar
潘栩锋 committed
18 19 20
        /// <param name="type">PLC 数据类型</param>
        /// <param name="scale">PLC数据放大倍数</param>
        /// <returns></returns>
21
        public static object ToObject(object plc_value, REG_TYPE type, double scale)
潘栩锋's avatar
潘栩锋 committed
22
        {
23 24 25 26 27 28 29 30 31 32 33
            if(type == REG_TYPE.BOOL)
            {
                var v = (bool)plc_value;
                if (scale < 0)
                    return !v;
                else
                    return v;
            }

            ushort[] data = (ushort[])plc_value;

潘栩锋's avatar
潘栩锋 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
            switch (type)
            {
                //case REG_TYPE.BOOL:
                //    {
                //        return data[0] != 0 ? true : false;
                //    }
                case REG_TYPE.INT16:
                    {
                        float v = (Int16)data[0];
                        v = (float)(v * scale);
                        return v;
                    }
                case REG_TYPE.UINT16:
                    {
                        float v = (UInt16)data[0];
                        v = (float)(v * scale);
                        return v;
                    }
                case REG_TYPE.UINT32:
                    {
                        float v = (UInt32)(data[0] | (data[1] << 16));
                        v = (float)(v * scale);
                        return v;
                    }
                case REG_TYPE.INT32:
                    {
                        float v = (Int32)(data[0] | (data[1] << 16));
                        v = (float)(v * scale);
                        return v;
                    }
                case REG_TYPE.FLOAT:
                    {

                        byte[] dat = new byte[] { (byte)data[0], (byte)(data[0] >> 8), (byte)data[1], (byte)(data[1] >> 8) };
                        float v = BitConverter.ToSingle(dat, 0);

                        if ((v > 1000000) || (v < -1000000))
                            v = 0;

                        v = (float)(v * scale);
                        return v;
                    }
                default:
                    {
                        throw new Exception("ToObject type=" + type + "不支持的类型");
                    }
            }
        }

        /// <summary>
84
        /// C# 类型数据 转 PLC寄存器 或线圈
潘栩锋's avatar
潘栩锋 committed
85 86 87 88
        /// </summary>
        /// <param name="value">C#中数值 只能是 float or bool</param>
        /// <param name="type">PLC 数据类型</param>
        /// <param name="scale">PLC数据放大倍数</param>
89 90
        /// <returns>ushort[] or bool </returns>
        public static object ToRegs(object value, REG_TYPE type, double scale)
潘栩锋's avatar
潘栩锋 committed
91
        {
92 93 94 95 96 97 98 99 100 101

            if (type == REG_TYPE.BOOL)
            {
                bool v = (bool)value;
                if (scale < 0)
                    return !v;
                else
                    return v;
            }

潘栩锋's avatar
潘栩锋 committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            byte[] bs;
            float f = (float)value;
            f = (float)(f / scale);
            switch (type)
            {
                case REG_TYPE.INT16:
                    {
                        short v = (short)f;
                        return new ushort[] { (ushort)v };
                    }
                case REG_TYPE.UINT16:
                    {
                        ushort v = (ushort)f;
                        return new ushort[] { v };
                    }
                case REG_TYPE.FLOAT:
                    {
                        float v = (float)f;
                        bs = BitConverter.GetBytes(v);
                        return new ushort[] {
                            (ushort)((bs[1] << 8) | bs[0]),
                            (ushort)((bs[3] << 8) | bs[2]) };
                    }
                case REG_TYPE.INT32:
                    {
                        Int32 v = (Int32)f;
                        bs = BitConverter.GetBytes(v);
                        return new ushort[] {
                            (ushort)((bs[1] << 8) | bs[0]),
                            (ushort)((bs[3] << 8) | bs[2]) };
                    }
                case REG_TYPE.UINT32:
                    {
                        UInt32 v = (UInt32)f;
                        bs = BitConverter.GetBytes(v);
                        return new ushort[] {
                            (ushort)((bs[1] << 8) | bs[0]),
                            (ushort)((bs[3] << 8) | bs[2]) };
                    }
141 142 143 144
                default:
                    {
                        throw new Exception("ToObject type=" + type + "不支持的类型");
                    }
潘栩锋's avatar
潘栩锋 committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            }
            return null;
        }
    }

    /// <summary>
    /// C# 类型枚举
    /// </summary>
    public enum REG_TYPE
    {
        /// <summary>
        /// 出错
        /// </summary>
        Unknown = 0,
        /// <summary>
        /// float
        /// </summary>
        FLOAT = 1,
        /// <summary>
        /// uint16
        /// </summary>
        UINT16 = 2,
        /// <summary>
        /// int16
        /// </summary>
        INT16 = 3,
        /// <summary>
        /// uint32
        /// </summary>
        UINT32 = 4,

        /// <summary>
        /// int32
        /// </summary>
        INT32 = 5,

        /// <summary>
        /// bool
        /// </summary>
        BOOL = 6,
    }
}