当前位置:   article > 正文

用C#写个串口助手_c#串口助手源代码

c#串口助手源代码

以下是用C#写的串口助手。该程序使用了Windows Forms和System.IO.Ports命名空间。

  1. using System;
  2. using System.IO.Ports;
  3. using System.Windows.Forms;
  4. namespace SerialAssistant {
  5. public partial class MainForm : Form {
  6. private SerialPort serialPort;
  7. public MainForm() {
  8. InitializeComponent();
  9. serialPort = new SerialPort();
  10. }
  11. private void MainForm_Load(object sender, EventArgs e) {
  12. baudRateComboBox.Items.AddRange(new object[] { "9600", "19200", "38400", "57600", "115200" });
  13. parityComboBox.Items.AddRange(Enum.GetNames(typeof(Parity)));
  14. dataBitsComboBox.Items.AddRange(new object[] { "5", "6", "7", "8" });
  15. stopBitsComboBox.Items.AddRange(Enum.GetNames(typeof(StopBits)));
  16. portNameComboBox.Items.AddRange(SerialPort.GetPortNames());
  17. if (portNameComboBox.Items.Count > 0) {
  18. portNameComboBox.SelectedIndex = 0;
  19. }
  20. }
  21. private void refreshButton_Click(object sender, EventArgs e) {
  22. portNameComboBox.Items.Clear();
  23. portNameComboBox.Items.AddRange(SerialPort.GetPortNames());
  24. if (portNameComboBox.Items.Count > 0) {
  25. portNameComboBox.SelectedIndex = 0;
  26. }
  27. }
  28. private void connectButton_Click(object sender, EventArgs e) {
  29. if (!serialPort.IsOpen) {
  30. try {
  31. serialPort.PortName = portNameComboBox.SelectedItem.ToString();
  32. serialPort.BaudRate = int.Parse(baudRateComboBox.SelectedItem.ToString());
  33. serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), parityComboBox.SelectedItem.ToString());
  34. serialPort.DataBits = int.Parse(dataBitsComboBox.SelectedItem.ToString());
  35. serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBitsComboBox.SelectedItem.ToString());
  36. serialPort.Open();
  37. connectButton.Text = "断开";
  38. sendButton.Enabled = true;
  39. } catch (Exception ex) {
  40. MessageBox.Show(ex.Message);
  41. }
  42. } else {
  43. serialPort.Close();
  44. connectButton.Text = "连接";
  45. sendButton.Enabled = false;
  46. }
  47. }
  48. private void sendButton_Click(object sender, EventArgs e) {
  49. string text = sendTextBox.Text.Trim();
  50. if (!string.IsNullOrEmpty(text)) {
  51. try {
  52. serialPort.WriteLine(text);
  53. sendTextBox.Clear();
  54. } catch (Exception ex) {
  55. MessageBox.Show(ex.Message);
  56. }
  57. }
  58. }
  59. private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
  60. if (serialPort.IsOpen) {
  61. serialPort.Close();
  62. }
  63. }
  64. private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) {
  65. string text = serialPort.ReadLine();
  66. this.Invoke(new Action(() => {
  67. receiveTextBox.AppendText(text);
  68. }));
  69. }
  70. }
  71. }

在上面的代码中,我们创建了一个名为MainForm的Windows窗体,并在其中添加了以下控件:

  • 一个ComboBox控件用于选择串口号。

  • 一个ComboBox控件用于选择波特率。

  • 一个ComboBox控件用于选择奇偶校验位。

  • 一个ComboBox控件用于选择数据位。

  • 一个ComboBox控件用于选择停止位。

  • 一个Button控件用于刷新可用串口列表。

  • 一个Button控件用于连接/断开串口。

  • 一个TextBox控件用于输入要发送的数据。

  • 一个Button控件用于发送数据。

  • 一个TextBox控件用于显示接收到的数据。

在窗体加载时,我们将可用的波特率、奇偶校验位、数据位和停止位添加到相应的ComboBox控件中,并将可用的串口号添加到portNameComboBox控件中。如果有可用的串口,则默认选中第一个串口。

在连接按钮的Click事件处理程序中,我们首先检查串口是否已经打开。如果串口未打开,则使用选择的端口名称、波特率、奇偶校验位、数据位和停止位打开串口。如果串口已经打开,则关闭串口。

在发送按钮的Click事件处理程序中,我们从sendTextBox控件中获取要发送的文本。如果文本不为空,则向串口写入该文本,并清空sendTextBox控件。

在窗体关闭时,如果串口已经打开,则关闭串口。

最后,在serialPort_DataReceived事件处理程序中,我们从串口读取数据并将其追加到receiveTextBox控件中。由于该事件可能会在不同的线程上触发,因此我们需要在UI线程上执行操作,以避免跨线程访问错误。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/264321
推荐阅读
相关标签
  

闽ICP备14008679号