Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# AT Commands serialport
Hello!
 
I have problem with my program. I want my program to automatically search for the port which is connected to the phone. Now I have to manually enter the COM port. I already have the code to check the port. My question is how can I do to make my program automatically search for the COM port to which the phone is connected.
Program:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
 
namespace telefon
{
    class Program
    {
 
        static void Main(string[] args)
        {
            SerialPort portConfig = new SerialPort();
            portConfig.PortName = "COM4";
            portConfig.BaudRate = 2400;
            portConfig.Parity = Parity.None;
            portConfig.DataBits = 8;
            portConfig.StopBits = StopBits.One;
            portConfig.Handshake = Handshake.XOnXOff;
            portConfig.Encoding = Encoding.ASCII;
 

            portConfig.Open();
            portConfig.NewLine = "\r\n";
 

            portConfig.WriteLine("ATD 123456789;");
           
 
            Console.ReadLine();
        }
    }
}
    
 
program to test com ports activity:
using System;
using System.IO.Ports;
using System.Threading;
 
public class PortChat
{
    static bool _continue;
    static SerialPort _serialPort;
 
    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);
 
        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();
 
        // Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName);
        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
        _serialPort.Parity = SetPortParity(_serialPort.Parity);
        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);
 
        // Set the read/write timeouts
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;
 
        _serialPort.Open();
        _continue = true;
        readThread.Start();
 
        Console.Write("Name: ");
        name = Console.ReadLine();
 
        Console.WriteLine("Type QUIT to exit");
 
        while (_continue)
        {
            message = Console.ReadLine();
 
            if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort.WriteLine(
                    String.Format("<{0}>: {1}", name, message));
            }
        }
 
        readThread.Join();
        _serialPort.Close();
    }
 
    public static void Read()
    {
        while (_continue)
        {
            try
            {
                string message = _serialPort.ReadLine();
                Console.WriteLine(message);
            }
            catch (TimeoutException) { }
        }
    }
 
    public static string SetPortName(string defaultPortName)
    {
        string portName;
 
        Console.WriteLine("Available Ports:");
        foreach (string s in SerialPort.GetPortNames())
        {
            Console.WriteLine("   {0}", s);
        }
 
        Console.Write("COM port({0}): ", defaultPortName);
        portName = Console.ReadLine();
 
        if (portName == "")
        {
            portName = defaultPortName;
        }
        return portName;
    }
 
    public static int SetPortBaudRate(int defaultPortBaudRate)
    {
        string baudRate;
 
        Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
        baudRate = Console.ReadLine();
 
        if (baudRate == "")
        {
            baudRate = defaultPortBaudRate.ToString();
        }
 
        return int.Parse(baudRate);
    }
 
    public static Parity SetPortParity(Parity defaultPortParity)
    {
        string parity;
 
        Console.WriteLine("Available Parity options:");
        foreach (string s in Enum.GetNames(typeof(Parity)))
        {
            Console.WriteLine("   {0}", s);
        }
 
        Console.Write("Parity({0}):", defaultPortParity.ToString());
        parity = Console.ReadLine();
 
        if (parity == "")
        {
            parity = defaultPortParity.ToString();
        }
 
        return (Parity)Enum.Parse(typeof(Parity), parity);
    }
 
    public static int SetPortDataBits(int defaultPortDataBits)
    {
        string dataBits;
 
        Console.Write("Data Bits({0}): ", defaultPortDataBits);
        dataBits = Console.ReadLine();
 
        if (dataBits == "")
        {
            dataBits = defaultPortDataBits.ToString();
        }
 
        return int.Parse(dataBits);
    }
 
    public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
    {
        string stopBits;
 
        Console.WriteLine("Available Stop Bits options:");
        foreach (string s in Enum.GetNames(typeof(StopBits)))
        {
            Console.WriteLine("   {0}", s);
        }
 
        Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString());
        stopBits = Console.ReadLine();
 
        if (stopBits == "")
        {
            stopBits = defaultPortStopBits.ToString();
        }
 
        return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
    }
 
    public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
    {
        string handshake;
 
        Console.WriteLine("Available Handshake options:");
        foreach (string s in Enum.GetNames(typeof(Handshake)))
        {
            Console.WriteLine("   {0}", s);
        }
 
        Console.Write("Handshake({0}):", defaultPortHandshake.ToString());
        handshake = Console.ReadLine();
 
        if (handshake == "")
        {
            handshake = defaultPortHandshake.ToString();
        }
 
        return (Handshake)Enum.Parse(typeof(Handshake), handshake);
    }
}
 
pleace help.
Posted 27-Aug-12 8:05am
Edited 27-Aug-12 8:33am
v2
Comments
Wes Aday at 27-Aug-12 13:09pm
   
Help with what? Why dump your code here without asking a question?
ideal-profi at 27-Aug-12 13:13pm
   
My question is how can I do to make my program automatically search for the COM port to which the phone is connected. As you can see now I have to manually enter
Wes Aday at 27-Aug-12 17:25pm
   
Get a list of the available comports and see which one responds to an AT command.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

have a look on this link..
 
http://stackoverflow.com/questions/2938442/how-to-find-which-serial-port-is-currently-used[^]
you have to find the command for your phone and you can just send the command to each port and check for response.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Maciej Los 205
2 Peter Leow 140
3 Suvendu Shekhar Giri 134
4 Abhinav S 126
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 8,895
2 Peter Leow 5,044
3 Kornfeld Eliyahu Peter 3,333
4 Maciej Los 2,561


Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 27 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100