在C#上显示有效的COM端口 [英] display valid com port on C#

查看:99
本文介绍了在C#上显示有效的COM端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我想通过串口连接到硬件。我需要知道哪些是有效的。例如,我刚刚comport3!我想选择这个。我该怎么办?

hi i want to connect via serial port to a hardware. i need to know which comports is valid. for example, i have just comport3! i want to select this. what shall i do?

推荐答案



阅读这篇文章:

http:// msdn。 microsoft.com/en-us/library/system.io.ports.serialport.getportnames(v=vs.110).aspx

但是你必须提供更多关于有效的精确度。 COM端口。

这取决于您的硬件。

一般我这样做:

- 打开COM端口

- 发送消息以获取序列号,例如等待响应。

- 如果反应良好,则检测到硬件

- 必要时关闭COM端口



祝你好运
Hi,
Read this article :
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames(v=vs.110).aspx
But you must give more precision about "Valid" COM port.
This is depending of your hardware.
Generally I do this :
- Open COM port
- send a message to get the serial number for example and wait the response.
- if a good response, hardware is detected
- close COM Port if necessary

Best regards


您可以使用WMI( System.Management ):http://msdn.microsoft.com/en-us/library/system.management%28v=vs.110%29.aspx [ ^ ]。



它可能类似于 http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports [ ^ ]。






You can use WMI (System.Management): http://msdn.microsoft.com/en-us/library/system.management%28v=vs.110%29.aspx[^].

It could be something like http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports[^].



mohsenvasefi写道:
mohsenvasefi wrote:

我需要将这些组合放在组合框上,然后选择其中一个用于连接硬件。

I need to put these comports on a combobox and then select one of them for connect to hardware.

当然。我在这里描述了适当的技术: combobox.selectedvalue在winform中显示{} [< a href =http://www.codeproject.com/Answers/541683/combobox-selectedvalueplusshowsplus-7b-7dplusinplu#answer1target =_ blanktitle =New Window> ^ ]。



这个想法是:您可以将某些类型的实例放在硬件通信所需的所有数据上,而不是仅将COM端口名称放入组合框列表中,通过重写 System.Object.ToString(),使每个列表项显示COM端口名称,如上面引用的代码示例所示。



-SA

Sure. I described appropriate technique here: combobox.selectedvalue shows {} in winform[^].

The idea is: instead of putting just the COM port names to the combo box list, you can put the instances of some type carrying all the data needed for your hardware communications, and make each list item showing COM port names by overriding System.Object.ToString(), as shown in the code sample referenced above.

—SA


如果您使用的是Serial Port类,那么您可以使用以下方法获取可用的组件



If your are using Serial Port class then you can use the below method to get the comports available

using System.IO.Ports; // include the namespace

SerialPort sp=new SerialPort(string portName,int baudRate,Parity parity,int dataBits,StopBits stopBits); //set your device baudrate,parity,stopbits bit to talk with the device.

sp.Open(); // open a serial port connection
string[] arr=Serialport.GetPortNames();
foreach(var item in arr)
{
Combobox1.Items.Add(item); //suppose Combobox1 is yours required combobox name
}
//once you access the comport that hardware connected,do sending a commands or what you want once its done 
sp.Close(); //close the serial port connection





在串口类中,GetPortNames()将获取PC的所有COM端口



GetPortNames - 获取一系列串口名称当前的电脑。



希望这会对你有帮助。



In Serial port class,GetPortNames() will fetch all the com ports of PC

GetPortNames -Gets an array of serial port names for the current computer.

Hope this will help you.


这篇关于在C#上显示有效的COM端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