COEN 3244
INPUT / OUTPUT MEMORY SYSTEM
Experiment # 4
AT Commands
Submitted by:
Aquino, Richard F.
Daitan, R B.
Rodriguez, Joshua Benjamin B.
Sarong, Christopher B.
BSCpE 5-4
Submitted to:
Engr. Allan B. Verzo
Instructor
Date Submitted: Schedule:
July 14, 2014 M/M 7:30-10:30AM/10:30-1:30PM
Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
COLLEGE OF ENGINEERING
DEPARTMENT OF COMPUTER ENGINEERING
OBJECTIVES:
To launch an external application once the GSM Modem receives a message.
MATERIALS:
Computer x1
Broadband Stick x1
Cellphone
PROCEDURE:
1. Study AT Commands.
2. Connect Broadband Stick to PC.
3. Verify that its working using AT Commands.
4. Start Coding
5. Verify that the program is working.
CONCLUSION:
AT Commands are an important part in IO projects because it allows communication to
devices that enable wireless connectivity like SMS.
SCREENSHOTS:
SOURCE CODE:
using System;
using System.IO.Ports;
using System.Threading;
namespace Kippenberger {
public class GsmComm {
public SerialPort GsmPort;
private const int BAUDRATE = 9600;
private const int DATABITS = 8;
private const int SLEEPTIME = 5;
public GsmComm () {
GsmPort = new SerialPort ();
GsmPort.BaudRate = BAUDRATE;
GsmPort.Parity = Parity.None;
GsmPort.DataBits = DATABITS;
GsmPort.StopBits = StopBits.One;
GsmPort.Handshake = Handshake.RequestToSend;
GsmPort.DtrEnable = true;
GsmPort.RtsEnable = true;
GsmPort.NewLine = System.Environment.NewLine;
}
public string Port {
set {
if (GsmPort.IsOpen)
GsmPort.Close ();
GsmPort.PortName = value;
}
get {
return GsmPort.PortName;
}
}
public void Open () {
if (!GsmPort.IsOpen)
GsmPort.Open ();
}
public void Close () {
if (GsmPort.IsOpen)
GsmPort.Close ();
}
public string Execute (string atCommand, char? lineEnd, int? sleepTime) {
if (GsmPort.IsOpen) {
try {
GsmPort.DiscardInBuffer ();
GsmPort.WriteLine (atCommand + (lineEnd.HasValue ?
(char)lineEnd : (char)13));
Thread.Sleep (sleepTime.HasValue ? (int)sleepTime :
SLEEPTIME);
return "Run: " + atCommand;
}
catch (Exception ex) {
return atCommand + " Error: " + ex.Message;
}
}
else
return atCommand + " Error: Port is CLOSED.";
}
public string Read () {
string readBuffer = string.Empty;
if (GsmPort.IsOpen) {
try {
int bytesToRead = 0;
do {
bytesToRead = GsmPort.BytesToRead;
byte[] byteData = new byte[bytesToRead];
GsmPort.Read (byteData, 0, byteData.Length);
foreach (byte bData in byteData)
readBuffer += ((char)bData).ToString ();
}
while(bytesToRead > 0);
return readBuffer;
}
catch (Exception ex) {
return "ReadData Error: " + ex.Message;
}
}
else
return "ReadData Error: Port is CLOSED.";
}
}
}
using System;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Threading;
using Gtk;
using IO_SMS_GUI;
public partial class MainWindow: Gtk.Window {
public MainWindow () : base (Gtk.WindowType.Toplevel) {
Project.Sms = new Kippenberger.GsmComm ();
Project.Sms.GsmPort.DataReceived += new SerialDataReceivedEventHandler
(OnMessageReceived);
Build ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a) {
Project.Sms.Close ();
Application.Quit ();
a.RetVal = true;
}
protected void OnSend (object sender, EventArgs e) {
//throw new NotImplementedException ();
string atNumber = string.Format ("AT+CMGS=\"{0}\"", entry_number.Text);
string atMessage = string.Format (">{0}{1}", entry_message.Text, (char)26);
Project.Sms.Execute (atNumber, null, 100);
Project.Sms.Execute (atMessage, null, 1000);
}
protected void OnUsePort (object sender, EventArgs e) {
//throw new NotImplementedException ();
Project.Sms.Port = entry_port.Text;
Project.Sms.Open ();
Project.Sms.Execute ("AT^CURC=0", null, 100);
Project.Sms.Execute ("AT+CGREG=0", null, 100);
Project.Sms.Execute ("AT+CREG=0", null, 100);
Project.Sms.Execute ("AT+CMGF=1", null, 100);
Project.Sms.Execute ("AT+CMGL=\"ALL\"", null, 1000);
textview_messages.Buffer.Text = Project.Sms.Read ();
}
protected void OnMessageReceived (object sender, SerialDataReceivedEventArgs e) {
// Sleep thread to delay execution of Read() when AT+CGML="ALL" is executed
// Sleep time must be greater than sleep time in AT+CGML="ALL"
Thread.Sleep (1500);
//Console.WriteLine (Project.Sms.Read());
string receivedData = Project.Sms.Read ();
if (Regex.IsMatch (receivedData, Regex.Escape ("+CMTI:"))) {
Project.ExecFromMessage (receivedData);
}
}
protected void OnRefresh (object sender, EventArgs e) {
//throw new NotImplementedException ();
Project.Sms.Execute ("AT+CMGL=\"ALL\"", null, 1000);
textview_messages.Buffer.Text = Project.Sms.Read ();
}
protected void OnDeleteAll (object sender, EventArgs e) {
// throw new NotImplementedException ();
Project.Sms.Execute ("AT+CMGD=0,1", null, 1000);
Project.Sms.Execute ("AT+CMGL=\"ALL\"", null, 1000);
textview_messages.Buffer.Text = Project.Sms.Read ();
}
}