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>
相关推荐
腾讯TNTWeb前端团队5 小时前
helux v5 发布了,像pinia一样优雅地管理你的react状态吧
前端·javascript·react.js
范文杰9 小时前
AI 时代如何更高效开发前端组件?21st.dev 给了一种答案
前端·ai编程
拉不动的猪9 小时前
刷刷题50(常见的js数据通信与渲染问题)
前端·javascript·面试
拉不动的猪9 小时前
JS多线程Webworks中的几种实战场景演示
前端·javascript·面试
FreeCultureBoy10 小时前
macOS 命令行 原生挂载 webdav 方法
前端
uhakadotcom10 小时前
Astro 框架:快速构建内容驱动型网站的利器
前端·javascript·面试
uhakadotcom10 小时前
了解Nest.js和Next.js:如何选择合适的框架
前端·javascript·面试
uhakadotcom11 小时前
React与Next.js:基础知识及应用场景
前端·面试·github
uhakadotcom11 小时前
Remix 框架:性能与易用性的完美结合
前端·javascript·面试
uhakadotcom11 小时前
Node.js 包管理器:npm vs pnpm
前端·javascript·面试