获取串口连接的设备名称

发布于 2024-10-09 05:58:00 字数 144 浏览 0 评论 0原文

我搜索如何获取连接到串口的材料的设备名称。

我有两种不同类型的材料可以在上面连接。

第一个:打印机(只接收数据,不发回任何数据) 第二个:余额(只发送数据,不接收任何内容)

我如何区分这两种类型的材料?

谢谢。

I search how to get the device name of the material connected to the serial port.

I've two different types of material that can connect on it.

First one : a printer (only receives data and send nothing back)
Second one : a balance (only send data and receives nothing)

How can I distinguish this two types of material?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

追我者格杀勿论 2024-10-16 05:58:00

试试这个:

        ManagementObjectCollection ManObjReturn;
        ManagementObjectSearcher ManObjSearch;
        ManObjSearch = new ManagementObjectSearcher("Select * from Win32_SerialPort");
        ManObjReturn = ManObjSearch.Get();

        foreach (ManagementObject ManObj in ManObjReturn)
        {
            //int s = ManObj.Properties.Count;
            //foreach (PropertyData d in ManObj.Properties)
            //{
            //    MessageBox.Show(d.Name);
            //}
            MessageBox.Show(ManObj["DeviceID"].ToString());
            MessageBox.Show(ManObj["PNPDeviceID"].ToString());
               MessageBox.Show(ManObj["Name"].ToString());
               MessageBox.Show(ManObj["Caption"].ToString());
               MessageBox.Show(ManObj["Description"].ToString());
               MessageBox.Show(ManObj["ProviderType"].ToString());
               MessageBox.Show(ManObj["Status"].ToString());

        }

try this:

        ManagementObjectCollection ManObjReturn;
        ManagementObjectSearcher ManObjSearch;
        ManObjSearch = new ManagementObjectSearcher("Select * from Win32_SerialPort");
        ManObjReturn = ManObjSearch.Get();

        foreach (ManagementObject ManObj in ManObjReturn)
        {
            //int s = ManObj.Properties.Count;
            //foreach (PropertyData d in ManObj.Properties)
            //{
            //    MessageBox.Show(d.Name);
            //}
            MessageBox.Show(ManObj["DeviceID"].ToString());
            MessageBox.Show(ManObj["PNPDeviceID"].ToString());
               MessageBox.Show(ManObj["Name"].ToString());
               MessageBox.Show(ManObj["Caption"].ToString());
               MessageBox.Show(ManObj["Description"].ToString());
               MessageBox.Show(ManObj["ProviderType"].ToString());
               MessageBox.Show(ManObj["Status"].ToString());

        }
花桑 2024-10-16 05:58:00

没有识别串行端口 (UART RS232) 设备的通用方法。

除非设备有特殊命令,您可以发送到设备并让它响应识别信息,否则您无能为力。

通常,依赖于串行端口的应用程序会有一个标准设置屏幕,用户可以使用该屏幕来配置设备连接到的串行端口、波特率、奇偶校验位、停止位和数据位等端口配置。如果可以在同一端口上切换多个设备,则操作员将负责在与目标设备通信之前为目标设备选择正确的配置。

这是 USB 等新技术的优势,其中设备识别已内置于标准中。

There is no univeral way of identifying serial port (UART RS232) devices.

Unless the devices have special commands that you can send to the device and have it respond with identifying information there is not much you can do.

Typically application that rely on the serial port will have a standard setting screen that the user would use to configure the serial port the device is connected to, port configuration for things like baud rate, parity bits, stop bits and data bits. If mutiple devices can be switched on the same port, the operator would then be responsible for selecting the correct configuration for the target device before communicating with the device.

This is the advantage of newer technologies like USB etc. where device identification is built into the standard.

雨后咖啡店 2024-10-16 05:58:00
static void Main(string[] args)
{
    ManagementObjectCollection ManObjReturn;
    ManagementObjectSearcher ManObjSearch;
    ManObjSearch = new ManagementObjectSearcher("Select * from **Win32_ParallelPort**");
    ManObjReturn = ManObjSearch.Get();

    foreach (ManagementObject ManObj in ManObjReturn)
    {
        //int s = ManObj.Properties.Count;
        //foreach (PropertyData d in ManObj.Properties)
        //{
        //    MessageBox.Show(d.Name);
        //}
        Console.WriteLine(ManObj["DeviceID"].ToString());
        Console.WriteLine(ManObj["PNPDeviceID"].ToString());
        Console.WriteLine(ManObj["Name"].ToString());
        Console.WriteLine(ManObj["Caption"].ToString());
        Console.WriteLine(ManObj["Description"].ToString());
        Console.WriteLine(ManObj["ProviderType"].ToString());
        Console.WriteLine(ManObj["Status"].ToString());

    }

}

http ://www.seeques.com/20766280/the-port-name-is-illegal-or-couldnt-be-connected-to-the-device.html

端口名称非法 错误消息如何就这样……菲奥.!

