简体   繁体   English

自动调整C#中的COM串行端口

[英]Automatically adjust COM serial port in C#

I've got Arduino Nano controlling a DC motor connected to my PC and controlled over C#. 我已经用Arduino Nano控制连接到PC并通过C#控制的直流电动机。 The only problem is that it works on my computer at the moment, and if I connect it to another PC it won't work unless it uses the same serial port. 唯一的问题是,此刻它现在可以在我的计算机上运行,​​如果我将其连接到另一台PC,除非它使用相同的串行端口,否则它将无法工作。 That's why I want the COM port to "set itself". 这就是为什么我要COM端口“自行设置”。 Is it possible to do? 有可能吗? If not, I wanted to make another Form just for entering the number of COM port, but I want to avoid that if possible. 如果没有,我想制作另一个表格只是为了输入COM端口号,但是如果可能的话,我想避免这种情况。 Thank you in advance. 先感谢您。 This is my code: 这是我的代码:

public partial class Form1 : Form
{
    String s = "0";
    string brojPorta = "COM5";
    int vrijednost = 0;
    System.IO.Ports.SerialPort serialPort1;
    public Form1()
    {
        InitializeComponent();
        System.ComponentModel.IContainer components =
    new System.ComponentModel.Container();
        serialPort1 = new System.IO.Ports.SerialPort(components);
        serialPort1.PortName = brojPorta;
        serialPort1.BaudRate = 9600;
        serialPort1.Open();
    }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (serialPort1.IsOpen) serialPort1.Close();
    }

    private void Klizac1_Scroll(object sender, ScrollEventArgs e)
    {
        vrijednost = Klizac1.Value;
        s = (vrijednost * 10.24).ToString();
        serialPort1.Write(s + '\n');
        label1.Text = ((vrijednost-50)*2).ToString()+"%";
    }

    private void btn_Zaustavi_Click(object sender, EventArgs e)
    {
        Klizac1.Value = 50;
        label1.Text = "0";
        s = (Klizac1.Value * 10.24).ToString();
        serialPort1.Write(s + '\n');
    }
}

First you would have to enumerate all the ports. 首先,您必须枚举所有端口。 See this question: How to find available COM ports? 看到这个问题: 如何找到可用的COM端口?

Then you would have to attempt to connect on each port with a timeout until you find it. 然后,您将不得不尝试在每个端口上连接超时,直到找到为止。

The more prudent scenario is to enumerate the available ports in a drop down list and have the user select the port it's connected to. 更为谨慎的情况是在下拉列表中枚举可用端口,并让用户选择其连接的端口。

There could be some pitfalls here but this example appears to work: 这里可能会有一些陷阱,但是此示例似乎有效:

/*Use the WMI to search for the Arduino device on a serial port driver
and assign the serial port to the device*/

ManagementObjectSearcher SerialPortSearcher =
    new ManagementObjectSearcher(
    "root\\CIMV2",
    "SELECT * FROM Win32_SerialPort");

foreach (ManagementObject SerialPortObject in SerialPortSearcher.Get())
{
    if (SerialPortObject["Description"].ToString() == "Arduino Mega 2560")
    {
        SerialPort _serialPort =
            new SerialPort(SerialPortObject["DeviceID"].ToString());
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM