简体   繁体   English

查找COM端口

[英]Finding COM ports

i write a code(vs 2003,ver 1.1) for listing all the com ports available in a system.Now i want to know which of these com ports are connected to a device,and which one is available. 我编写了一个代码(vs 2003,版本1.1)来列出系统中所有可用的com端口。现在,我想知道这些com端口中的哪些已连接到设备,以及哪个可用。

i wrote a dll in vc++ and done it. 我用vc ++写了一个dll并完成了。 thnx n

This is going to be a tough one. 这将是一个艰难的过程。

Serial communications are just send/receive wires, they aren't negotiated connections like TCP. 串行通信只是发送/接收线路,它们不是像TCP这样的协商连接。

You can open a connection (using System.IO.Ports) to a port that has no connected device. 您可以打开到没有连接设备的端口的连接(使用System.IO.Ports)。

You can also open a connection and write to it all day using different baud rates, etc., and you may never get a response back, even if there is a connected device. 您还可以打开连接并使用不同的波特率等全天写入该连接,即使有已连接的设备,也可能永远都不会收到响应。

All that said, you can get a list of serial ports (not including USB ports) on the machine using the code below, but it will tell you nothing about whether or not there is a device connected to them: 综上所述,您可以使用以下代码在计算机上获取串行端口列表 (不包括USB端口),但不会告诉您是否有设备与其连接:

foreach (string s in SerialPort.GetPortNames()) {
    Console.WriteLine("{0}", s);
    }

Try opening each port. 尝试打开每个端口。 If you can't open it (or an exception is thrown) the port is not available. 如果您无法打开它(或引发异常),则该端口不可用。 If you can open it, it is available. 如果可以打开它,则可用。

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

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