static void Main(string[] args)
{
    ManagementObjectCollection ManObjReturn;
    ManagementObjectSearcher ManObjSearch;
    ManObjSearch = new ManagementObjectSearcher("Select * from **Win32_ParallelPort**");
    ManObjReturn = ManObjSearch.Get();

    foreach (ManagementObject ManObj in ManObjReturn)
    {
        //int s = ManObj.Properties.Count;
        //foreach (PropertyData d in ManObj.Properties)
        //{
        //    MessageBox.Show(d.Name);
        //}
        Console.WriteLine(ManObj["DeviceID"].ToString());
        Console.WriteLine(ManObj["PNPDeviceID"].ToString());
        Console.WriteLine(ManObj["Name"].ToString());
        Console.WriteLine(ManObj["Caption"].ToString());
        Console.WriteLine(ManObj["Description"].ToString());
        Console.WriteLine(ManObj["ProviderType"].ToString());
        Console.WriteLine(ManObj["Status"].ToString());

    }

}

http://www.seeques.com/20766280/the-port-name-is-illegal-or-couldnt-be-connected-to-the-device.html

the port name is illegal how is an error message like that...fio.!

∞觅青森が 2024-10-16 05:58:00
Class1 UD = new Class1();
{
string strUserAgent = Request.UserAgent.ToLower();
        if (strUserAgent != null)
        {
            string Browser = Request.Browser.Browser;
            string a = Request.Browser.MobileDeviceManufacturer;
            string b = Request.Browser.MobileDeviceModel;
            string c = Request.Browser.Platform;
            string d = Request.Browser.Type;
            string e = Request.Browser.Version;

            UD.Browser = Browser;
            UD.MobileDeviceModel = b;
            UD.MobileDeviceManufacturer = a; 
            UD.Platform2 = c;
            UD.Type = d;
            UD.Version2 = e; 




            if (Request.Browser.IsMobileDevice == true || strUserAgent.Contains("iphone") ||
                     strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") ||
                     strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") ||
                     strUserAgent.Contains("palm"))
            {
                UD.deviceType = "Request from Mobile Device";
            }
            else
            {
                UD.deviceType = "Request from Computer";
            }

        }
}
Class1 UD = new Class1();
{
string strUserAgent = Request.UserAgent.ToLower();
        if (strUserAgent != null)
        {
            string Browser = Request.Browser.Browser;
            string a = Request.Browser.MobileDeviceManufacturer;
            string b = Request.Browser.MobileDeviceModel;
            string c = Request.Browser.Platform;
            string d = Request.Browser.Type;
            string e = Request.Browser.Version;

            UD.Browser = Browser;
            UD.MobileDeviceModel = b;
            UD.MobileDeviceManufacturer = a; 
            UD.Platform2 = c;
            UD.Type = d;
            UD.Version2 = e; 




            if (Request.Browser.IsMobileDevice == true || strUserAgent.Contains("iphone") ||
                     strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") ||
                     strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") ||
                     strUserAgent.Contains("palm"))
            {
                UD.deviceType = "Request from Mobile Device";
            }
            else
            {
                UD.deviceType = "Request from Computer";
            }

        }
}
谎言 2024-10-16 05:58:00

这在 win11 上不起作用,有时在 win10 上不起作用,请尝试这个,使用带有参数 hwids =ports 的 devcon.exe

    private void RetrieveDeviceInfo()
           {
        Process proc1 = null;
        try
        {
            proc1 = new Process();
            proc1.StartInfo = new ProcessStartInfo();
            proc1.StartInfo.FileName = this.DevConExeFile;
            proc1.StartInfo.Arguments = "hwids =ports";
            //proc1.StartInfo.RedirectStandardInput = true;
            proc1.StartInfo.RedirectStandardOutput = true;
            proc1.StartInfo.UseShellExecute = false;
            proc1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc1.StartInfo.CreateNoWindow = true;
            proc1.Start();
            proc1.WaitForExit(1000);
            string AllOutput = proc1.StandardOutput.ReadToEnd();
            this.LastOutput = AllOutput;
        }
        catch(Exception em1)
        {
            throw em1;
        }
        finally
        {
            try
            {
                proc1.Kill();
            }
            catch
            {

            }
        }
    }
    

This wont work on win11, and win10 sometimes, Try this instead, use devcon.exe with the argument hwids =ports

    private void RetrieveDeviceInfo()
           {
        Process proc1 = null;
        try
        {
            proc1 = new Process();
            proc1.StartInfo = new ProcessStartInfo();
            proc1.StartInfo.FileName = this.DevConExeFile;
            proc1.StartInfo.Arguments = "hwids =ports";
            //proc1.StartInfo.RedirectStandardInput = true;
            proc1.StartInfo.RedirectStandardOutput = true;
            proc1.StartInfo.UseShellExecute = false;
            proc1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc1.StartInfo.CreateNoWindow = true;
            proc1.Start();
            proc1.WaitForExit(1000);
            string AllOutput = proc1.StandardOutput.ReadToEnd();
            this.LastOutput = AllOutput;
        }
        catch(Exception em1)
        {
            throw em1;
        }
        finally
        {
            try
            {
                proc1.Kill();
            }
            catch
            {

            }
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文