Click here to Skip to main content
15,885,980 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Friends,

Here iam Developing one Sms Application Through Modem.While Connecting to Modem iam serching minimum 20 Com ports through loop.Actually the Modem is Connected to Com5.In for loop iam trying to search which Com port is Connected.So the for loop is checking com1,com2,com3,com4 which is failed so the Exception is Coming,So every time iam Showing Exception Messages in Message Box.So it is Not Good For End User.So My idea is while Connected to Modem.We should hold the Com port Information.Means if the Modem is Connected to Com5.I should connected to Com5 or If Modem is Connected to Com3 i should connected to Com3 like this.So i can avoid the Exceptuon Messages.

Regarding this if any Idea please Share with me.



Regards,


AnilKumar.D
Posted

Solution : Comment or remove the MessageBox which you are currently showing.

What you are doing presently is 100% correct, there is no way to find out to which comport is connected to the modem. Because you can't identify the modem until you talk(send command) to the modem, like at least(AT)
if the comport is response to the command that is the comport connected to the modem.


Thanks
--RA
 
Share this answer
 
Comments
Anil Honey 206 14-Jun-12 2:18am    
As per Your Information i can Remove and Commet the Try catch and Exception Block.But if the Modem is Not There.Then the End User will try to Connect the Modem which is not Present in the System.At that time i should the Message "Modem is not there or Not Connected.For this also i should put Exception Message Box.Because this Exception will also hit through the for loop only.Because as per me the Application should be Customized.
Rajesh Anuhya 14-Jun-12 2:20am    
can you post your code
Anil Honey 206 14-Jun-12 2:28am    
Down i posted My Code please check it.
this is my code
C#
private bool EnterNewSettings()
{
    int newPort=0;
    int newBaudRate=0;
    int newTimeout=0;
    int count = 0;
    string[] strport;
    string[] strbaudrate;
    string[] strtimeout;
    try
    {
        strport = Convert.ToString(ConfigurationSettings.AppSettings["COMPortID"]).Split(',');
        strbaudrate = Convert.ToString(ConfigurationSettings.AppSettings["COMBaudRATE"]).Split(',');
        strtimeout = Convert.ToString(ConfigurationSettings.AppSettings["COMTimeOUT"]).Split(',');

    }
    catch (Exception es) { return false; }

    bool comStatus;
    bool comMTN = true;
    int comPortNonMTN = 0;
    if (count != 1)
    {
        for (int i = 0; i < strport.Length; i++)
        {
            comStatus = true;

            for (int j = 0; j < strbaudrate.Length; j++)
            {
                for (int k = 0; k < strtimeout.Length; k++)
                {
                    try
                    {
                        newPort = int.Parse(strport[i]);
                    }
                    catch (Exception)
                    {
                        AddToLog("Invalid port number");
                        return false;
                    }

                    try
                    {
                        newBaudRate = int.Parse(strbaudrate[j]);
                    }
                    catch (Exception)
                    {
                        AddToLog("Invalid baud rate");
                        return false;
                    }

                    try
                    {
                        newTimeout = int.Parse(strtimeout[k]);
                    }
                    catch (Exception)
                    {
                        AddToLog("Invalid timeout value");
                        return false;
                    }

                    SetData(newPort, newBaudRate, newTimeout);
                    GsmCommMain comm = new GsmCommMain(port, baudRate, timeout);
                    try
                    {
                        if (comStatus)
                        {
                            comm.Open();
                            comStatus = false;
                            Thread.Sleep(20000);
                            if (!comm.IsConnected())
                            {
                                //AddToLog("Not Connected : Port - " + port);
                                comm.Close();
                                //return false;
                            }
                            else
                            {
                                if (comMTN)
                                {
                                    comm.Close();
                                    comMTN = false;
                                    comPortNonMTN = i;

                                    i = strport.Length + 1;
                                    count = 1;

                                    return true;

                                }
                                else
                                {
                                    AddToLog("Connected: Port - " + port);
                                    comm.Close();
                                    return true;
                                }
                            }
                        }
                    }

                    catch (Exception ex)
                    {
                        //AddToLog("Connection error: " + ex.Message);
                        //return false;
                    }
                }
            }
            if (i + 1 == strport.Length)
            {
                i = comPortNonMTN - 1;
            }
        }
    }
    return false;
}
 
Share this answer
 
v2

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