Instead of having to manually put the port in the txt field, how to make it auto-detect and fill in the field?
private void btnSend_Click(object sender, EventArgs e)
{
try
{
SerialPort sp = new SerialPort();
sp.PortName = txtPort.Text;
sp.Open();
sp.WriteLine("AT" + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine("AT+CMGF=1" + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine("AT+CSCS=\"GSM\"" + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine("AT+CMGS=\"" + txtPhoneNumber.Text + "\"" + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine(txtMessage.Text + Environment.NewLine);
Thread.Sleep(100);
sp.Write(new byte[] { 26 }, 0, 1);
Thread.Sleep(100);
var response = sp.ReadExisting();
if (response.Contains("ERRO"))
MessageBox.Show("Envio falhou , tente novamente daqui 5 segundos !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("SMS Enviado !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
sp.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}