网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

利用C#项目实现一个串口监视上位机功能-创新互联

这篇文章将为大家详细讲解有关利用C# 项目实现一个串口监视上位机功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

网站的建设创新互联建站专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为自拌料搅拌车等企业提供专业服务。

实现上位机和下位机之间的通信,通常使用的是串口通信,接下来实现一个通过上位机和串口调试助手来完成串口通信测试。

首先创建一个WInfrom窗体应用工程文件,在创建好的工程下面,通过工具箱中已有的控件完成界面的搭建,如下图所示,为了方便初学者容易看懂程序,下图将控件的命名一并标注出来:

利用C# 项目实现一个串口监视上位机功能

直接进入正题,将完整的工程代码黏贴出来:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Diagnostics;

namespace Tem_Hum_Monitorring
{

 public partial class Form1 : Form
 {
 //实例化串口
 SerialPort s = new SerialPort();

 public Form1()
 {
  InitializeComponent();
  Control.CheckForIllegalCrossThreadCalls = false;
  button1.Text = "打开串口";
  int[] item = { 9600,115200}; //遍历
  foreach (int a in item)
  {
  comboBox2.Items.Add(a.ToString());
  }
  comboBox2.SelectedItem = comboBox2.Items[1];
 }

 private void Form1_Load(object sender, EventArgs e)
 {
  portInit();
 }

 /// 
 /// 串口初始化
 /// 
 private void portInit()
 {
  string[] ports = SerialPort.GetPortNames();
  comboBox1.Items.AddRange(ports);
  comboBox1.SelectedItem = comboBox1.Items[0];
 }

 #region 开关串口
 private void button1_Click(object sender, EventArgs e)
 {
  try
  {
  if (!s.IsOpen)
  {
   s.PortName = comboBox1.SelectedItem.ToString();
   s.BaudRate = Convert.ToInt32(comboBox2.SelectedItem.ToString());
   s.Open();
   s.DataReceived += s_DataReceived; //"+="代表指定响应事件时要调用的方法
   button1.Text = "关闭串口";
  }
  else
  {
   s.Close();
   s.DataReceived -= s_DataReceived;
   button1.Text = "打开串口";
  }
  }
  catch(Exception ee)
  {
  MessageBox.Show(ee.ToString());
  }
 }
 #endregion

 #region 串口接收
 void s_DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
  int count = s.BytesToRead;
  string str = null;
  if (count == 8)
  {
  //数据解析
  byte[] buff = new byte[count];
  s.Read(buff, 0, count);
  foreach (byte item in buff)
  {
   str += item.ToString("X2") + " ";
  }
  richTextBox1.Text = "[" + System.DateTime.Now.ToString() + "] " + str + "\n" + richTextBox1.Text;
  if (buff[0] == 0x04)
  {
   ID.Text = buff[0].ToString();
   switch (buff[2])
   {
   case 0x01:
    {
    Tem.Text = (buff[5] * 4 + buff[4] * 0.05 - 30).ToString();
    Hum.Text = (buff[6] + buff[7]).ToString();
    break;
    }
   case 0x02:
    {
    Light.Text = (buff[6] + buff[7]).ToString();
    break;
    }
   case 0x04:
    {
    Dust.Text = (buff[6] + buff[7]).ToString();
    break;
    }
   default:
    break;
   }
  }
  }
  else
  {
  //当接收数据不在设定的数据位范围之内时,会出现接受到的数据一直保存在接收缓存区之内,后续每次接手数据都会将上一次的数据进行叠加,造成只能通过关闭串口的方法来清除缓冲区的数据
  s.DiscardInBuffer(); //丢弃来自串行驱动程序的接收缓冲区的数据
  }
 }
 #endregion

 #region 串口发送
 private void button3_Click(object sender, EventArgs e)
 {
  string[] sendbuff = richTextBox2.Text.Split();
  Debug.WriteLine("发送字节数:" + sendbuff.Length);
  foreach (string item in sendbuff)
  {
  int count = 1;
  byte[] buff = new byte[count];
  buff[0] = byte.Parse(item, System.Globalization.NumberStyles.HexNumber);
  s.Write(buff,0,count);
  }
 }
 #endregion

 private void button2_Click(object sender, EventArgs e)
 {
  int count = 1;
  byte[] buff = new byte[count];
  buff[0] = byte.Parse("04", System.Globalization.NumberStyles.HexNumber);
  s.Write(buff, 0, count);
 }
 }
}

当前标题:利用C#项目实现一个串口监视上位机功能-创新互联
本文路径:http://bjjierui.cn/article/dgjiho.html

其他资讯