您的位置:首页 > 文旅 > 旅游 > 最大的房产网站排名_厦门网站制作企业_跨界营销案例_百度热搜榜排名今日第一

最大的房产网站排名_厦门网站制作企业_跨界营销案例_百度热搜榜排名今日第一

2024/10/5 21:18:24 来源:https://blog.csdn.net/m0_54138660/article/details/142314674  浏览:    关键词:最大的房产网站排名_厦门网站制作企业_跨界营销案例_百度热搜榜排名今日第一
最大的房产网站排名_厦门网站制作企业_跨界营销案例_百度热搜榜排名今日第一

一、分享代码 

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.Threading;//串口参数结构体
struct COMPORT_ATTRIBUTE
{public int bandrate;public int data_bit;public Parity parity_check_bit;public StopBits stop_bit;public string comport_number;
};namespace uarttest2
{public partial class Form1 : Form{// 串口参数private COMPORT_ATTRIBUTE uart_port;//实例化串口类public System.IO.Ports.SerialPort _serialPort = new System.IO.Ports.SerialPort();//============================== 定义线程 ===========================================Thread listenThread;public bool rx_thread_stop_flag = false;//线程停止标志public bool rx_thread_start_flag = false;//线程停止标志//初始化端口public void InitializePorts(){string[] port_names = SerialPort.GetPortNames();string last_name = "";comport.Items.Clear();//清除数据if (port_names == null){MessageBox.Show("本机没有串口!", "Error");return;}foreach (string s in System.IO.Ports.SerialPort.GetPortNames()){//获取有多少个COM口就添加进COMBOX项目列表  comport.Items.Add(s);last_name = s;//保存最新的一个}comport.Text = last_name;//显示最新的一个串口uart_port.comport_number = last_name;//赋值变量}public void InitializeSerialSet(){// 初始化扫描串口InitializePorts();// 初始化波特率uart_port.bandrate = 115200;// 初始化数据位uart_port.data_bit = 8;// 初始化停止位uart_port.stop_bit = (StopBits)1;// 初始化校验位uart_port.parity_check_bit = 0;//Parity.Noneif (uart_port.parity_check_bit == (Parity)1)//  Parity.Odd{}else if (uart_port.parity_check_bit == (Parity)2) //Parity.Even{}else{}}public void Open_uart(){ if (_serialPort.IsOpen){_serialPort.Close();}_serialPort.PortName = comport.Text;            //串口名称_serialPort.BaudRate = uart_port.bandrate;      //波特率_serialPort.DataBits = uart_port.data_bit;      //数据位_serialPort.Parity = uart_port.parity_check_bit;//校验位_serialPort.StopBits = uart_port.stop_bit;      //停止位}public void Close_uart(){try {}catch (Exception ex){MessageBox.Show(ex.Message);}}/// <summary>/// 开关接收线程/// </summary>private void RxThreadOnOff(bool on_off){if (on_off){listenThread = new Thread(() => ReceiveData());//开启一个线程来不断的接收数据listenThread.IsBackground = true;listenThread.Start();}else{rx_thread_stop_flag = true;while (listenThread.IsAlive)//等待停止{ }rx_thread_stop_flag = false;}}/// <summary>/// 接收数据/// </summary>public void ReceiveData(){//接收线程while (_serialPort.IsOpen && !rx_thread_stop_flag)//线程循环{//......}}public Form1(){InitializeComponent();InitializeSerialSet();}private void scanuart_Click(object sender, EventArgs e){InitializePorts();}private void openuart_Click(object sender, EventArgs e){Open_uart();}}
}

串口通信基础及C#实现详解

串口通信(UART)是计算机与外部设备(如传感器、模块等)进行数据传输的常用方式。本文将介绍串口通信的基本原理,并通过一个C#示例讲解如何在Windows上实现串口通信。

什么是串口通信?

串口通信是指通过串行方式在两个设备之间进行数据传输的一种通信方式。与并行通信不同,串口通信的数据传输是按位进行的。串口通信最常见的应用是计算机与外设(如传感器、模块或单片机)之间的通信。

在串口通信中,常见的参数包括:

  • 波特率(Baud Rate):表示每秒传输的比特数。常用的波特率有9600、115200等。
  • 数据位(Data Bits):数据包中有效数据的位数,常见的有7位或8位。
  • 停止位(Stop Bits):用于标识一个数据包的结束,常见的有1位或2位停止位。
  • 校验位(Parity Bit):用于检测数据传输中的错误,分为无校验、偶校验和奇校验等。
  • 串口号(Port Number):表示计算机与外设通信时使用的物理串口,常见的有COM1、COM2等。
C#中的串口通信类

在C#中,System.IO.Ports.SerialPort 类封装了串口通信相关的功能。你可以通过该类轻松实现串口的打开、配置、数据发送与接收等操作。

本文就实现了通过配置这一段属性来实现打开串口的操作。界面元素展示如下:

 对应三个功能,扫描,开启,关闭串口的方法。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com