winform实现串口通信
时间: 2023-12-06 16:03:09 浏览: 40
以下是WinForm实现串口通信的步骤:
1.在窗体上添加控件:Label、ComboBox、Button、TextBox等。
2.在代码中定义串口对象SerialPort,以及串口参数:串口号、波特率、数据位、停止位、校验位等。
3.在代码中编写串口打开、关闭、发送数据、接收数据等相关操作。
4.在代码中编写串口接收数据的回调函数,将接收到的数据显示在文本框中。
5.在代码中编写保存串口参数设置的函数,以便下次打开程序时能够自动加载上次的设置。
以下是一个WinForm实现串口通信的示例代码:
```csharp
using System;
using System.IO.Ports;
using System.Windows.Forms;
namespace SerialPortDemo
{
public partial class Form1 : Form
{
private SerialPort _serialPort = new SerialPort();
private Tuple<Label, ComboBox>[] _portSetting = new Tuple<Label, ComboBox>[5];
private Button _btnSwitch = new Button();
private Button _btnSaveSetting = new Button();
private TextBox _txtTrans = new TextBox();
public Form1()
{
InitializeComponent();
// 初始化串口参数设置控件
_portSetting[0] = new Tuple<Label, ComboBox>(label1, comboBox1);
_portSetting[1] = new Tuple<Label, ComboBox>(label2, comboBox2);
_portSetting[2] = new Tuple<Label, ComboBox>(label3, comboBox3);
_portSetting[3] = new Tuple<Label, ComboBox>(label4, comboBox4);
_portSetting[4] = new Tuple<Label, ComboBox>(label5, comboBox5);
// 初始化串口开关按钮
_btnSwitch.Text = "打开串口";
_btnSwitch.Click += new EventHandler(btnSwitch_Click);
// 初始化保存设置按钮
_btnSaveSetting.Text = "保存设置";
_btnSaveSetting.Click += new EventHandler(btnSaveSetting_Click);
// 初始化文本框
_txtTrans.Multiline = true;
_txtTrans.ScrollBars = ScrollBars.Vertical;
_txtTrans.ReadOnly = true;
// 将控件添加到窗体上
this.Controls.Add(_btnSwitch);
this.Controls.Add(_btnSaveSetting);
this.Controls.Add(_txtTrans);
foreach (var item in _portSetting)
{
this.Controls.Add(item.Item1);
this.Controls.Add(item.Item2);
}
// 加载串口参数设置
LoadSerialPortSetting();
}
private void btnSwitch_Click(object sender, EventArgs e)
{
if (_serialPort.IsOpen)
{
// 关闭串口
_serialPort.Close();
_btnSwitch.Text = "打开串口";
}
else
{
// 打开串口
try
{
_serialPort.PortName = _portSetting[0].Item2.Text;
_serialPort.BaudRate = int.Parse(_portSetting[1].Item2.Text);
_serialPort.DataBits = int.Parse(_portSetting[2].Item2.Text);
_serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), _portSetting[3].Item2.Text);
_serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), _portSetting[4].Item2.Text);
_serialPort.Open();
_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
_btnSwitch.Text = "关闭串口";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void btnSaveSetting_Click(object sender, EventArgs e)
{
// 保存串口参数设置
Properties.Settings.Default.PortName = _portSetting[0].Item2.Text;
Properties.Settings.Default.BaudRate = _portSetting[1].Item2.Text;
Properties.Settings.Default.DataBits = _portSetting[2].Item2.Text;
Properties.Settings.Default.StopBits = _portSetting[3].Item2.Text;
Properties.Settings.Default.Parity = _portSetting[4].Item2.Text;
Properties.Settings.Default.Save();
}
private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// 接收数据回调函数
string recv = _serialPort.ReadExisting();
_txtTrans.Invoke(new Action(() => _txtTrans.Text += recv));
}
private void LoadSerialPortSetting()
{
// 加载串口参数设置
_portSetting[0].Item2.Text = Properties.Settings.Default.PortName;
_portSetting[1].Item2.Text = Properties.Settings.Default.BaudRate;
_portSetting[2].Item2.Text = Properties.Settings.Default.DataBits;
_portSetting[3].Item2.Text = Properties.Settings.Default.StopBits;
_portSetting[4].Item2.Text = Properties.Settings.Default.Parity;
}
}
}
```