1
votes

I have written a program in C# that allows me to communicate (using AT Commands) with the serial ports inside of our computers. The code to find the ports looks like this:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2",
                "SELECT * FROM Win32_PnPEntity");

            //This loops through the results from the searcher
            foreach (ManagementObject queryObj in searcher.Get())
            {
                //If it finds the port, 
                if (queryObj["Caption"].ToString().Contains("##### Wireless AT"))
                {
                    //it writes it to the file
                    sw.WriteLine("serial port : {0}", queryObj["Caption"] + "\n");
                    sw.Flush();
                }

This code works splendidly with our older modems, it searches through the COM ports and finds the AT Wireless Command port. This is the port which I eventually send my AT commands to. Here are two pictures of the device manager of the ports I am searching for

XP

Windows 7

The issue is, we are rolling out our computers with newer modems, and these work differently...

The new modems do not use serial ports with physical listing of ports in the device manager. Also, the serial port does not show up in the Win32_PnpEntity search... The serial port is listed under the modem properties.

The New Modem

My question is, how do I find the serial port of the modem using C#?

Please let me know if there is some way I can elaborate.

-Luke

2

2 Answers

1
votes

So, I figured out how to solve my issue. Thank you Marc for your answer, but it just wasn't working out for me.

I ended up following the instructions contained in this Stack Overflow post: List all System Modems

Here is the code that worked:

using System.IO.Ports;
using System.Management;
using System;
using System.Collections.Generic;
using System.Linq;
using ROOT.CIMV2.Win32;

namespace Modem
{
    class Program
    {

    public static string portName;
    public static SerialPort _serialPort;

    static void Main(string[] args)
    {

        foreach (POTSModem modem in POTSModem.GetInstances())
        {
            if (modem.Description.Contains("WWAN"))
            {
                portName = modem.AttachedTo;

                SetPort();

                break;
            }
        }

     }


 public static void SetPort()
    {
        //Assign the port to a COM address and a Baud rate
        _serialPort = new SerialPort(portName, 9600);

        //Open the connection
        _serialPort.Open();

        // Makes sure serial port is open before trying to write
        try
        {
            //If the port is not open
            if (!(_serialPort.IsOpen))
                //Open it
                _serialPort.Open();

        }

        catch
        {
            Console.WriteLine("ERROR! Couldn't open serial port...");
            Console.ReadKey();
        }

        try
        {
            //Here it executes a command on the modem
            _serialPort.Write("ATI\r");


            //Retrieves the output, setting the value to a string
            string indata = _serialPort.ReadExisting();

            Console.WriteLine(indata);
        }

        catch
        {
            Console.WriteLine("ERROR GETTING INFO!!!");
            Console.ReadKey();
        }

     }
  }
}

It works like a charm! Now I just need to figure out all the new AT commands I'm going to have to use with the new modems. ;)

Thanks for your help, Luke

0
votes

Luke,

You will have to change up how you Query the Management tree.

Using System.IO.Ports;
Using System.Managment;
Using System;
private void SetPort()
    {
        string[] allPorts = SerialPort.GetPortNames();
        bool found = false;
        SerialPort port;

        for (int i = 0; i < allPorts.Length; i++)
        {
            var searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
            foreach (ManagementObject queryObj in searcher.Get())
            {
                string instanceName = queryObj["InstanceName"].ToString();

                if (instanceName.IndexOf("Your Modem String", StringComparison.Ordinal) > -1)
                {
                    string portName = queryObj["PortName"].ToString();
                    port = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One);
                    found = true;
                    break;
                }
            }

            if (found) break;
        }
    }

In the loop, the code creates a serial port to which your AT commands could be sent. Hope this helps.

Regards, Marc