1. 基本概念
- 数据是串行发送的,比如RS232、RS422、RS485,一位一位地发送数据,就是串口通信。串口通信分为同步和异步。
- 串口通信要设置以下参数:
- 串口号:com1,com2…
- 波特率:9600,115200…
- 数据位:8,7,6
- 校验位:None,Even,Odd
- 停止位:1,2
- 9针脚,2针脚收数据,3针脚发数据
2.C#代码实现串口通信
- 串口实例化
- 设置串口参数
- 打开串口
- 发送数据
- 接收数据(事件响应)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a958793b27f146f052a9d1abe61137ac.png)
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;
using System.IO.Ports;
namespace SerialPortCom
{
public partial class SerialPortForm : Form
{
#region 接收数据事件响应
public void DataRecv(object sender, SerialDataReceivedEventArgs e)
{
int counts = sp.BytesToRead;
byte[] buffer = new byte[counts];
sp.Read(buffer, 0, counts);
if (radioButtonString.Checked)
{
Invoke(new MethodInvoker(delegate ()
{
textBoxRec.Text += Encoding.Default.GetString(buffer);
labelReceByte.Text = buffer.Length.ToString();
}));
}
else
{
Invoke(new MethodInvoker(delegate()
{
textBoxRec.Text += HexByteToString(buffer);
labelReceByte.Text = buffer.Length.ToString();
}));
}
}
#endregion
#region 串口实例化
SerialPort sp = new SerialPort();
public SerialPortForm()
{
InitializeComponent();
}
#endregion
#region 设置串口参数
private void SerialPortForm_Load(object sender, EventArgs e)
{
string[] comNames = SerialPort.GetPortNames();
for(int i=0; i < comNames.Length; i++)
{
comboBoxPortNum.Items.Add(comNames[i]);
}
if(comNames.Length>0)
{
comboBoxPortNum.SelectedIndex = 0;
}
comboBoxBaudRate.SelectedIndex = 1;
comboBoxDataBit.SelectedIndex = 0;
comboBoxCheckBit.SelectedIndex = 0;
comboBoxStopBit.SelectedIndex = 0;
}
#endregion
#region 打开串口
private void buttonOpenPort_Click(object sender, EventArgs e)
{
if(!sp.IsOpen)
{
sp.PortName = comboBoxPortNum.Text;
sp.BaudRate = Convert.ToInt32(comboBoxBaudRate.Text);
sp.DataBits = Convert.ToInt32(comboBoxDataBit.Text);
sp.Parity = (Parity)Enum.Parse(typeof(Parity),comboBoxCheckBit.Text);
sp.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopBit.Text);
sp.Open();
buttonOpenPort.Text = "关闭串口";
buttonOpenPort.BackColor = Color.Green;
sp.DataReceived += new SerialDataReceivedEventHandler(DataRecv);
}
else
{
sp.Close();
buttonOpenPort.Text = "打开串口";
buttonOpenPort.BackColor = Color.White;
}
}
#endregion
#region 发送
private void buttonSend_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
byte[] buffer;
if (radioButtonString.Checked)
{
buffer = Encoding.Default.GetBytes(textBoxSend.Text);
sp.Write(buffer, 0, buffer.Length);
}
else
{
buffer = StringToHexByte(textBoxSend.Text);
sp.Write(buffer, 0, buffer.Length);
}
labelSendByte.Text = Convert.ToString(buffer.Length);
}
else
{
MessageBox.Show("");
}
}
#endregion
#region 字符串转16进制
public byte[] StringToHexByte(string str)
{
str.Replace(" ", "");
str.Replace("0x", "");
str.Replace("0X", "");
if (str.Length % 2 != 0)
{
str = "0" + str;
}
byte[] newbuffer = new byte[str.Length / 2];
for (int i = 0; i < str.Length / 2; i++)
{
newbuffer[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
}
return newbuffer;
}
#endregion
#region 16进制转字符串
public string HexByteToString(byte[] buffer)
{
string str = "";
if(buffer==null)
{
return "0";
}
for(int i=0;i<buffer.Length;i++)
{
str += buffer[i].ToString("X2");
}
return str;
}
#endregion
#region 清空发送字节
private void buttonClearSendByte_Click(object sender, EventArgs e)
{
labelSendByte.Text = "0";
}
#endregion
#region 清空接收字节
private void buttonClearRecvByte_Click(object sender, EventArgs e)
{
labelReceByte.Text = "0";
}
#endregion
#region 清空发送区
private void buttonClearSend_Click(object sender, EventArgs e)
{
textBoxSend.Text = "";
}
#endregion
#region 清空接收区
private void buttonClearRecv_Click(object sender, EventArgs e)
{
textBoxRec.Text = "";
}
#endregion
}
}
3. 资源下载地址
https://download.csdn.net/download/weixin_38566632/18594094