예제 #1
0
파일: Test.cs 프로젝트: ewin66/Robotics
        public static Robotics.HAL.Sensors.Telemetric.LaserReading[] Deserialize(string serializedString)
        {
            if ((serializedString == null) || (serializedString.Length < 20) || ((serializedString.Length % 2) != 0))
            {
                return(null);
            }

            int    count;
            bool   mistaken;
            double min;
            double step;
            double angle;
            double distance;
            ushort distanceMM;

            Robotics.HAL.Sensors.Telemetric.LaserReading[] readings;
            int ix;

            byte[] bytes;

            ix    = serializedString.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase) ? 2 : 0;
            bytes = new byte[(serializedString.Length / 2) - ix];
            for (int i = 0; i < bytes.Length; ++i, ix += 2)
            {
                bytes[i] = Byte.Parse(serializedString.Substring(ix), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture);
            }

            ix    = 0;
            count = BitConverter.ToInt32(bytes, ix);
            ix   += 4;
            if ((count < 0) || (((serializedString.Length - 40) / 4) != count))
            {
                return(null);
            }

            min = BitConverter.ToDouble(bytes, ix);
            ix += 8;

            step = BitConverter.ToDouble(bytes, ix);
            ix  += 8;

            angle    = min;
            readings = new Robotics.HAL.Sensors.Telemetric.LaserReading[count];
            for (int i = 0; i < count; ++i, angle += step)
            {
                distanceMM = BitConverter.ToUInt16(bytes, ix);
                if (mistaken = ((distanceMM & 0x8000) != 0))
                {
                    distanceMM &= 0x7FFF;
                }
                ix         += 2;
                distance    = distanceMM / 1000.0;
                readings[i] = new Robotics.HAL.Sensors.Telemetric.LaserReading(null, angle, distance, mistaken);
            }
            return(readings);
        }
예제 #2
0
        public static Robotics.HAL.Sensors.Telemetric.LaserReading[] Deserialize(string serializedString)
        {
            if ((serializedString == null) || (serializedString.Length < 20) || ((serializedString.Length % 2) != 0))
                return null;

            int count;
            bool mistaken;
            double min;
            double step;
            double angle;
            double distance;
            ushort distanceMM;
            Robotics.HAL.Sensors.Telemetric.LaserReading[] readings;
            int ix;
            byte[] bytes;

            ix = serializedString.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase) ? 2 : 0;
            bytes = new byte[(serializedString.Length / 2) - ix];
            for (int i = 0; i < bytes.Length; ++i, ix+=2)
                bytes[i] = Byte.Parse(serializedString.Substring(ix), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture);

            ix = 0;
            count = BitConverter.ToInt32(bytes, ix);
            ix += 4;
            if ((count < 0) || (((serializedString.Length - 40) / 4) != count))
                return null;

            min = BitConverter.ToDouble(bytes, ix);
            ix += 8;

            step = BitConverter.ToDouble(bytes, ix);
            ix += 8;

            angle = min;
            readings = new Robotics.HAL.Sensors.Telemetric.LaserReading[count];
            for (int i = 0; i < count; ++i, angle += step)
            {
                distanceMM = BitConverter.ToUInt16(bytes, ix);
                if (mistaken = ((distanceMM & 0x8000) != 0))
                    distanceMM &= 0x7FFF;
                ix += 2;
                distance = distanceMM / 1000.0;
                readings[i] = new Robotics.HAL.Sensors.Telemetric.LaserReading(null, angle, distance, mistaken);
            }
            return readings;
        }