Exemple #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");
        }