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>
相关推荐
GISer_Jing3 分钟前
AI时代面试新常态——从“会用工具”到“深挖原理”的跨越
前端·人工智能·ai编程
IT_陈寒13 分钟前
React的useEffect把我坑惨了,这些闭包陷阱真要命
前端·人工智能·后端
前端之虎陈随易25 分钟前
有生之年系列,Nodejs进程管理pm2 v7.0发布
前端·typescript·npm·node.js
ayqy贾杰32 分钟前
Cursor SDK发布!开发者可直接搬走其内核
前端·vue.js·面试
椰猫子40 分钟前
SpringMVC(SpringMVC简介、请求与响应(请求映射路径、请求参数、日期类型参数传递、响应json数据))
java·前端·数据库
love530love44 分钟前
如何在 Google Chrome 中强制开启 Gemini AI 侧边栏(完整图文教程)
前端·人工智能·chrome·windows
光影少年1 小时前
对typescript开发框架的理解?
前端·javascript·typescript
跨境数据猎手1 小时前
反向海淘代购系统:1688 / 淘宝自动代采 + API 同步(附可用源码)
前端
lUie INGA1 小时前
Go-Gin Web 框架完整教程
前端·golang·gin
a1117761 小时前
“像风之翼“无人机巡检平台仪表盘
前端·javascript·开源·html·无人机