Arnaud F.
Arnaud F.

Reputation: 8452

Get the device name connected to the serial port

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.

Upvotes: 16

Views: 26297

Answers (5)

Mnyikka
Mnyikka

Reputation: 1261

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
            {

            }
        }
    }
    

Upvotes: 0

Tathagata Mukherjee
Tathagata Mukherjee

Reputation: 1

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";
            }

        }
}

Upvotes: 0

Hamit YILDIRIM
Hamit YILDIRIM

Reputation: 4539

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.!

Upvotes: 0

mahendra apte
mahendra apte

Reputation: 161

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

        }

Upvotes: 14

Chris Taylor
Chris Taylor

Reputation: 53699

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.

Upvotes: 11

Related Questions