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 =
C# 串口 byte[] 数据解析-- TI mmWave数据接收和显示
最新推荐文章于 2023-10-05 01:00:33 发布
本文介绍如何使用C#处理和显示来自TI毫米波雷达的byte[]数据,涉及串口通信和数据解析技术。
摘要由CSDN通过智能技术生成