1

我已经设法在视觉 C# express 中识别驱动器和串行端口,但我仍然无法访问特定设备(RepRap 打印机)。我想向它发送一个字符串数组,但首先我需要找到它,我该怎么做?我正在使用 Windows 7。

要获取驱动器:

using System.Linq;
using System.IO;
using System;

    class Program
    {
            static void Main(string[] args)
            {
                var drives = DriveInfo.GetDrives();
               DriveInfo[] allDrives = DriveInfo.GetDrives();
                     foreach(DriveInfo dv in drives)          
                     {              
                            Console.WriteLine("drive Name:{0}", dv.Name);      
                     }    
                    Console.ReadLine();
            }
        }

获取串口:

using System;
using System.IO.Ports;

namespace SerialPortExample
{
    class SerialPortExample
    {
        public static void Main()
        {
            string[] ports = SerialPort.GetPortNames();
            Console.WriteLine("The following serial ports were found:");
            foreach (string port in ports)
            {
                Console.WriteLine(port);
            }
            Console.ReadLine();
        }
    }
}

提前谢谢了!

4

1 回答 1

1

我鼓励您首先检查这两个问题和答案:

获取连接到串行端口的设备名称
这个简短地解释了为什么它很难,但提供了一些关于如何询问 Windows 它对设备了解多少的线索

获取串行端口信息
这里有一堆进一步的代码示例。

Generally, you will probably want to find the device by it's NAME that the system assigned to it - you probalby know the name and its something like "reprap#1" etc. I just guess. It may be a good idea to ask to scan all COM-device names and display it to the user so he may choose the proper one..

if you want to automaticaly detect, you can try to detect them by some lower-level details like drivername etc, but usually it is better to just leave that to the user.

于 2012-08-14T20:32:35.550 回答