我已经将客户显示器与POS应用程序连接起来。我正在使用串行端口类在客户显示器上显示消息。要发送消息,我需要知道此USB显示器正在使用的com端口。我已经搜索了很多示例来获取该计算机的所有计算机,但我能够找到任何帮助来获取特定的 com 端口号,例如 COM93、COM01 或 COM2。
我已经尝试了以下程序,也尝试了Microsoft程序WMI代码创建器。
// 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;
我也尝试了以下解决方案,为此也使用它,我被拒绝访问,所以我需要编辑注册表以授予我想要以编程方式解决
的权限ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\WMI", "SELECT * FROM MSSerial_PortName");
public string Get_RegistryInfo(string VID, string PID)
{
try
{
RegistryKey rk1 = Registry.LocalMachine;
// HKEY_LOCAL_MACHINE
RegistryKey rk2 = rk1.OpenSubKey("HARDWARE\\DEVICEMAP\\SERIALCOMM");
// HKEY_LOCAL_MACHINEHARDWARE\\DEVICEMAP\\SERIALCOMM
VendorID = VID;
ProduktID = PID;
string pattern = string.Format("^VID_{0}.PID_{1}", VID, PID);
Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
string rk2_SubKeyNames = null;
foreach (string rk2_SubKeyNames_loopVariable in rk2.GetValueNames())
{
rk2_SubKeyNames = rk2_SubKeyNames_loopVariable;
if (rk2_SubKeyNames == "\Device\ProlificSerial0")
{
COM_Port = rk2.GetValue(rk2_SubKeyNames).ToString();
}
}
return COM_Port;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return COM_Port;
}
}