16

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.

Arnaud F.
  • 8,252
  • 11
  • 53
  • 102

5 Answers5

13

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());

        }
2pietjuh2
  • 879
  • 2
  • 13
  • 29
mahendra apte
  • 151
  • 1
  • 2
  • Excellent! Much faster than checking all attached USB devices and then filter through them, thanks! – Starwave Oct 25 '19 at 11:12
11

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.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • I'm in a industrial project, so USB isn't implemented everywhere... :( How would you manage this case? – Arnaud F. Dec 21 '10 at 14:07
  • @Arnaud: Ask the user (probably just once and remember the answer). – Ben Voigt Dec 21 '10 at 14:09
  • @ArnaudF, since there is not standard for this in the RS-232 spec, the best you can hope for is to probe the serial port and hope fully the devices have some kind of identifying response that you can use. If you are lucky the devices might even have a documented command language that you can use to communicate and query the device, but it will be some kind of heuristic depending on the devices in question. Most typically this is handled as part of the setting of an application, where the user is responsible for identifying the devices and the serial port configuration (Baud Rate, Parity etc.) – Chris Taylor Dec 21 '10 at 14:10
  • It seems to be the best solution, you're right. Thanks Chris ! – Arnaud F. Dec 21 '10 at 14:21
0
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.!

Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64
Hamit YILDIRIM
  • 4,224
  • 1
  • 32
  • 35
0
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";
            }

        }
}
0

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
            {

            }
        }
    }
    
Mnyikka
  • 1,223
  • 17
  • 12