Quality ready made and affordable custom software



PHP Serial
Easy Control
Rack Designer

Quick Texts
Wallpaper Slideshow


All articles
WS2300 weather station PHP project
Your own terminal in 17 lines
PC PIC Voltmeter
Network messenger in 22 lines
Phone Dialer




Copyright 2001-2022
© Cosmin Buhu

  PHP Serial Example
Download      Buy

PHP Serial Extension Example - Listing SMS form a GSM phone, modem or terminal

This example shows how easy is to program with PHP serial extension to handle a GSM device

We assume you have setup PHP serial extension as described here, and the included test page lists functions correctly.
As hardware setup we'll connect GSM phone or modem to say port COM1, and will work at 9600 baudrate, 8 databits, parity none, 1 stop bit, no flowcontrol. Any GSM phone with serial data port and cable is OK if offers SMS services via AT commands, or better get a dedicated GSM modem such as Siemens MC35, Telit EZ10, Falcom, Wavecom.

These GSM devices can be controlled with some kind of language based on commands, called AT commands, you can find a lot of documentation around. In short, the host (your PC running PHP script) sends commands to the modem and gets back answers. The whole program logic is based on this conversation. For example, host sends "AT" command, modem answers with "OK".

Enough talks, let's see what our PHP script source is doing.

First, we can read PHP extension version:
$str = ser_version();
echo "Version: $str";

This outputs something like 20091007.1.

Then open serial port:
ser_open( "COM1", 9600, 8, "None", "1", "None" );

Hopefully the port is open now, we can test it with:
if (ser_isopen()) echo "Port is open!";
Which outputs: Port is open!

Some modems require to set DTR, so we can do it:
ser_setDTR( True );

We handle SMS in text mode, set this with AT+CMGF command:

All commands returns something, at least an OK or error. We read and print this response:
sleep(1); // wait a while
$str = ser_read();
echo $str;

Should get an OK at this point.

We are ready now to list SMS using AT+CMGL command:

Modem will answer with complete list of SMS from its memory storage (or SIM, depending of settings)
sleep(2); // wait a while, if list is long we must wait longer
$str = ser_read();
echo $str;

We have now printed the list on webpage.

Easy isn't it? Of course this is just a basic example, a complete SMS PHP interface can be built starting with a dedicated procedure to chat with the modem based on expected answers and timeouts, to reading each message, deleting, and sending SMS messages. The core functionality is php_ser module, the rest of programming is your! If you have questions don't hesitate to contact me, please use the contact form.