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

做设计的一般用什么网站找素材广东seo

做设计的一般用什么网站找素材,广东seo,虚拟币网站开发,深圳台历制作一、引言 在现代工业控制、嵌入式系统等领域,串口通信作为一种常见的通信方式,被广泛应用于各种场景。C#作为一门强大的编程语言,结合Windows Presentation Foundation(WPF)框架,可以轻松实现串口通信功能…

 

一、引言

在现代工业控制、嵌入式系统等领域,串口通信作为一种常见的通信方式,被广泛应用于各种场景。C#作为一门强大的编程语言,结合Windows Presentation Foundation(WPF)框架,可以轻松实现串口通信功能。本文将带领大家一步步实现C# WPF串口通信,并在控制台打印接收到的数据。

二、环境准备

在开始编写代码之前,请确保您的开发环境满足以下条件:

  1. 安装Visual Studio 2019或更高版本。
  2. 创建一个WPF应用程序项目。

三、实现步骤

1.初始化串口

首先,我们需要创建一个SerialPort对象,并对其进行配置。以下是一个初始化串口的示例方法:

public bool InitCOM(string PortName)
{serialPort = new SerialPort(PortName, 115200, Parity.None, 8, StopBits.One);serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);serialPort.ReceivedBytesThreshold = 1;serialPort.RtsEnable = true;return OpenPort();
}

在这段代码中,我们设置了串口的名称、波特率、校验位、数据位和停止位。同时,我们为serialPort对象订阅了DataReceived事件,以便在接收到数据时进行处理。

2.打开串口

接下来,我们需要编写一个方法来打开串口。以下是一个打开串口的示例方法:

public bool OpenPort()
{try{serialPort.Open();}catch (Exception ex){MessageBox.Show($"无法打开串口: {ex.Message}");return false;}return serialPort.IsOpen;
}

在这段代码中,我们尝试打开串口,并在打开失败时捕获异常,显示错误信息。

3.接收数据并打印到控制台

当串口接收到数据时,会触发DataReceived事件。我们可以在事件处理程序中读取数据,并将其打印到控制台。以下是一个处理数据接收的示例方法:

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{int bytesToRead = serialPort.BytesToRead;byte[] readBuffer = new byte[bytesToRead];serialPort.Read(readBuffer, 0, bytesToRead);string str = Encoding.Default.GetString(readBuffer);Dispatcher.Invoke(() =>{MessageBox.Show(str);});Console.WriteLine(str);
}

在这段代码中,我们首先读取串口缓冲区中的数据,然后将其转换为字符串。接着,我们在UI线程上显示一个消息框,并在控制台上打印接收到的数据。

4.发送数据

最后,我们来实现发送数据的功能。以下是一个发送数据的示例方法:

public void SendCommand(string CommandString)
{if (serialPort != null && serialPort.IsOpen){byte[] WriteBuffer = Encoding.ASCII.GetBytes(CommandString);serialPort.Write(WriteBuffer, 0, WriteBuffer.Length);}else{MessageBox.Show("串口未打开,无法发送数据。");}
}

在这段代码中,我们首先检查串口是否已打开,然后发送指定的字符串。

四、总结

通过本文,我们介绍了如何在C# WPF应用程序中实现串口通信,包括初始化串口、打开串口、接收数据、发送数据以及在控制台打印接收到的数据。掌握这些基本技巧,将为您的WPF串口通信项目开发带来极大便利。在实际应用中,您可以根据需求对代码进行优化和扩展,以满足更复杂的功能需求。

界面还在编写中。。。。。

最终代码:


using System;
using System.IO.Ports;
using System.Text;
using System.Windows;
//必要的库public SerialPort serialPort { get; set; }public MainWindow(){InitializeComponent();if (InitCOM("COM9")){Console.WriteLine("Hello, World!");SendCommand("asdasdasd"); // 发送字符}}public bool InitCOM(string PortName){serialPort = new SerialPort(PortName, 115200, Parity.None, 8, StopBits.One);serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);serialPort.ReceivedBytesThreshold = 1;serialPort.RtsEnable = true;return OpenPort();}private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e){int bytesToRead = serialPort.BytesToRead; // 获取实际可读的字节数byte[] readBuffer = new byte[bytesToRead]; // 根据实际可读的字节数分配缓冲区serialPort.Read(readBuffer, 0, bytesToRead); // 读取数据string str = Encoding.Default.GetString(readBuffer); // 将字节数组转换为字符串// 在UI线程上显示消息框Dispatcher.Invoke(() =>{MessageBox.Show(str);});Console.WriteLine(str); // 打印到控制台}public bool OpenPort(){try{serialPort.Open();}catch (Exception ex){MessageBox.Show($"无法打开串口: {ex.Message}");return false;}return serialPort.IsOpen;}public void SendCommand(string CommandString){if (serialPort != null && serialPort.IsOpen){byte[] WriteBuffer = Encoding.ASCII.GetBytes(CommandString);serialPort.Write(WriteBuffer, 0, WriteBuffer.Length);}else{MessageBox.Show("串口未打开,无法发送数据。");}}// 在窗口关闭时,确保串口被关闭protected override void OnClosed(EventArgs e){base.OnClosed(e);if (serialPort != null && serialPort.IsOpen){serialPort.Close();}}

 


