Пример #1
0
        internal static byte[] _SystemDataToPLCData(object data, int destByteWidth, int plcDataFormat)
        {
            switch (plcDataFormat)
            {
            case 0:
            {
                if (destByteWidth != 1)
                {
                    throw new ArgumentException("sourceByteWidth");
                }
                bool flag = (bool)Convert.ChangeType(data, typeof(bool));
                return(new byte[] { (flag ? ((byte)1) : ((byte)0)) });
            }

            case 1:
                switch (destByteWidth)
                {
                case 2:
                {
                    ushort hostValue = (ushort)Convert.ChangeType(data, typeof(ushort));
                    return(BitConverter.GetBytes(PLCByteOrderConverter.HostToPLCOrder(hostValue)));
                }

                case 3:
                    goto Label_01D0;

                case 4:
                {
                    uint num5 = (uint)Convert.ChangeType(data, typeof(uint));
                    return(BitConverter.GetBytes(PLCByteOrderConverter.HostToPLCOrder(num5)));
                }

                case 8:
                {
                    ulong num6 = (ulong)Convert.ChangeType(data, typeof(ulong));
                    return(BitConverter.GetBytes(PLCByteOrderConverter.HostToPLCOrder(num6)));
                }
                }
                goto Label_01D0;

            case 2:
                switch (destByteWidth)
                {
                case 2:
                {
                    short num7 = (short)Convert.ChangeType(data, typeof(short));
                    num7 = (short)PLCByteOrderConverter.HostToPLCOrder((ushort)num7);
                    return(BitConverter.GetBytes(num7));
                }

                case 3:
                    goto Label_0274;

                case 4:
                {
                    int num8 = (int)Convert.ChangeType(data, typeof(int));
                    return(BitConverter.GetBytes((int)PLCByteOrderConverter.HostToPLCOrder((uint)num8)));
                }

                case 8:
                {
                    long num9 = (long)Convert.ChangeType(data, typeof(long));
                    return(BitConverter.GetBytes((long)PLCByteOrderConverter.HostToPLCOrder((ulong)num9)));
                }
                }
                goto Label_0274;

            case 3:
                switch (destByteWidth)
                {
                case 2:
                {
                    ushort num = (ushort)Convert.ChangeType(data, typeof(ushort));
                    return(BitConverter.GetBytes(PLCByteOrderConverter.HostToPLCOrder(Convert.ToUInt16(num.ToString(), 0x10))));
                }

                case 4:
                {
                    uint num2 = (uint)Convert.ChangeType(data, typeof(uint));
                    return(BitConverter.GetBytes(PLCByteOrderConverter.HostToPLCOrder(Convert.ToUInt32(num2.ToString(), 0x10))));
                }

                case 8:
                {
                    ulong num3 = (ulong)Convert.ChangeType(data, typeof(ulong));
                    return(BitConverter.GetBytes(PLCByteOrderConverter.HostToPLCOrder(Convert.ToUInt64(num3.ToString(), 0x10))));
                }
                }
                break;

            case 4:
                switch (destByteWidth)
                {
                case 4:
                {
                    float num10 = (float)Convert.ChangeType(data, typeof(float));
                    return(PLCByteOrderConverter.SwapByteIfNeed(BitConverter.GetBytes(num10)));
                }

                case 8:
                {
                    double num11 = (double)Convert.ChangeType(data, typeof(double));
                    return(PLCByteOrderConverter.SwapByteIfNeed(BitConverter.GetBytes(num11)));
                }
                }
                throw new ArgumentOutOfRangeException("destByteWidht");

            case 5:
            {
                byte[] destinationArray = new byte[destByteWidth];
                for (int i = 0; i < destByteWidth; i++)
                {
                    destinationArray[i] = 0;
                }
                string        s        = (string)Convert.ChangeType(data, typeof(string));
                ASCIIEncoding encoding = new ASCIIEncoding();
                Array.Copy(encoding.GetBytes(s), 0, destinationArray, 0, Math.Min(encoding.GetByteCount(s), destinationArray.Length));
                return(destinationArray);
            }

            default:
                throw new ArgumentOutOfRangeException("destByteWidht");
            }
            throw new ArgumentOutOfRangeException("destByteWidht");
Label_01D0:
            throw new ArgumentOutOfRangeException("destByteWidth");
Label_0274:
            throw new ArgumentOutOfRangeException("destByteWidht");
        }
Пример #2
0
        public static object PLCDataToSystemData(byte[] array, int index, int sourceByteWidth, int plcDataFormat, Type systemDataType)
        {
            ulong  num;
            object obj2;
            object obj3;

            byte[] destinationArray = new byte[sourceByteWidth];
            Array.Copy(array, index, destinationArray, 0, sourceByteWidth);
            switch (plcDataFormat)
            {
            case 0:
            {
                if (sourceByteWidth != 1)
                {
                    throw new ArgumentOutOfRangeException("sourceByteWidth");
                }
                bool flag = destinationArray[0] != 0;
                return(Convert.ChangeType(flag, systemDataType));
            }

            case 1:
            case 3:
                destinationArray = PLCByteOrderConverter.SwapByteIfNeed(destinationArray);
                switch (sourceByteWidth)
                {
                case 1:
                    num = destinationArray[0];
                    goto Label_00BB;

                case 2:
                    num = BitConverter.ToUInt16(destinationArray, 0);
                    goto Label_00BB;

                case 4:
                    num = BitConverter.ToUInt32(destinationArray, 0);
                    goto Label_00BB;

                case 8:
                    num = BitConverter.ToUInt64(destinationArray, 0);
                    goto Label_00BB;
                }
                break;

            case 2:
                destinationArray = PLCByteOrderConverter.SwapByteIfNeed(destinationArray);
                switch (sourceByteWidth)
                {
                case 1:
                    obj2 = destinationArray[0];
                    goto Label_015F;

                case 2:
                    obj2 = BitConverter.ToInt16(destinationArray, 0);
                    goto Label_015F;

                case 3:
                    goto Label_0154;

                case 4:
                    obj2 = BitConverter.ToInt32(destinationArray, 0);
                    goto Label_015F;

                case 8:
                    obj2 = BitConverter.ToInt64(destinationArray, 0);
                    goto Label_015F;
                }
                goto Label_0154;

            case 4:
                destinationArray = PLCByteOrderConverter.SwapByteIfNeed(destinationArray);
                switch (sourceByteWidth)
                {
                case 4:
                    obj3 = BitConverter.ToSingle(destinationArray, 0);
                    goto Label_01AA;

                case 8:
                    obj3 = BitConverter.ToDouble(destinationArray, 0);
                    goto Label_01AA;
                }
                throw new ArgumentOutOfRangeException("sourceByteWidth");

            case 5:
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                return(Convert.ChangeType(encoding.GetString(destinationArray, index, sourceByteWidth), systemDataType));
            }

            default:
                throw new ArgumentOutOfRangeException("plcDataFormat");
            }
            throw new ArgumentOutOfRangeException("sourceByteWidth");
Label_00BB:
            if (plcDataFormat == 3)
            {
                try
                {
                    num = Convert.ToUInt64(num.ToString("X"));
                }
                catch (FormatException exception)
                {
                    throw new FormatException("BCDFormatException((long) num, exception);");
                }
            }
            return(Convert.ChangeType(num, systemDataType));

Label_0154:
            throw new ArgumentOutOfRangeException("sourceByteWidth");
Label_015F:
            return(Convert.ChangeType(obj2, systemDataType));

Label_01AA:
            return(Convert.ChangeType(obj3, systemDataType));
        }