这篇文章主要为大家详细介绍了C#基于SerialPort类实现串口通讯,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C#基于SerialPort类实现串口通讯的具体代码,供大家参考,具体内容如下
最终效果
窗体设置:
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.IO.Ports;
using System.Threading;
using System.Windows.Forms;
namespace SerialCommunication
{
public partial class Form1 : Form
{
System.Threading.SynchronizationContext m_SyncContext = null;
SerialPort serialPort = null;
ToHexadecimalString toHexadecimalString = new ToHexadecimalString();
public Form1()
{
m_SyncContext = SynchronizationContext.Current;
InitializeComponent();
}
//Data initialization
private void Form1_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
SerialPortNumber.Items.AddRange(ports);
SerialPortNumber.SelectedIndex = SerialPortNumber.Items.Count > 0 ? 0 : -1;
BaudRateCom.Text = "9600"; //Set parameters
CheckBitCom.Text = "None";
DataBitCom.Text = "8";
StopBitCom.Text = "One";
}
//Send data
private void Send_Click(object sender, EventArgs e)
{
try
{
//Send data and handle exceptions
byte[] bytes = new byte[SendMessage.Text.Length];
bytes = Encoding.Default.GetBytes(SendMessage.Text);
serialPort.Write(bytes, 0, bytes.Length);
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
// Byte to hexadecimal string
//Clear data
private void Clear_Click(object sender, EventArgs e)
{
ReceiveMessage.Text = "";
}
private void Open_Click(object sender, EventArgs e)
{
string portName = SerialPortNumber.Text;
int buadRate = int.Parse(BaudRateCom.Text);
Parity parity = 0;
switch (CheckBitCom.Text)
{
case "None":
parity = Parity.None; break;
case "Odd":
parity = Parity.Odd; break;
case "Even":
parity = Parity.Even; break;
case "Mark":
parity = Parity.Mark; break;
case "Space":
parity = Parity.Space; break;
}
int dataBit = int.Parse(DataBitCom.Text);
StopBits stopBits = 0;
switch (StopBitCom.Text)
{
case "None":
stopBits = StopBits.None; break;
case "One":
stopBits = StopBits.One; break;
case "Tow":
stopBits = StopBits.Two; break;
case "OnePointFive":
stopBits = StopBits.OnePointFive; break;
}
try
{
if (Open.Text == "Open")
{
serialPort = new SerialPort(portName, buadRate, parity, dataBit, stopBits);
serialPort.Open();
Open.Text = "Close";
}
else if (Open.Text == "Close")
{
serialPort.Close();
Open.Text = "Open";
}
}
catch (Exception errror)
{
MessageBox.Show(errror.Message);
}
serialPort.DataReceived += onDataReceived;
}
private void onDataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
Byte[] bytes = new byte[serialPort.BytesToRead];
serialPort.Read(bytes, 0, serialPort.BytesToRead);
m_SyncContext.Post(new SendOrPostCallback((obj) =>
{
ReceiveMessage.AppendText(Encoding.Default.GetString(bytes));
}), bytes);
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:C#基于SerialPort类实现串口通讯详解
基础教程推荐
猜你喜欢
- unity实现动态排行榜 2023-04-27
- winform把Office转成PDF文件 2023-06-14
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# 调用WebService的方法 2023-03-09
- C#控制台实现飞行棋小游戏 2023-04-22
- C# List实现行转列的通用方案 2022-11-02
- C#类和结构详解 2023-05-30
- ZooKeeper的安装及部署教程 2023-01-22
- 一个读写csv文件的C#类 2022-11-06
- C# windows语音识别与朗读实例 2023-04-27