Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
THIS QUESTION MAY SEAM A DUPLICATE, BUT NOT. after a long period of search on internet and no, result, then had to seek for assistance.

All solutions apply to loading available ports in a combo box and the user checks one at a go. But the automation feature then dies.
thus,
i'm looking for assistance on how the modem can connect automatically from the available ports without user interaction (USER FRIENDLINESS)

FOR THE COMBO BOX, IT IS WORKING FINE;

What I have tried:

C#
using GsmComm.GsmCommunication;
using GsmComm.PduConverter;
using GsmComm.Server;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace COMM_All
{
    public partial class Comm_F : Form
    {
        public Comm_F()
        {
            InitializeComponent(); 
        }

        private void COM_PORTS()
        {
            string[] ports = SerialPort.GetPortNames();
            txtGPort1.Items.AddRange(ports);
            txtGPort2.Items.AddRange(ports);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            COM_PORTS(); 
        }

        private void Modem1_Click(object sender, EventArgs e)
        {
            if (txtGPort1.Text == "") { MessageBox.Show("Invalid Port Name"); return; }
            comm = new GsmCommMain(txtGPort1.Text, 9600, 8);
            Cursor.Current = Cursors.Default;

            bool retry;
            do
            {
                retry = false;
                try
                {
                    Cursor.Current = Cursors.WaitCursor; comm.Open(); Cursor.Current = Cursors.Default;
                    //MessageBox.Show("Modem Connected Sucessfully");
                     txtGStatus1.Text = "Connected Sucessfully";
                     comm.EnableMessageNotifications();
                     MessageBox.Show("Message notifications activated.");

                }
                catch (Exception)
                {
                    Cursor.Current = Cursors.Default;
                    if (MessageBox.Show(this, "GSM Modem is not available", "Check",
                        MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry)
                        retry = true; 
                    else { return; }
                }
            } while (retry);
  
      }
}
}
Posted
Updated 13-Oct-22 22:36pm
v3
Comments
[no name] 15-Nov-20 9:04am    
You could try connecting to each port in turn until one succeeds.
honey the codewitch 15-Nov-20 10:34am    
ooh that could be fun depending on whats on the other COM ports. :P
[no name] 15-Nov-20 10:46am    
Not to mention matching the baud rate, etc.
[no name] 15-Nov-20 10:46am    
Fun is what it's all about. :)
Robert S4r 15-Nov-20 10:51am    
HOW? Code....?

Use the WMI (Windows Management Instrumentation) API to list all USB modems attached to the machine. Connect to the port with your modem automatically in code (for example, when your application starts).
 
Share this answer
 
v2
Comments
Dave Kreskowiak 27-Aug-24 9:04am    
It's a nice thought, but some modems don't show up in WMI as a modem. Sometimes, especially on the cheaper end, they just show up as a COM port and that's it.
Vincent Okeyo 27-Aug-24 12:25pm    
I understand, but there is a class called POTSModem in the namespace ROOT.CIMV2.Win32 that you can actually use to filter out ports that do not have a modem attached to them. It is a little bit more involving, especially if you want to automate everything and make the user's life easier. I have apps in production doing exactly this.
Dave Kreskowiak 27-Aug-24 13:33pm    
:thumbsup:
You would have to open each port, one at a time, and send a command to it to get an expected response. If you don't get that response, close the port and move on to the next one.
 
Share this answer
 
Comments
Robert S4r 15-Nov-20 10:52am    
how? code....?
Dave Kreskowiak 15-Nov-20 18:29pm    
It's simple. You open the port, send a command that doesn't do anything or send a command that tells the phone to ID itself. If you get an expected response, you found the port. If not, close the port and move to the next one.

I do not have code as I don't do anything over a serial port or with any phones connected to a PC.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900