本文介绍了查找 Arduino 连接的端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何列出可用的串行端口,但是每次连接我的 Arduino 时,我如何才能找到正确的 COM 端口?COM 端口应该像 MessageBox.Show(COMport);

I know how to list the available Serial Ports but whow can I find the right COM port everytime I connect my Arduino? COM Port should be printed like MessageBox.Show(COMport);

我想在 Visual C# 应用程序中读/写 Arduino 数据.

I want to read/write Arduino data in a Visual C# application.

[这个对我不起作用]

推荐答案

打开设备管理器,展开端口(COM & LPT)".插入 Arduino USB 连接,一个新的 COM 端口出现,名称为 Arduino UNO (COMxx).这是在我的机器上,因为我有一个 Arduino UNO.

Open Device manager, expand "Ports (COM & LPT)". Plug in the Arduino USB connection, a new COM port shows up with name Arduino UNO (COMxx). This is on my machine as I have an Arduino UNO.

您可以使用 WMI(Windows 管理规范)找到此字符串.我在一个类中使用下面的方法,并将 COMports 作为公共列表

You can find this string using WMI (Windows Management Instrumentation). I am using the method below in a class, and has COMports as a public List

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Windows;

 public void getCOMportsValues()
    {
        try
        {
            if (COMports.Count > 0) COMports.Clear(); // COMports is a List<string>

            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                string s = queryObj["Name"] as string;
                if (s.Contains("(COM"))
                    COMports.Add(s);
            }
        }
        catch (ManagementException e)
        {
            MessageBox.Show("An error occurred while querying WMI data: " + e.Message);
        }
    }

这篇关于查找 Arduino 连接的端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 01:20