赞
踩
以下是用C#写的串口助手。该程序使用了Windows Forms和System.IO.Ports命名空间。
- using System;
- using System.IO.Ports;
- using System.Windows.Forms;
-
- namespace SerialAssistant {
- public partial class MainForm : Form {
- private SerialPort serialPort;
-
- public MainForm() {
- InitializeComponent();
- serialPort = new SerialPort();
- }
-
- private void MainForm_Load(object sender, EventArgs e) {
- baudRateComboBox.Items.AddRange(new object[] { "9600", "19200", "38400", "57600", "115200" });
- parityComboBox.Items.AddRange(Enum.GetNames(typeof(Parity)));
- dataBitsComboBox.Items.AddRange(new object[] { "5", "6", "7", "8" });
- stopBitsComboBox.Items.AddRange(Enum.GetNames(typeof(StopBits)));
-
- portNameComboBox.Items.AddRange(SerialPort.GetPortNames());
- if (portNameComboBox.Items.Count > 0) {
- portNameComboBox.SelectedIndex = 0;
- }
- }
-
- private void refreshButton_Click(object sender, EventArgs e) {
- portNameComboBox.Items.Clear();
- portNameComboBox.Items.AddRange(SerialPort.GetPortNames());
- if (portNameComboBox.Items.Count > 0) {
- portNameComboBox.SelectedIndex = 0;
- }
- }
-
- private void connectButton_Click(object sender, EventArgs e) {
- if (!serialPort.IsOpen) {
- try {
- serialPort.PortName = portNameComboBox.SelectedItem.ToString();
- serialPort.BaudRate = int.Parse(baudRateComboBox.SelectedItem.ToString());
- serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), parityComboBox.SelectedItem.ToString());
- serialPort.DataBits = int.Parse(dataBitsComboBox.SelectedItem.ToString());
- serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBitsComboBox.SelectedItem.ToString());
- serialPort.Open();
- connectButton.Text = "断开";
- sendButton.Enabled = true;
- } catch (Exception ex) {
- MessageBox.Show(ex.Message);
- }
- } else {
- serialPort.Close();
- connectButton.Text = "连接";
- sendButton.Enabled = false;
- }
- }
-
- private void sendButton_Click(object sender, EventArgs e) {
- string text = sendTextBox.Text.Trim();
- if (!string.IsNullOrEmpty(text)) {
- try {
- serialPort.WriteLine(text);
- sendTextBox.Clear();
- } catch (Exception ex) {
- MessageBox.Show(ex.Message);
- }
- }
- }
-
- private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
- if (serialPort.IsOpen) {
- serialPort.Close();
- }
- }
-
- private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) {
- string text = serialPort.ReadLine();
- this.Invoke(new Action(() => {
- receiveTextBox.AppendText(text);
- }));
- }
- }
- }

在上面的代码中,我们创建了一个名为MainForm的Windows窗体,并在其中添加了以下控件:
一个ComboBox控件用于选择串口号。
一个ComboBox控件用于选择波特率。
一个ComboBox控件用于选择奇偶校验位。
一个ComboBox控件用于选择数据位。
一个ComboBox控件用于选择停止位。
一个Button控件用于刷新可用串口列表。
一个Button控件用于连接/断开串口。
一个TextBox控件用于输入要发送的数据。
一个Button控件用于发送数据。
一个TextBox控件用于显示接收到的数据。
在窗体加载时,我们将可用的波特率、奇偶校验位、数据位和停止位添加到相应的ComboBox控件中,并将可用的串口号添加到portNameComboBox控件中。如果有可用的串口,则默认选中第一个串口。
在连接按钮的Click事件处理程序中,我们首先检查串口是否已经打开。如果串口未打开,则使用选择的端口名称、波特率、奇偶校验位、数据位和停止位打开串口。如果串口已经打开,则关闭串口。
在发送按钮的Click事件处理程序中,我们从sendTextBox控件中获取要发送的文本。如果文本不为空,则向串口写入该文本,并清空sendTextBox控件。
在窗体关闭时,如果串口已经打开,则关闭串口。
最后,在serialPort_DataReceived事件处理程序中,我们从串口读取数据并将其追加到receiveTextBox控件中。由于该事件可能会在不同的线程上触发,因此我们需要在UI线程上执行操作,以避免跨线程访问错误。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。