确定(USB启用)客户显示器的COM端口 [英] Determining the COM port of a (USB enabled) custumer display
问题描述
I have connected customer Display with POS application. I am using serial port class for display message on customer Display. To message i need to know with com port this USB display are using. I have searched allowed there are many examples for getting all com pot for that cumputer but i am able to find any help to get perticular com port no e.g COM93,COM01 or COM2.
I have tried following program and also i have tried Microsoft program WMI code creator.
<pre lang="c#">// Get a list of serial port names. string[] ports = SerialPort.GetPortNames();
Console.WriteLine("The following serial ports were found:");
// Display each port name to the console.
foreach (string port in ports)
{
Console.WriteLine(port);
}
Console.ReadLine();
SerialPort sp = new SerialPort();
sp.PortName = "COM93";------------- How i can find this no automatic
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.DataBits = 8;
sp.StopBits = StopBits.One;
sp.Open();
sp.Write("\f");
sp.WriteLine("***Velkommen***");
sp.Close();
sp.Dispose();
sp = null;
我也尝试了以下解决方案,为此我也使用了这个访问被拒绝所以我需要编辑registory给予权利我点wana我想以编程方式获得解决方案
I have tried following solution as well, to use this as well for this I get access denied so i need to edit registory to give rights that i dot wana do i want solution programatically
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\WMI", "SELECT * FROM MSSerial_PortName");
我尝试过:
What I have tried:
<pre lang="c#">ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\WMI", "SELECT * FROM MSSerial_PortName");
推荐答案
您可以使用SerialPort.GetPortNames( )
填充列表框,让用户选择一个端口,并将其存储为用户设置,以便在下一个程序启动时重复使用。但这需要用户知道哪个端口是您的显示设备。
为了帮助用户识别设备,您还可以提供Windows设备管理器中显示的说明或者使用描述自动选择端口。
但.NET不提供描述,因此您必须调用Windows API函数或使用WMI(需要管理权限)。
一个例子可以在枚举所有COM端口并在C#中查找其名称和描述Svetlin Nakov的博客 [ ^ ]。
You can useSerialPort.GetPortNames()
to populate a list box, let the user select a port, and store that as user settings to be re-used at next program start. But this requires that the user knows which port is your display device.
To help the user identifying the device you can also provide the description as shown in the Windows Device Manager or use the description to select the port automatically.
But getting the description is not provided by .NET so that you have to call Windows API functions or use WMI (which requires administrative privileges).
An example can be found at Enumerate All COM Ports and Find Their Name and Description in C# | Svetlin Nakov's Blog[^].
比我想象的要容易......
It was easier than I thought ...
Imports Microsoft.Win32
Imports System.Text.RegularExpressions
Public Class ReaderInfo
Public DriverExists As Boolean
Public PortName As String
Public PortExists As Boolean
Public PortOpened As Boolean
Public VendorID As String
Public ProduktID As String
Public DriverID As String
Public Description As String
Public Typ_Converter As String
Public Typ_Reader As String
Sub Clear()
DriverExists = False
PortName = ""
PortExists = False
PortOpened = False
VendorID = ""
ProduktID = ""
Description = ""
Typ_Converter = ""
Typ_Reader = "Kaba B-Net 9107 Card-Reader"
End Sub
Sub New()
Clear()
End Sub
Public Sub Get_RegistryInfo(VID As String, PID As String)
Dim rk1 As RegistryKey = Registry.LocalMachine ' HKEY_LOCAL_MACHINE
Dim rk2 As RegistryKey = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum") ' HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum
Clear()
VendorID = VID
ProduktID = PID
Dim pattern As String = String.Format("^VID_{0}.PID_{1}", VID, PID)
Dim _rx As Regex = New Regex(pattern, RegexOptions.IgnoreCase)
Dim rk2_SubKeyNames, rk3_SubKeyNames, rk4_SubKeyNames As String
Dim Desc, Mfg As String
Dim Index As Integer
For Each rk2_SubKeyNames In rk2.GetSubKeyNames()
Dim rk3 As RegistryKey = rk2.OpenSubKey(rk2_SubKeyNames)
For Each rk3_SubKeyNames In rk3.GetSubKeyNames()
If _rx.Match(rk3_SubKeyNames).Success Then
Dim rk4 As RegistryKey = rk3.OpenSubKey(rk3_SubKeyNames)
DriverExists = True
For Each rk4_SubKeyNames In rk4.GetSubKeyNames()
Index = CInt(rk4_SubKeyNames)
Dim rk5 As RegistryKey = rk4.OpenSubKey(rk4_SubKeyNames)
If rk5.OpenSubKey("Control") IsNot Nothing Then
Dim rk6 As RegistryKey = rk5.OpenSubKey("Device Parameters")
Dim rk5_DeviceDesc As String() = rk5.GetValue("DeviceDesc").ToString.Split(";")
Desc = rk5_DeviceDesc(rk5_DeviceDesc.Length - 1)
If Index = 0 Then
Description = Desc
PortName = rk6.GetValue("PortName")
Dim myComports As String() = System.IO.Ports.SerialPort.GetPortNames()
For i As Integer = 1 To myComports.Length
If myComports(i - 1) = PortName Then
PortExists = True
Exit For
End If
Next
Else ' Index <> 0 // ? > 200
Dim rk5_Mfg As String() = rk5.GetValue("Mfg").ToString.Split(";")
Mfg = rk5_Mfg(rk5_Mfg.Length - 1)
Typ_Converter = Mfg + vbCrLf + Desc
DriverID = Index.ToString
End If
End If
Next
End If
Next
Next
End Sub
End Class
当然......样本是VB - 但我想你可以看到它是如何工作的。我应该将其转换为C#吗?
唯一要知道的是Vendor-ID和Product-ID。使用此信息,您可以调用方法Get_RegistryInfo
我希望,这有帮助 - 否则请随时与我联系...
of course ... the sample is VB - but I think you could see how it works. Should I convert it to C# ?
The only Thing is to know the Vendor-ID and the Product-ID. With this Information you call the Method "Get_RegistryInfo"
I hope, it helps - otherwise feel free to contact me again ...
这篇关于确定(USB启用)客户显示器的COM端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!