Пример #1
0
        /// <summary>
        /// 尝试以波特率通讯
        /// </summary>
        /// <param name="_baudRate"></param>
        /// <returns></returns>
        private string TryBaudRate(string _baudRate)
        {
            try
            {
                CreateOpenPort(_baudRate);

                CompassModuleData data = ReadAllData();

                if (data != null)
                {
                    return(_baudRate);
                }

                return(null);
            }
            catch (TimeoutException)
            {
                return(null);
            }
            finally
            {
                CompassModulePort.Close();
                CompassModulePort.Dispose();
            }
        }
Пример #2
0
        /// <summary>
        /// 采集数据到达
        /// </summary>
        /// <param name="newData"></param>
        void cm_OnDataChanged(CompassModuleData newData)
        {
            UpdateView ev2 = delegate(CompassModuleData args)
            {
                VMThis.CMDataRT = args;

                CMDataList.Add(args);
            };

            Dispatcher.BeginInvoke(ev2, newData);
        }
Пример #3
0
        /// <summary>
        /// 转换一帧数据
        /// </summary>
        /// <returns></returns>
        private CompassModuleData ConvertToCMData(byte[] frame)
        {
            CompassModuleData data = new CompassModuleData();

            data.PitchAngle = ConvertWitLinkBCD(frame, 3);

            data.RollAngle = ConvertWitLinkBCD(frame, 6);

            data.HeadingAngle = ConvertWitLinkBCD(frame, 9);

            return(data);
        }
Пример #4
0
        private CompassModuleData ReadAllData()
        {
            CompassModuleData data = null;

            // 发送读取单帧数据命令
            CompassModulePort.Write(CMD_READALL, 0, CMD_READALL.Length);

            // 寻找帧头
            if (ReadFrameHead())
            {
                // 读取帧数据
                byte[] frame = ReadFrame(0x0d);

                // 转换数据
                data = ConvertToCMData(frame);
            }
            return(data);
        }
Пример #5
0
        /// <summary>
        /// 50Hz 采集过程
        /// </summary>
        private void Acquisition50HzAction()
        {
            // 发送50Hz 命令
            try
            {
                // 按照默认波特率打开串口
                CreateOpenPort(PcBaudRate);

                // 发送连续采样命令
                SetSampleRate();

                while (IsAcquisiting)
                {
                    if (ReadFrameHead())
                    {
                        // 读取一帧数据
                        // 读取帧数据
                        byte[] frame = ReadFrame(0x0d);

                        if (frame == null)
                        {
                            continue;
                        }

                        // 转换数据
                        CompassModuleData data = ConvertToCMData(frame);

                        if (OnDataChanged != null)
                        {
                            // System.Diagnostics.Debug.WriteLine("Data Changed.");

                            OnDataChanged(data);
                        }
                    }
                }
            }
            finally
            {
                StopAcq();

                CompassModulePort.Close();
            }
        }
Пример #6
0
        /// <summary>
        /// 读取单帧数据
        /// </summary>
        /// <returns></returns>
        public CompassModuleData ReadSigleFrameData()
        {
            CompassModuleData data = null;

            if (TryBaudRate(PcBaudRate) != PcBaudRate)
            {
                throw new NotSupportedException("访问超时,可能使用了错误的波特率。");
            }

            try
            {
                // 按照默认波特率打开串口
                CreateOpenPort(PcBaudRate);

                // 设置波特率
                //if (PcBaudRate != CMBaudRate)
                //{
                //    TrySetBaudRate(CMBaudRate, PcBaudRate);
                //}

                // 重新打开串口(工作在新的波特率
                if (CompassModulePort.IsOpen == false)
                {
                    CompassModulePort.Open();
                }

                data = ReadAllData();
            }
            finally
            {
                // 设置默认波特率
                //if (PcBaudRate != DefaultBaudRate)
                //{
                //    TrySetBaudRate(CMBaudRate, DefaultBaudRate);
                //}

                CompassModulePort.Close();
            }

            return(data);
        }