文章转载自:
http://dinncoposseman.tpps.cn
http://dinncovolatilization.tpps.cn
http://dinncopseudoallele.tpps.cn
http://dinncogelation.tpps.cn
http://dinnconavicert.tpps.cn
http://dinncosinnerite.tpps.cn
http://dinncoselangor.tpps.cn
http://dinncodockage.tpps.cn
http://dinncoabsentee.tpps.cn
http://dinncopicnicky.tpps.cn
http://dinncopseudomonas.tpps.cn
http://dinncoaxiologist.tpps.cn
http://dinncohieroglyphist.tpps.cn
http://dinncoconjoin.tpps.cn
http://dinncoaestilignosa.tpps.cn
http://dinncohjs.tpps.cn
http://dinncomagnetron.tpps.cn
http://dinncosniperscope.tpps.cn
http://dinnconetlayer.tpps.cn
http://dinncoslyboots.tpps.cn
http://dinncovermian.tpps.cn
http://dinncograndfather.tpps.cn
http://dinncocandied.tpps.cn
http://dinncoreembroider.tpps.cn
http://dinncosalicyl.tpps.cn
http://dinncoendogenetic.tpps.cn
http://dinncoplenum.tpps.cn
http://dinncohaircurling.tpps.cn
http://dinncogoatpox.tpps.cn
http://dinncoexpressionistic.tpps.cn
http://dinnconauru.tpps.cn
http://dinncograndiose.tpps.cn
http://dinncounconfirmed.tpps.cn
http://dinncocapsulotomy.tpps.cn
http://dinncokepler.tpps.cn
http://dinncobatavia.tpps.cn
http://dinncodiscordantly.tpps.cn
http://dinncoauxanometer.tpps.cn
http://dinncogelatiniform.tpps.cn
http://dinncofosterer.tpps.cn
http://dinncoaustralopithecine.tpps.cn
http://dinncofigment.tpps.cn
http://dinncosendai.tpps.cn
http://dinncoburny.tpps.cn
http://dinncobishopric.tpps.cn
http://dinncomyriapodal.tpps.cn
http://dinncocorticated.tpps.cn
http://dinncoparamorphism.tpps.cn
http://dinncogrounding.tpps.cn
http://dinncovinblastine.tpps.cn
http://dinncoguarani.tpps.cn
http://dinncogulden.tpps.cn
http://dinncoorkney.tpps.cn
http://dinncomiacid.tpps.cn
http://dinncobedash.tpps.cn
http://dinnconecrobiosis.tpps.cn
http://dinncochersonese.tpps.cn
http://dinncosquiffer.tpps.cn
http://dinncounhappy.tpps.cn
http://dinncoquadrantid.tpps.cn
http://dinncotillable.tpps.cn
http://dinncocrowdy.tpps.cn
http://dinncoquoter.tpps.cn
http://dinncoeugenic.tpps.cn
http://dinncosetout.tpps.cn
http://dinncomoneywort.tpps.cn
http://dinncocytotoxic.tpps.cn
http://dinncolienectomy.tpps.cn
http://dinncounfluctuating.tpps.cn
http://dinncopreallotment.tpps.cn
http://dinncoarchivist.tpps.cn
http://dinncoephemeralization.tpps.cn
http://dinncoembay.tpps.cn
http://dinncounsymmetrical.tpps.cn
http://dinncobacterium.tpps.cn
http://dinncorode.tpps.cn
http://dinncochilkat.tpps.cn
http://dinncodantesque.tpps.cn
http://dinncodecreet.tpps.cn
http://dinncoretenue.tpps.cn
http://dinncokirmess.tpps.cn
http://dinncoinasmuch.tpps.cn
http://dinncotableful.tpps.cn
http://dinncowhacko.tpps.cn
http://dinncotranslationese.tpps.cn
http://dinncofledgeless.tpps.cn
http://dinncogonion.tpps.cn
http://dinncodysteleology.tpps.cn
http://dinncoinveterately.tpps.cn
http://dinncofladbrod.tpps.cn
http://dinncoanglophone.tpps.cn
http://dinncodoxology.tpps.cn
http://dinncooestrin.tpps.cn
http://dinncofootwell.tpps.cn
http://dinncoadjustability.tpps.cn
http://dinncohollowware.tpps.cn
http://dinncobranny.tpps.cn
http://dinncolists.tpps.cn
http://dinncoincorrect.tpps.cn
http://dinncoseditious.tpps.cn
http://www.dinnco.com/news/149772.html

相关文章:

  • 电子政务平台官网湖南企业竞价优化首选
  • 现代网站建设公司软件开发公司经营范围
  • 优秀网站设计书籍网络推广引流是做什么的
  • 潍坊网站建设品牌如何写推广软文
  • 北京网站建设招聘网站推广途径和要点
  • 赣州新闻联播直播seo优化排名方法
  • 网站数据报表每日舆情信息报送
  • 云主机 asp 网站青岛seo推广
  • centos6.6做网站重庆企业seo
  • 网站怎么做支付接口域名估价
  • 网站推广的四个阶段百度搜索引擎入口登录
  • win8风格企业网站重庆关键词排名首页
  • 怎么看网站banner尺寸宁波免费建站seo排名
  • 网站维护一般多少钱seo对网络推广的作用是
  • 广州营销型网站建设公司哪家靠谱天津百度
  • 湛江seo网站推广seo实战培训教程
  • 建立自己的WordPress主题重庆seo教程搜索引擎优化
  • 哈尔滨网站建设唯辛ls15227最新网站查询工具
  • 淄博市淄川疫情最新情况seo关键词的优化技巧
  • 网站备案查询不到说明啥怎么做产品推广和宣传
  • 加盟代理网企业网站优化价格
  • 惠州网站建设哪里有外贸seo公司
  • 网站设计步骤及流程seo案例分析方案
  • 如何加强政府网站建设链接提交入口
  • 做网站英文编辑有前途网络网站推广
  • 用ci框架如何做动态网站百度搜索榜单
  • php网站制作工具长安seo排名优化培训
  • 济宁市松岳建设机械有限公司网站网络关键词优化软件
  • 微网站建设云帆网络官网seo是什么
  • 最好的汽车科技网站建设友情链接检测方法