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>
相关推荐
木斯佳几秒前
前端八股文面经大全:Bilibili 前端实习面(2026-03-20)·深度解析
前端·sse·ssr·rag
比特森林探险记3 分钟前
Element Plus 实战指南
前端·javascript
Fairy要carry9 分钟前
面试-Dispatch Tools
前端·chrome
IT_陈寒10 分钟前
JavaScript开发者必看:3个让代码效率翻倍的隐藏技巧
前端·人工智能·后端
嘉琪00112 分钟前
Day8 完整学习包(Vue 基础 & 响应式)——2026 0320
前端·vue.js·学习
FlyWIHTSKY13 分钟前
Vue3 单文件中不同的组件
前端·javascript·vue.js
一字白首26 分钟前
小程序组件化进阶:从复用到通信的完整指南DAY04
前端·小程序·apache
读忆27 分钟前
你是否用过Tailwind CSS?你是在什么情况下使用的呢?
前端·css·经验分享·笔记·taiiwindcss
阿珊和她的猫31 分钟前
探秘小程序:为何拿不到 DOM 相关 API
前端·小程序
FlyWIHTSKY43 分钟前
Vue 3 onMounted 中控制同步与异步执行策略
前端·javascript·vue.js