C# 实现简易串口监视上位机完整步骤

360影视 动漫周边 2025-05-29 08:31 2

摘要:在工业自动化和物联网项目中,串口通信是实现上位机和下位机之间数据交换的常用方式。本文将指导您如何使用 C# 和 WinForms 开发一个简易的串口监视上位机应用程序。通过这个应用程序,您可以轻松地发送和接收串口数据,监控设备状态。

在工业自动化和物联网项目中,串口通信是实现上位机和下位机之间数据交换的常用方式。本文将指导您如何使用 C# 和 WinForms 开发一个简易的串口监视上位机应用程序。通过这个应用程序,您可以轻松地发送和接收串口数据,监控设备状态。

打开 Visual Studio,选择“创建新项目”。

在项目类型中选择“Windows 窗体应用”,命名为“SerialPortMonitor”。

从工具箱中拖拽以下控件到窗体:

两个 ComboBox 控件(命名为 comboBoxPort 和 comboBoxBaudRate),用于选择串口号和波特率。

两个 Button 控件(命名为 buttonOpenPort 和 buttonSendData),分别用于打开/关闭串口和发送数据。

一个 RichTextBox 控件(命名为 richTextBoxReceiveData),用于显示接收到的数据。

一个 TextBox 控件(命名为 textBoxSendData),用于输入要发送的数据。

下面是完整的代码示例,实现了串口的打开/关闭、数据发送和接收功能。

```csharpusing System;using System.IO.Ports;using System.Windows.Forms;namespace SerialPortMonitor{ public partial class Form1 : Form { private SerialPort serialPort; public Form1 { InitializeComponent; serialPort = new SerialPort; InitializePortSettings; } private void InitializePortSettings { // 获取系统中所有可用的串口 string ports = SerialPort.GetPortNames; comboBoxPort.Items.AddRange(ports); if (ports.Length > 0) { comboBoxPort.SelectedIndex = 0; } // 设置常用的波特率 int baudRates = { 9600, 115200, 57600, 38400 }; comboBoxBaudRate.Items.AddRange(baudRates); comboBoxBaudRate.SelectedIndex = 1; } private void buttonOpenPort_Click(object sender, EventArgs e) { try { if (!serialPort.IsOpen) { // 打开串口 serialPort.PortName = comboBoxPort.SelectedItem.ToString; serialPort.BaudRate = int.Parse(comboBoxBaudRate.SelectedItem.ToString); serialPort.DataReceived += SerialPort_DataReceived; serialPort.Open; buttonOpenPort.Text = "关闭串口"; } else { // 关闭串口 serialPort.DataReceived -= SerialPort_DataReceived; serialPort.Close; buttonOpenPort.Text = "打开串口"; } } catch (Exception ex) { MessageBox.Show("串口操作失败: " + ex.Message); } } private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { // 接收数据 string receivedData = serialPort.ReadExisting; // 更新 UI,显示接收到的数据 this.Invoke(new Action( => { richTextBoxReceiveData.AppendText("[" + DateTime.Now.ToString + "] 收到: " + receivedData + Environment.NewLine); })); } catch (Exception ex) { MessageBox.Show("接收数据错误: " + ex.Message); } } private void buttonSendData_Click(object sender, EventArgs e) { try { if (serialPort.IsOpen) { // 发送数据 string dataToSend = textBoxSendData.Text; serialPort.WriteLine(dataToSend); richTextBoxReceiveData.AppendText("[" + DateTime.Now.ToString + "] 发送: " + dataToSend + Environment.NewLine); } else { MessageBox.Show("请先打开串口!"); } } catch (Exception ex) { MessageBox.Show("发送数据错误: " + ex.Message); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // 关闭窗体时,确保串口已关闭 { serialPort.Close; } } }}```

在窗体加载时初始化串口设置,自动获取系统中的串口号,并设置常用的波特率。

```csharpprivate void InitializePortSettings{ string ports = SerialPort.GetPortNames; comboBoxPort.Items.AddRange(ports); if (ports.Length > 0) { comboBoxPort.SelectedIndex = 0; } int baudRates = { 9600, 115200, 57600, 38400 }; comboBoxBaudRate.Items.AddRange(baudRates); comboBoxBaudRate.SelectedIndex = 1;}```

通过按钮点击事件实现串口的打开和关闭操作。

```csharpprivate void buttonOpenPort_Click(object sender, EventArgs e){ try { if (!serialPort.IsOpen) { serialPort.PortName = comboBoxPort.SelectedItem.ToString; serialPort.BaudRate = int.Parse(comboBoxBaudRate.SelectedItem.ToString); serialPort.DataReceived += SerialPort_DataReceived; serialPort.Open; buttonOpenPort.Text = "关闭串口"; } else { serialPort.DataReceived -= SerialPort_DataReceived; serialPort.Close; buttonOpenPort.Text = "打开串口"; } } catch (Exception ex) { MessageBox.Show("串口操作失败: " + ex.Message); }}```

在串口数据接收事件中读取数据,并在 UI 中显示。

```csharpprivate void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e){ try { string receivedData = serialPort.ReadExisting; this.Invoke(new Action( => { richTextBoxReceiveData.AppendText("[" + DateTime.Now.ToString + "] 收到: " + receivedData + Environment.NewLine); })); } catch (Exception ex) { MessageBox.Show("接收数据错误: " + ex.Message); }}```

通过按钮点击事件发送数据到串口。

```csharpprivate void buttonSendData_Click(object sender, EventArgs e){ try { if (serialPort.IsOpen) { string dataToSend = textBoxSendData.Text; serialPort.WriteLine(dataToSend); richTextBoxReceiveData.AppendText("[" + DateTime.Now.ToString + "] 发送: " + dataToSend + Environment.NewLine); } else { MessageBox.Show("请先打开串口!"); } } catch (Exception ex) { MessageBox.Show("发送数据错误: " + ex.Message); }}```

无法打开串口

检查串口号是否正确。

确保串口未被其他程序占用。

检查程序是否有足够的权限访问串口。

接收数据不完整或延迟

确保波特率设置正确。

检查接收缓冲区是否已满。

尝试调整接收事件的触发条件。

发送数据失败

确保串口已打开。

检查发送的数据格式是否符合设备要求。

通过本文的示例代码和步骤,您可以快速实现一个简易的串口监视上位机。这个应用程序可以帮助您轻松地进行串口通信测试,监控设备状态。您还可以根据实际需求进一步扩展功能,如增加数据解析、保存日志等功能。如果您有任何疑问或需要进一步的支持,请随时留言。

来源:opendotnet

相关推荐