WPF串口通讯程序

目录

[一 设计原型](#一 设计原型)

[二 后台源码](#二 后台源码)


一 设计原型

二 后台源码

cs 复制代码
using HardwareCommunications;
using System.IO.Ports;
using System.Windows;

namespace PortTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        SerialCommunication s = new SerialCommunication(new SerialPortParameter()
        {
            Port = "COM1",
            DataBits = 8,
            StopBits = StopBits.One,
            Baudrate = 115200,
            Parity = Parity.None,
            ConnectionTimeout = 3000,
        });

        SerialCommunication s2 = new SerialCommunication(new SerialPortParameter()
        {
            Port = "COM1",
            DataBits = 8,
            StopBits = StopBits.One,
            Baudrate = 115200,
            Parity = Parity.None,
            ConnectionTimeout = 3000,
        }, "\r\n");

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            s.Open();
            s.Send("hello");

            s.Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //s.Open();
            //string data= s.Receive(1024);
            //string data= s.ReadExisting();
            //string data= s.ReadLine();
            s2.Open();
            string data = s2.Receive();
            s2.DataReadWriteTimeoutEvent += S2_DataReadWriteTimeoutEvent;
            s2.DataReceiveEvent += S2_DataReceiveEvent;
            log.Text = data;
            s.Close();
        }


        private void S2_DataReceiveEvent(object sender, string data)
        {
            count++;
            Dispatcher.Invoke(new Action(() =>
            {
                log3.Text = data + ":" + count;
            }));
        }

        int count = 0;
        private void S2_DataReadWriteTimeoutEvent(object sender, string errorInfo)
        {
            count++;
            Dispatcher.Invoke(new Action(() =>
            {
                log2.Text = errorInfo + count;
            }));

        }
    }
}
XML 复制代码
<Window x:Class="PortTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PortTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Content="发送" FontSize="30" Click="Button_Click"/>
        <Button Content="接收" FontSize="30" Click="Button_Click_1" Grid.Row="1"/>
        <TextBox x:Name="log" Grid.Row="2"/>
        <TextBox x:Name="log2" Grid.Row="3"/>
        <TextBox x:Name="log3" Grid.Row="4"/>
    </Grid>
</Window>
相关推荐
梦de天空一米168821 分钟前
【一个重复id让游戏卡在62%,聊聊配置表检查】
前端
ryn4839824 分钟前
01 一个功能模块,怎么分层才不乱
前端·ai编程·claude
Amos_Web27 分钟前
通过 Rspack 源码学 Rust(五):读懂 Result 与问号运算符的错误返回
前端
杨航 AI41 分钟前
JEV的用法
java·前端·数据库
Amos_Web41 分钟前
Rspack 源码解析(三十五):rspack_error 如何用 miette 渲染带源码定位的错误链
前端
去伪存真1 小时前
高敏资产场景下的大模型协同设计架构
前端·agent
修炼的dance1 小时前
OCR预处理测了108组,纠斜去噪有用,坑在顺序和力度
前端
疯华整猫1 小时前
Android 16 后台常驻适配:用 Trae 搭一套“前台服务 + 加急 Job”的可存活架构
前端
不可能片场1 小时前
AI视频成片 帧间差分把拼接缝照出来
前端·electron
写的都是BUG1 小时前
APK 里的钥匙,一直就放在门口:聊聊加固真正该做什么
前端·算法