当前位置: 首页 > news >正文

WPF串口通讯程序

目录

一 设计原型

二 后台源码


一 设计原型

二 后台源码

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;}));}}
}
<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>

http://www.lryc.cn/news/404860.html

相关文章:

  • 汽车技术智能化程度不断提升,线束可靠性如何设计?
  • 实现Nginx的反向代理和负载均衡
  • 【算法】子集
  • Web前端:HTML篇(一)
  • ActiViz中的选择点vtkWorldPointPicker
  • 如何开启或者关闭 Windows 安全登录?
  • 【目标检测】Anaconda+PyTorch配置
  • 什么是离线语音识别芯片?与在线语音识别的区别
  • 使用Diffusion Models进行街景视频生成
  • UFO:革新Windows操作系统交互的UI聚焦代理
  • scp免密复制文件
  • Maven 的模块化开发示例
  • 通过QT进行服务器和客户端之间的网络通信
  • 【STM32 HAL库】DMA+串口
  • C#类型基础Part2-对象判等
  • 13.CSS 打印样式表 悬停下划线动画
  • C#基础:数据库分表的好处和实现方式
  • 基于3D开发引擎HOOPS平台的大型三维PLM系统的设计、开发与应用
  • 学习React(描述 UI)
  • mysql字符类型字段设置默认值为当前时间
  • java题目之数字加密以及如何解密
  • Linux基于CentOS7【yum】【vim】的基础学习,【普通用户提权】
  • 盛元广通实验室自动化生物样本库质量控制管理系统
  • Java | 自制AWT单词猜一猜小游戏(测试版)
  • docker搭建ES 8.14 集群
  • 自定义特征的智能演进:Mojo模型中的动态特征选择控制
  • Git->Git生成patch和使用patch
  • 开发面试算法题求教
  • OpenStack中nova的架构
  • 力扣高频SQL 50题(基础版)第五题