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

模具东莞网站建设关键词百度网盘

模具东莞网站建设,关键词百度网盘,大学生网页设计实训报告,有关网站建设的参考书记录时间;2024年4月 记录如何开启虚拟串口以及进行基础串口通信。 建立虚拟串口 使用的软件是vspd,建立虚拟串口之后就可以将他们当成实际物理连接的两个串口进行通信。 之后使用我们之前给出的通信模板,建立一个稍微规矩一点的界面。 界面建立 其中…

记录时间;2024年4月

记录如何开启虚拟串口以及进行基础串口通信。

建立虚拟串口

使用的软件是vspd,建立虚拟串口之后就可以将他们当成实际物理连接的两个串口进行通信。

之后使用我们之前给出的通信模板,建立一个稍微规矩一点的界面。

界面建立

 

 其中添加对用户输入的检查。

配合之前打开串口的逻辑就可以开始串口应用的调试和开发了。

代码

  public partial class Form1 : Form{//串口参数和串口工具类public System.IO.Ports.SerialPort _serialPort = new System.IO.Ports.SerialPort();string[] baud = { "43000", "56000", "57600", "115200", "128000", "230400", "256000", "460800" };private void displaytext(object sender, EventArgs e){string str = _serialPort.ReadExisting();rtxreceive.AppendText(str);}//串口接收事件private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e){//串口接收事件try{//串口接收数据//if(serialPort1.IsOpen){this.Invoke(new EventHandler(displaytext));}}catch (Exception ex){//捕获到异常,创建一个新的对象,之前的不可以再用_serialPort = new System.IO.Ports.SerialPort();//刷新COM口选项comboBox1.Items.Clear();comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());//响铃并显示异常给用户System.Media.SystemSounds.Beep.Play();button4.Text = "打开串口";MessageBox.Show(ex.Message);hintrtx.Text += "\n串口接收失败";comboBox1.Enabled = true;comboBox2.Enabled = true;comboBox3.Enabled = true;comboBox4.Enabled = true;comboBox5.Enabled = true;}}public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){//检测try{string[] ports = SerialPort.GetPortNames();if (ports.Length == 0){MessageBox.Show("没有找到串口");hintrtx.Text += "没有找到串口";return;}comboBox1.Items.Clear();comboBox1.Items.AddRange(ports);comboBox1.SelectedIndex = 0;comboBox2.SelectedIndex = 0;comboBox3.SelectedIndex = 0;comboBox4.SelectedIndex = 0;comboBox5.SelectedIndex = 0;//检测到串口后,使能串口参数设置comboBox1.Enabled = true;comboBox2.Enabled = true;comboBox3.Enabled = true;comboBox4.Enabled = true;comboBox5.Enabled = true;}catch (Exception ex){MessageBox.Show(ex.Message);}}private void Form1_Load(object sender, EventArgs e){//界面初始化int i;for (i = 300; i <= 38400; i = i * 2){comboBox2.Items.Add(i.ToString());  //添加波特率列表}comboBox2.Items.AddRange(baud);comboBox1.Enabled = false;comboBox2.Enabled = false;comboBox3.Enabled = false;comboBox4.Enabled = false;comboBox5.Enabled = false;hintrtx.Text += "欢迎使用";}private void button4_Click(object sender, EventArgs e){//开启串口try{if (_serialPort.IsOpen){//如果串口已经处于打开状态,则关闭串口_serialPort.Close();button4.Text = "打开串口";hintrtx.Text += "\n串口已经关闭";//关闭串口后,使能串口参数设置comboBox1.Enabled = true;comboBox2.Enabled = true;comboBox3.Enabled = true;comboBox4.Enabled = true;comboBox5.Enabled = true;//取消串口接收事件注册_serialPort.DataReceived -= new SerialDataReceivedEventHandler(_serialPort_DataReceived);return;}else{//串口已经处于关闭状态,则设置好串口属性后打开comboBox1.Enabled = false;comboBox2.Enabled = false;comboBox3.Enabled = false;comboBox4.Enabled = false;comboBox5.Enabled = false;_serialPort.PortName = comboBox1.Text;_serialPort.BaudRate = Convert.ToInt32(comboBox2.Text);_serialPort.DataBits = Convert.ToInt16(comboBox3.Text);_serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox4.Text);if (comboBox5.Text.Equals("1"))_serialPort.StopBits = System.IO.Ports.StopBits.One;else if (comboBox5.Text.Equals("1.5"))_serialPort.StopBits = System.IO.Ports.StopBits.OnePointFive;else if (comboBox5.Text.Equals("2"))_serialPort.StopBits = System.IO.Ports.StopBits.Two;//_serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox5.Text);_serialPort.Open();//注册串口接收事件_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);button4.Text = "关闭串口";hintrtx.Text += "\n串口已经打开";}}catch (Exception ex){MessageBox.Show(ex.Message);hintrtx.Text += "\n串口打开失败";}}private void button3_Click(object sender, EventArgs e){//try{//首先判断串口是否开启if (_serialPort.IsOpen){//串口处于开启状态,将发送区文本发送_serialPort.Write(rtx_send.Text);}}catch (Exception ex){//捕获到异常,创建一个新的对象,之前的不可以再用_serialPort = new System.IO.Ports.SerialPort();//刷新COM口选项comboBox1.Items.Clear();comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());//响铃并显示异常给用户System.Media.SystemSounds.Beep.Play();button4.Text = "打开串口";MessageBox.Show(ex.Message);hintrtx.Text += "\n串口发送失败";comboBox1.Enabled = true;comboBox2.Enabled = true;comboBox3.Enabled = true;comboBox4.Enabled = true;comboBox5.Enabled = true;}}}

代码解释:

扫描串口:

我们使用serialport类进行串口的扫描,能够给到一个串口名称的列表。展示的中间修改页面元素的可用性,允许用户修改串口配置。

private void button1_Click(object sender, EventArgs e){//检测try{string[] ports = SerialPort.GetPortNames();if (ports.Length == 0){MessageBox.Show("没有找到串口");hintrtx.Text += "没有找到串口";return;}comboBox1.Items.Clear();comboBox1.Items.AddRange(ports);comboBox1.SelectedIndex = 0;comboBox2.SelectedIndex = 0;comboBox3.SelectedIndex = 0;comboBox4.SelectedIndex = 0;comboBox5.SelectedIndex = 0;//检测到串口后,使能串口参数设置comboBox1.Enabled = true;comboBox2.Enabled = true;comboBox3.Enabled = true;comboBox4.Enabled = true;comboBox5.Enabled = true;}catch (Exception ex){MessageBox.Show(ex.Message);}}

打开/关闭串口

逻辑非常简单,判断是否已经打开,如果已经打开就关闭。

打开的逻辑是:

读取用户设置之前先锁定配置,防止调试中用户修改。

接着就是打开串口,注册串口接收事件用于执行接收指定的逻辑。

关闭串口更加简单,重新给予用户修改配置的机会,同时关闭串口,取消事件的注册。

事件机制是串口收发的核心:

C#笔记4 详细解释事件及其原型、匿名方法和委托的关系-CSDN博客

c#笔记5 详解事件的内置类型EventHandler、windows事件在winform中的运用_c# eventhandler用法-CSDN博客

 private void button4_Click(object sender, EventArgs e){//开启串口try{if (_serialPort.IsOpen){//如果串口已经处于打开状态,则关闭串口_serialPort.Close();button4.Text = "打开串口";hintrtx.Text += "\n串口已经关闭";//关闭串口后,使能串口参数设置comboBox1.Enabled = true;comboBox2.Enabled = true;comboBox3.Enabled = true;comboBox4.Enabled = true;comboBox5.Enabled = true;//取消串口接收事件注册_serialPort.DataReceived -= new SerialDataReceivedEventHandler(_serialPort_DataReceived);return;}else{//串口已经处于关闭状态,则设置好串口属性后打开comboBox1.Enabled = false;comboBox2.Enabled = false;comboBox3.Enabled = false;comboBox4.Enabled = false;comboBox5.Enabled = false;_serialPort.PortName = comboBox1.Text;_serialPort.BaudRate = Convert.ToInt32(comboBox2.Text);_serialPort.DataBits = Convert.ToInt16(comboBox3.Text);_serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox4.Text);if (comboBox5.Text.Equals("1"))_serialPort.StopBits = System.IO.Ports.StopBits.One;else if (comboBox5.Text.Equals("1.5"))_serialPort.StopBits = System.IO.Ports.StopBits.OnePointFive;else if (comboBox5.Text.Equals("2"))_serialPort.StopBits = System.IO.Ports.StopBits.Two;//_serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox5.Text);_serialPort.Open();//注册串口接收事件_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);button4.Text = "关闭串口";hintrtx.Text += "\n串口已经打开";}}catch (Exception ex){MessageBox.Show(ex.Message);hintrtx.Text += "\n串口打开失败";}}

串口接收事件:

这里主要是做了一个串口的错误处理和调用界面元素显示的方法。从不是创建这个元素的线程调用改变元素的方法需要使用invoke。

//串口接收事件private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e){//串口接收事件try{//串口接收数据//if(serialPort1.IsOpen){this.Invoke(new EventHandler(displaytext));}}catch (Exception ex){//捕获到异常,创建一个新的对象,之前的不可以再用_serialPort = new System.IO.Ports.SerialPort();//刷新COM口选项comboBox1.Items.Clear();comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());//响铃并显示异常给用户System.Media.SystemSounds.Beep.Play();button4.Text = "打开串口";MessageBox.Show(ex.Message);hintrtx.Text += "\n串口接收失败";comboBox1.Enabled = true;comboBox2.Enabled = true;comboBox3.Enabled = true;comboBox4.Enabled = true;comboBox5.Enabled = true;}}

display方法 

这里就可以写你自己的方法了。串口的数据经过怎么处理展示到哪里。 

private void displaytext(object sender, EventArgs e){string str = _serialPort.ReadExisting();rtxreceive.AppendText(str);}

http://www.hkea.cn/news/282113/

相关文章:

  • 韩国吃秀在哪个网站做直播企业宣传
  • 江西网站建设成都百度
  • 糯米团网站怎么做微信软文范例100字
  • 如何在社交网站上做视频推广seo营销的概念
  • 大连做网站仟亿科技最新域名查询
  • 网站开发实施计划与安排宁波网络推广方式
  • 企业网站建设公司注意哪些问题软件开发外包公司
  • abc网站建设怎么样yandex引擎搜索入口
  • wordpress屏蔽f12广州seo网络优化公司
  • 南宁网站建设推广服务云服务器免费
  • 大数据营销是什么seo站长
  • 建设政府网站的公司乐山网站seo
  • 仿站容易还是建站容易专业做灰色关键词排名
  • 做网站背景音乐管理课程培训
  • 网站建设可以自学吗品牌软文范文
  • 网站风格对比哪里有学计算机培训班
  • 做mla的网站网站优化哪家好
  • 网站注册的账号怎么注销线上营销活动有哪些
  • 国内做进口的电商网站网站推广软件哪个好
  • 谁有做那事的网站百度投诉中心入口
  • 免费单页网站在线制作沈阳seo排名优化教程
  • 廊坊网站建大型网站建站公司
  • 远程桌面做网站sem和seo区别与联系
  • 做贷款网站优化大师有用吗
  • 有没有便宜的网站制作制作网页教程
  • 医院网站制作优化关键词的方法有哪些
  • wordpress安装到网站吗泰安seo
  • 长春网站开发培训价格google play三件套
  • 做生存分析的网站有哪些国外新闻最新消息
  • 济南网站优化收费百度互联网营销