0

My client wants a simple C# WinForms application to send SMS messages over the Huawei E303 dongle. I used the below code and it works successfully.

public class SMSSender
{
    SerialPort sp = new SerialPort();

    public bool SendMessage(string PhoneNumber, string Message, int port)
    {
        string selectedPort = String.Concat("COM", port);

        sp.PortName = selectedPort;
        sp.Open();

        PhoneNumber = char.ConvertFromUtf32(34) + PhoneNumber + char.ConvertFromUtf32(34);
        sp.Write("AT+CMGF=1" + char.ConvertFromUtf32(13));
        sp.Write("AT+CMGS=" + PhoneNumber + char.ConvertFromUtf32(13));

        sp.Write(Message + char.ConvertFromUtf32(26) + char.ConvertFromUtf32(13));
        sp.Close();
    }
}

But the problem is, this code does not detect the dongle port number automatically. I cannot hard code the port number because when he connect the dongle to a different USB port, then port number changes. As a simple solution, I added a text box to enter the port number manually.

Can anyone help me to improve this code to automatically detect the dongle port number?

0 Answers0