예제 #1
0
        /*
         * Modbus is Big Endian
         * Opto22 float is Big Endian
         * 3 Holding 1234
         * 3 Holding 3412
         * 4 Input 1234
         * 4 Input 3412
         */

        private float ByteArrayToFloat(ushort[] value, int byteOrder)
        {
            byte[] bytes = null;
            switch (byteOrder)
            {
            case 0:     //1234 opto22
                bytes = new byte[] {
                    (byte)((value[0] >> 8) & 0xff),
                    (byte)((value[0] >> 0) & 0xff),
                    (byte)((value[1] >> 8) & 0xff),
                    (byte)((value[1] >> 0) & 0xff),
                };
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(bytes);
                }
                return(BitConverter.ToSingle(bytes, 0));

            case 1:     //3412 selec
                bytes = new byte[] {
                    (byte)((value[1] >> 8) & 0xff),
                    (byte)((value[1] >> 0) & 0xff),
                    (byte)((value[0] >> 8) & 0xff),
                    (byte)((value[0] >> 0) & 0xff),
                };
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(bytes);
                }
                return(BitConverter.ToSingle(bytes, 0));
            }
            throw Thrower.Make("Invalid byte order {0}", byteOrder);
        }
예제 #2
0
        /*
         * Modbus is Big Endian
         * Opto22 float is Big Endian
         * 6 Holding 1234
         * 6 Holding 3412
         */

        private ushort[] FloatToByteArray(float value, int byteOrder)
        {
            byte[] bytes = null;
            switch (byteOrder)
            {
            case 0:     //1234 opto22
                bytes = BitConverter.GetBytes(value);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(bytes);
                }
                return(new ushort[] {
                    (ushort)((bytes[0] << 8 | bytes[1]) & 0xFFFF),
                    (ushort)((bytes[2] << 8 | bytes[3]) & 0xFFFF),
                });

            case 1:     //3412 selec
                bytes = BitConverter.GetBytes(value);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(bytes);
                }
                return(new ushort[] {
                    (ushort)((bytes[2] << 8 | bytes[3]) & 0xFFFF),
                    (ushort)((bytes[0] << 8 | bytes[1]) & 0xFFFF),
                });
            }
            throw Thrower.Make("Invalid byte order {0}", byteOrder);
        }
예제 #3
0
        public static IModbusCommand Parse(byte[] request, int offset)
        {
            var slave   = request[offset + 0];
            var code    = request[offset + 1];
            var address = ModbusHelper.GetUShort(request, offset + 2);

            switch (code)
            {
            case 1:
                return(Parse01(slave, code, address, request, offset));

            case 2:
                return(Parse02(slave, code, address, request, offset));

            case 3:
                return(Parse03(slave, code, address, request, offset));

            case 4:
                return(Parse04(slave, code, address, request, offset));

            case 5:
                return(Parse05(slave, code, address, request, offset));

            case 6:
                return(Parse06(slave, code, address, request, offset));

            case 15:
                return(Parse15(slave, code, address, request, offset));

            case 16:
                return(Parse16(slave, code, address, request, offset));
            }

            throw Thrower.Make("Unsupported function code {0}", code);
        }