コード例 #1
0
        public dynamic GetValue(byte[] p)
        {
            var raw = GetRawValue(p);

            if (raw == null)
            {
                return(null);
            }
            var cnv = ConversionFunction.Convert(raw);

            return(cnv);
        }
コード例 #2
0
        public unsafe dynamic GetRawValue(byte[] p)
        {
            switch (FieldType)
            {
            case PointType.F1234:
            {
                var v = (p[byte_offset] << 24) | (p[1 + byte_offset] << 16) | (p[2 + byte_offset] << 8) |
                        p[3 + byte_offset];
                var fp = (float *)&v;
                var f  = *fp;
                return(f);
            }

            case PointType.F12345678:
            {
                var v = ((ulong)p[0 + byte_offset] << 56) | ((ulong)p[1 + byte_offset] << 48) |
                        ((ulong)p[2 + byte_offset] << 40) | ((ulong)p[3 + byte_offset] << 32) |
                        ((ulong)p[4 + byte_offset] << 24) | ((ulong)p[5 + byte_offset] << 16) |
                        ((ulong)p[6 + byte_offset] << 8) | (ulong)p[7 + byte_offset];
                var dp = (double *)&v;
                var d  = *dp;
                return(d);
            }

            case PointType.I1:
                return((sbyte)p[byte_offset]);

            case PointType.I12:
                return((Int16)((p[byte_offset] << 8) | (p[byte_offset + 1])));

            case PointType.I12b:
            {
                long temp = ((p[byte_offset] << 8) | (p[byte_offset + 1]));
                temp = temp >> (15 - bit_stop);
                var   len    = bit_stop - bit_start + 1;
                var   result = (_mask32[len] & temp);
                var   isNeg  = (_bit32[len - 1] & result) != 0;
                Int16 r      = 0;
                if (isNeg)
                {
                    result = result - _bit32[len];
                    r      = (Int16)result;
                }
                return(r);
            }

            case PointType.I1234:
                return
                    ((Int32)
                     ((p[byte_offset] << 24) | (p[byte_offset + 1] << 16) | (p[byte_offset + 2] << 8) |
                      (p[byte_offset + 3])));

            case PointType.I1234b:
            {
                long temp = ((p[byte_offset] << 24) | (p[byte_offset + 1] << 16) | (p[byte_offset + 2] << 8) |
                             (p[byte_offset + 3]));
                temp = temp >> (31 - bit_stop);
                var len    = bit_stop - bit_start + 1;
                var result = (_mask32[len] & temp);
                var isNeg  = (_bit32[len - 1] & result) != 0;
                var r      = 0;
                if (isNeg)
                {
                    result = result - _bit32[len];
                    r      = (Int32)result;
                }
                return(r);
            }

            case PointType.S1:
            {
                int count = byte_size;
                for (var i = 0; i < byte_size; i++)
                {
                    if (p[i + byte_offset] == 0)
                    {
                        count = i;
                        break;
                    }
                }

                var s = Encoding.ASCII.GetString(p, byte_offset, count);
                return(s);
            }

            case PointType.TIME40:
                return
                    ((UInt32)
                     ((p[byte_offset] << 24) | (p[byte_offset + 1] << 16) | (p[byte_offset + 2] << 8) |
                      (p[byte_offset + 3])));

            case PointType.TIME42:
                return(((ulong)p[0 + byte_offset] << 40) | ((ulong)p[1 + byte_offset] << 32) |
                       ((ulong)p[2 + byte_offset] << 24) | ((ulong)p[3 + byte_offset] << 16) |
                       ((ulong)p[4 + byte_offset] << 8) | (ulong)p[5 + byte_offset]);

            case PointType.TIME44:
                return(((ulong)p[0 + byte_offset] << 56) | ((ulong)p[1 + byte_offset] << 48) |
                       ((ulong)p[2 + byte_offset] << 40) | ((ulong)p[3 + byte_offset] << 32) |
                       ((ulong)p[4 + byte_offset] << 24) | ((ulong)p[5 + byte_offset] << 16) |
                       ((ulong)p[6 + byte_offset] << 8) | (ulong)p[7 + byte_offset]);

            case PointType.U1:
                return(p[byte_offset]);

            case PointType.U1b:
            {
                int temp = p[byte_offset];
                temp = temp >> (7 - bit_stop);
                var len    = bit_stop - bit_start + 1;
                var result = (byte)(_mask32[len] & temp);
                return(result);
            }

            case PointType.U12:
                return((UInt16)((p[byte_offset] << 8) | (p[byte_offset + 1])));

            case PointType.U12b:
            {
                var temp = ((p[byte_offset] << 8) | (p[byte_offset + 1]));
                temp = temp >> (15 - bit_stop);
                var len    = bit_stop - bit_start + 1;
                var result = (UInt16)(_mask32[len] & temp);
                return(result);
            }

            case PointType.U1234:
                return
                    ((UInt32)
                     ((p[byte_offset] << 24) | (p[byte_offset + 1] << 16) | (p[byte_offset + 2] << 8) |
                      (p[byte_offset + 3])));

            case PointType.U12345678:
                return
                    ((UInt64)
                     (((ulong)p[byte_offset] << 56) | ((ulong)p[byte_offset + 1] << 48) | ((ulong)p[byte_offset + 2] << 40) | ((ulong)p[byte_offset + 3] << 32) |
                      ((ulong)p[byte_offset + 4] << 24) | ((ulong)p[byte_offset + 5] << 16) | ((ulong)p[byte_offset + 6] << 8) |
                      ((ulong)p[byte_offset + 7])));

            // This isn't right because the value is big endian
            //return (dynamic) BitConverter.ToUInt64(p, byte_offset);
            case PointType.U1234b:
            {
                var temp = ((p[byte_offset] << 24) | (p[byte_offset + 1] << 16) | (p[byte_offset + 2] << 8) |
                            (p[byte_offset + 3]));
                temp = temp >> (31 - bit_stop);
                var len    = bit_stop - bit_start + 1;
                var result = (UInt32)(_mask32[len] & temp);
                return(result);
            }

            case PointType.U21:
                return((UInt16)((p[byte_offset + 1] << 8) | (p[byte_offset])));

            case PointType.U4321:
                return
                    ((UInt32)
                     ((p[byte_offset + 3] << 24) | (p[byte_offset + 2] << 16) | (p[byte_offset + 1] << 8) |
                      (p[byte_offset])));

            case PointType.U4321b:
            {
                var temp = ((p[byte_offset + 3] << 24) | (p[byte_offset + 2] << 16) | (p[byte_offset + 1] << 8) |
                            (p[byte_offset]));
                temp = temp >> (31 - bit_stop);
                var len    = bit_stop - bit_start + 1;
                var result = (UInt32)(_mask32[len] & temp);
                return(result);
            }

            case PointType.Pseudo:
                return(ConversionFunction.Convert(0));

            case PointType.FullPacketConversion:
                return(ConversionFunction.Convert(p));
            }
            return(null);
        }