C# 串口 byte[] 数据解析-- TI mmWave数据接收和显示

本文介绍如何使用C#处理和显示来自TI毫米波雷达的byte[]数据,涉及串口通信和数据解析技术。
摘要由CSDN通过智能技术生成

using Panuon.UI.Silver;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SiaLiDar
{
   
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : WindowX
    {
   
        public MainWindow()
        {
   
            InitializeComponent();
            this.DataContext = new MainViewModel();

            var portsConnected = SerialPort.GetPortNames();
            foreach (var i in portsConnected)
            {
   
                cbDataPort.Items.Add(i);
                cbPort.Items.Add(i);
            }



            CommandPort = new SerialPort();
            DataPort = new SerialPort();

        }


        private System.IO.Ports.SerialPort CommandPort;
        private System.IO.Ports.SerialPort DataPort;

        static readonly byte[] MAGIC_WORD_ARRAY = new byte[] {
    2, 1, 4, 3, 6, 5, 8, 7 };



        private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
   
            byte[] ReDatas = new byte[CommandPort.BytesToRead];
            CommandPort.Read(ReDatas, 0, ReDatas.Length);//读取数据
            this.ProcessRev(ReDatas);//输出数据
        }

        private void ProcessRev(byte[] reDatas)
        {
   
            Dispatcher.Invoke(() => {
   
                this.tbRevData.Text += "接收:" + new UTF8Encoding().GetString(reDatas) + Environment.NewLine;
            });
        }

        private void btnOpenCommandCom_CLICK(object sender, RoutedEventArgs e)
        {
   
            try
            {
   
                //if (ComDevice.IsOpen == false)
                if (btnOpenCommandCom.Content == "打开")
                {
   
                    CommandPort.PortName = cbPort.Text.ToString();
                    CommandPort.BaudRate = 115200;
                    CommandPort.Parity = Parity.None;
                    CommandPort.DataBits = 8;
                    CommandPort.StopBits = StopBits.One;

                    this.CommandPort.ReadTimeout = 1000;
                    this.CommandPort.WriteTimeout = 1000;
                    this.CommandPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.Com_DataReceived);

                    CommandPort.Open();

                }
                else
                {
   
                    CommandPort.Close();
                }
            }
            catch (Exception ex)
            {
   
                MessageBox.Show($"打开串口时发生以下错误 {ex.Message},请确认串口状态");
            }
            finally
            {
   
                if (CommandPort.IsOpen == false)
                {
   
                    btnOpenCommandCom.Content =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值