如何确定使用.NET连接到其他机器的串口名称?
我需要使用 C# 确定连接到其他计算机的串行端口名称。
I need to Determine the serial port name connected to other machine using c#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我需要使用 C# 确定连接到其他计算机的串行端口名称。
I need to Determine the serial port name connected to other machine using c#.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
这不是串行端口的工作方式。它不是像 USB 或 PCI 这样的总线,您可以在其中插入一些东西,然后操作系统就会执行啊哈,新硬件!自动发现。串行端口非常原始,可以追溯到计算机硬件的石器时代。
需要人来插入串行端口设备连接器。如果幸运的话,连接器将有一个标签,上面标明分配给该连接器的 COM 端口号。虽然如今这样的运气已经很难得了。然后,她将告诉程序在该特定 COM 端口上建立连接。超级终端是 Windows 上此类程序的规范实现。
您实际上无法打开所有可能可用的 COM 端口。这可以防止另一个程序使用另一个 COM 端口。例如,您将阻止使用调制解调器。石器时代遗产的一部分是只有一个程序可以打开 COM 端口,所有其他程序都将被锁定。
因此,为您的程序提供一个 UI,让用户选择 COM 端口。将选择保存在配置数据中,设备启动备份时很可能仍连接到同一端口。您可以使用 WMI 和 Win32_SerialPort 类为 COM 端口提供更好的描述(不仅仅是数字)。某些 USB 串行端口仿真器可能会将“描述”属性设置为可识别的内容。
SerialPort.GetPortNames() 枚举可用的 COM 端口号。基本的健全性测试是检查 SerialPort.DsrHolding 属性,当串行端口设备插入并通电时,该属性应该为 true。
This is just not the way serial ports work. It is not a bus, like USB or PCI, where you can plug something in and the operating system will do the ah-ha, new hardware! discovery automatically. Serial ports are very primitive, dating from the stone age of computer hardware.
It takes a human to plug a serial port device connector. With some luck, the connector will have a label which says what COM port number is assigned to the connector. Although that luck is hard to come by these days. She'll then tell a program to establish a connection on that particular COM port. Hyperterminal is the canonical implementation of such a program on Windows.
You cannot realistically open every COM port that might be available. That prevents another program from using another COM port. You'll prevent a modem from getting used for example. Part of the stone age legacy is that only one program can open a COM port, every other program will be locked out.
So, provide your program with a UI that lets the user select the COM port(s). Save the selection in your config data, it is very likely that the device is still connected to the same port when it starts back up. You can use WMI and the Win32_SerialPort class to provide a better description for the COM port (more than just the number). Some USB serial port emulators may set the Description property to something recognizable.
SerialPort.GetPortNames() enumerates the available COM port numbers. A basic sanity test is to check the SerialPort.DsrHolding property, it should be true when the serial port device is plugged in and powered-up.
串行端口不报告任何连接状态。您可以打开计算机上的所有可用串行端口(如果没有其他应用程序已打开它),无论它是否连接到某些设备。
要查明串行端口是否连接到另一台机器,您必须打开所有可用端口,发送初始化数据并监听是否返回正确的数据。
假设您有一个旧的串行调制解调器连接到您的电脑。要找到这一点,您必须打开所有可用端口并通过线路发送“AT”。如果返回“确定”,则表明您找到了调制解调器(可能需要执行其他任务来检查您是否找到了正确的调制解调器[可能有多个设备连接到您的电脑])。
我刚刚错过了:不要忘记配置串行端口!不要仅设置波特率和停止位。将所有设置设置为您需要的值(即使您使用默认设置)。因为如果您关闭并再次重新打开,这些设置也会被保存。所有设置仍然有效,除非您更改它们。现在假设您的电脑上还有其他一些应用程序,它们也打开串行端口并更改某些不常见功能的设置(例如 XOnOff)。如果您没有在初始化阶段将其设置回原处
你将永远无法建立有效的连接!
更新
侦听所有可用端口非常简单:
就是这样。
A serial port doesn't report any connection state. You can open all available serial ports on your computer (if no other application already opened it) regardless if it is connected to something or not.
To find out if a serial port is connected to another machine, you have to open up all the available port, send your initialization data and listen if something correct comes back.
Imagine you have a good old serial modem connected to your pc. To find this out you have to open up all the available ports and send a 'AT' over the wire. If a 'OK' comes back you found a modem (maybe additional tasks are necessary to check if you found the right one [maybe there is more than one device connected to your pc]).
What i just missed out: Don't forget to configure the serial port! Don't set only baudrate and stop bits. Set all settings to the values you need (even if you use default settings). Cause these settings will be saved also if you close and reopen again. All settings are still valid unless you change them. Now imagine you have some other application on your pc that also opens up a serial port and changes the settings for some uncommon feature (e.g. XOnOff). If you don't set it back on your initialization phase
you'll never be able to get a working connection!
Update
Listening to all the available ports is quite easy:
That's it.
串行通信与 IP 相比没有任何区别,IP 具有随每个数据包发送的地址和端口。通过串行电缆发送的唯一数据是您自己发送的字节。
如果您控制两端,您可以将端口号作为您自己的协议的一部分发送。
Serial communication doesn't have anything compared to that of IP which has an address and port sent with every packet. The only data that is sent over a serial cable is the bytes you send yourself.
If you control both ends you can send the port number as a part of your own protocol.