GUI与串行通信C#

时间:2014-08-13 04:09:31

标签: c# visual-studio user-interface serial-communication

我是C#和Visual Studio的新手。我已经构建了一个GUI来从Visual Studio 2013中的MenuStrip中选择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;
    }

1 个答案:

答案 0 :(得分:2)

您可以使用System.IO.SerialPort类从.NET应用程序对串行端口进行编程。 (该链接指向该类的MSDN文档,其中包含一个示例程序。)如果您需要更多帮助,则需要提供一些代码,并提供有关您要执行的操作的更多说明,以及何时。 (例如,用户单击按钮X,您想发送消息Y ...)

更新:感谢您分享目前的代码。以下是我将如何基于您的开始实现一个简单的解决方案:

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”评论作为关于如何进一步改进代码的建议,但这是可选的(如果您对此有任何疑问,应该是一个新问题。)