What I was trying to achieve was to write a simple SMS web interface to send text messages via an old GSM modem (Huawei E270) which is connected to USB. I wanted to write it myself because of existing database contacts integration etc.

So after installing the Huawei USB driver to be able to access it via serial COM port, I tried several ready-to-use PHP SMS classes. Long story short, they all didn’t work – maybe because it was a virtual COM port, whatever.

As an alternative, I looked for a Windows command-line executable (the server is running Windows) which sends messages with the given parameters and can be run from a PHP script. There were some interesting ones like this one, but they didn’t accept new-line (CR, LF) and some other special characters in the message text.

This took me to writing the C# project myself, too. Basically, it is not hard to write the AT commands the modem uses to the serial port, but I also failed when it came to special characters. Trying several encoding types like ASCII, UTF8, Encoding.Default etc. didn’t help. I also had a look at the supported codepages of different modems via HyperTerminal, but they all looked quite the same, so I could not have any special modem type.

So I came to the conclusion, even if the modem accepts messages in TEXT mode, I would have to use PDU mode, where it is not that easy to generate the AT commands.

Fortunately, this library solved all problems finally. The included demo project is perfect in general for GSM modem diagnosis, and text messages can be sent with any special characters without problems. So I exctracted the following code of the demo project to create the command-line tool for my PHP script:

 

namespace smssend
{
    class Program
    {
        static void Main(string[] args)
        {
			string portName = "COM10";
			int baudRate = 57600;
			int timeout = 300;
			GsmCommMain comm;

			comm = new GsmCommMain(portName, baudRate, timeout);

			try
			{
				comm.Open();
			}
			catch(Exception)
			{
					return;
			}

            try
            {
                SmsSubmitPdu pdu;
                pdu = new SmsSubmitPdu("testöäünasdf","+43123456789");
                comm.SendMessage(pdu);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            // Close connection to phone
            if (comm != null && comm.IsOpen())
                comm.Close();
        }
    }
}

In the end, the code of course has to be modified to extract the parameters of the „args“ variable, I did not include it here to only show the more important steps. In PHP, the application can then be called with

exec(„yourprogram.exe -nr 12345 -text testtext“, $output, $retval);

Make sure Apache runs with the right permissions to execute the exe file.

 

Other References:

http://prakashmca007.blogspot.co.at/2009/12/sending-sms-in-cnet-using-gsm-modem-and.html

Online SMS PDU Decoder/Converter

https://sites.google.com/site/freesmsuk/gsm7-encoding

 

  [email protected]