this is my code
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())
{
comm.Close();
}
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)
{
}
}
}
if (i + 1 == strport.Length)
{
i = comPortNonMTN - 1;
}
}
}
return false;
}