C# 串口通信的图形用户界面
我是C和Visual Studio的新手。我在Visual Studio 2013中构建了一个GUI,用于从菜单中选择COM端口。我想知道的是如何将其与串行端口通信连接起来 我应该使用另一个类进行串行通信吗?或者我可以在同一个班上做吗?如何编程C# 串口通信的图形用户界面,c#,visual-studio,user-interface,serial-communication,C#,Visual Studio,User Interface,Serial Communication,我是C和Visual Studio的新手。我在Visual Studio 2013中构建了一个GUI,用于从菜单中选择COM端口。我想知道的是如何将其与串行端口通信连接起来 我应该使用另一个类进行串行通信吗?或者我可以在同一个班上做吗?如何编程 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Sys
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;
}
您可以使用该类从.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”注释,作为如何进一步改进代码的建议,但这是可选的,如果您对此有任何疑问,这应该是一个新问题。非常感谢您。用户从用户界面选择任何端口,该端口用于连接计算机。然后用户单击连接。信息会被发送的。非常感谢。我有一些问题要做。我将把它作为一个新问题发布。拉尔斯,我在这方面又遇到了一个问题。我应该把它作为新问题发布吗?当我选择一个COM端口,然后单击“连接”时,它会显示“找不到COM1”。即使我单击了COM2,它也会显示“COM1”。如果我提供的代码回答了您最初的问题,那么最好询问一个新的端口,并包含您现在拥有的更新代码。顺便说一句,我看到了你的另一个问题-不要以为人们会阅读你的其他问题来了解上下文!几乎没有人会花时间去做那件事