带有串行通信的GUI

本文关键字:GUI 通信 | 更新日期: 2023-09-27 18:17:40

我是c#和Visual Studio的新手。我在Visual Studio 2013中构建了一个GUI来从菜单条中选择COM端口。我想知道的是如何将其与串口通信连接。

我应该使用另一个类串行通信吗?或者我可以在同一节课上做吗?它是如何被编程的?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace exo_new
{
public partial class rehab : Form
{
    public rehab()
    {
        InitializeComponent();
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    }
    private void conndToolStripMenuItem_Click(object sender, EventArgs e)
    {
    }
    private void cOM1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        cOM1ToolStripMenuItem.Checked = true;
        cOM2ToolStripMenuItem.Checked = false;
        cOM3ToolStripMenuItem.Checked = false;
        cOM4ToolStripMenuItem.Checked = false;
        cOM5ToolStripMenuItem.Checked = false;
        cOM6ToolStripMenuItem.Checked = false;
        cOM7ToolStripMenuItem.Checked = false;
        cOM8ToolStripMenuItem.Checked = false;
        cOM9ToolStripMenuItem.Checked = false;
        cOM10ToolStripMenuItem.Checked = false;
    }
    private void cOM2ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        cOM2ToolStripMenuItem.Checked = true;
        cOM1ToolStripMenuItem.Checked = false;
        cOM3ToolStripMenuItem.Checked = false;
        cOM4ToolStripMenuItem.Checked = false;
        cOM5ToolStripMenuItem.Checked = false;
        cOM6ToolStripMenuItem.Checked = false;
        cOM7ToolStripMenuItem.Checked = false;
        cOM8ToolStripMenuItem.Checked = false;
        cOM9ToolStripMenuItem.Checked = false;
        cOM10ToolStripMenuItem.Checked = false;
    }

带有串行通信的GUI

您可以使用System.IO.SerialPort类从。net应用程序编程串行端口。(链接到该类的MSDN文档,其中包括一个示例程序。)如果你需要更多的帮助,你需要提供一些你的代码和更多的解释你想做什么,什么时候做。(例如,用户单击按钮X,而您想发送消息Y…)

UPDATE:感谢分享你的代码。以下是我将如何基于您开始的内容实现一个简单的解决方案:

public partial class rehab : Form
{
    private string portName = "COM1";
    private const int baudRate = 9600;
    public Form1()
    {
        InitializeComponent();
        //TODO: Simplify your UI by dynamically creating the COM port names.
        //      Get the list of available ports on the computer via the following:
        //var portNames = SerialPort.GetPortNames();
        // Call this to initially mark 'COM1' as checked.
        UpdatePortCheckmarks();
    }
    private void conndToolStripMenuItem_Click(object sender, EventArgs e)
    {
        var textToSend = this.textBox1.Text;
        // Use a try-catch block to log any exceptions that occur.
        try
        {
            // Use a using block to close and dispose of the serial port
            // resource automatically. Also, note that the SerialPort
            // constructor takes the port name and baud rate here.
            // There are also overloads that let you pass the number of
            // data bits, parity, and stop bits, if needed.
            using (var serialPort = new SerialPort(portName, baudRate))
            {
                // Open the port before writing to it.
                serialPort.Open();
                // Send the content of the textbox (with a newline afterwards).
                serialPort.WriteLine(textToSend);
            }
        }
        catch (Exception ex)
        {
            // You could also use MessageBox.Show. Console.WriteLine will
            // display errors in your debugger's output window.
            Console.WriteLine("ERROR: " + ex.ToString());
        }
    }
    private void cOM1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        portName = "COM1";
        UpdatePortCheckmarks();
    }
    private void cOM2ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        portName = "COM2";
        UpdatePortCheckmarks();
    }
    // .. and so on for each additional port menu item (COM3 through COM10)
    // This method lets you share the code for updating the checkmarks on
    // the menu items, so your form code will be cleaner.
    private void UpdatePortCheckmarks()
    {
        cOM1ToolStripMenuItem.Checked = portName == "COM1";
        cOM2ToolStripMenuItem.Checked = portName == "COM2";
        cOM3ToolStripMenuItem.Checked = portName == "COM3";
        cOM4ToolStripMenuItem.Checked = portName == "COM4";
        cOM5ToolStripMenuItem.Checked = portName == "COM5";
        cOM6ToolStripMenuItem.Checked = portName == "COM6";
        cOM7ToolStripMenuItem.Checked = portName == "COM7";
        cOM8ToolStripMenuItem.Checked = portName == "COM8";
        cOM9ToolStripMenuItem.Checked = portName == "COM9";
        cOM10ToolStripMenuItem.Checked = portName == "COM10";
    }
}

我已经包含了一个'TODO'注释,作为你如何进一步改进你的代码的建议,但这是可选的(如果你对此有任何疑问,应该是一个新的问题)。