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>
相关推荐
昨天;明天。今天。1 分钟前
案例-任务清单
前端·javascript·css
暮雪倾风37 分钟前
【WPF开发】控件介绍-Grid(网格布局)
windows·wpf
zqx_71 小时前
随记 前端框架React的初步认识
前端·react.js·前端框架
Death2001 小时前
Qt 中的 QListWidget、QTreeWidget 和 QTableWidget:简化的数据展示控件
c语言·开发语言·c++·qt·c#
惜.己1 小时前
javaScript基础(8个案例+代码+效果图)
开发语言·前端·javascript·vscode·css3·html5
什么鬼昵称2 小时前
Pikachu-csrf-CSRF(get)
前端·csrf
Death2002 小时前
Qt 3D、QtQuick、QtQuick 3D 和 QML 的关系
c语言·c++·qt·3d·c#
yufei-coder2 小时前
C#基础语法
开发语言·c#·.net
长天一色2 小时前
【ECMAScript 从入门到进阶教程】第三部分:高级主题(高级函数与范式,元编程,正则表达式,性能优化)
服务器·开发语言·前端·javascript·性能优化·ecmascript
yngsqq2 小时前
031集——文本文件按空格分行——C#学习笔记
笔记·学习·c#