Introduction
This is old code that I developed for my friend working in a network maintenance company many years ago. He asked me to write a software to check the connectivity of all computers on networks that he should admin and if there was a problem send him a SMS. With this stupid software, he successfully extended his job into a large business.
How It Works
All you need is a GSM Modem using rs-232 or USB connection. Install it to a computer on a network that you want to admin and debug this project and give the software local machine names to monitor - the software frequently pings each computer and if ping failed for 4 times, adds an Err to the report list and when the Err report list reaches 100 errs, sends my friend an SMS. You can make a better rule for reporting and sending SMS.
For Sending SMS, You Should
- Connect your gsm modem through COM Port or USB
- Provide Port Number
- Provide your local call service for your own country
- Write message and destination number
- Send

Pinger
I just took the code of loading circle from an article named Loading Circle by Martin Gagne and turned it to a pinging and status module that I used it a lot, thanks Martin !
ServerChecking()
function is called every time the loadingcircle
control turns one cycle. This function sends a ping package to network (for more information about ping, click here).
string buf = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
public virtual void ServerChecking()
{
if ( _serverCheckingEnable == false || string.IsNullOrEmpty( _serverName)) return;
string who = ServerName;
System.Threading.AutoResetEvent waiter = new System.Threading.AutoResetEvent(false);
System.Net.NetworkInformation.Ping pingSender =
new System.Net.NetworkInformation.Ping();
pingSender.PingCompleted += new System.Net.NetworkInformation.
PingCompletedEventHandler(PingCompletedCallback);
string data = buf;
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 1000;
System.Net.NetworkInformation.PingOptions options =
new System.Net.NetworkInformation.PingOptions(64, true);
pingSender.SendAsync(who, timeout, buffer, options, waiter);
}
And as ping reply is received or there is a ping error, you can specify the status on loadingcircle
by changing the color of it:
public System.Net.NetworkInformation.PingReply reply;
public event EventHandler<connectionstateeventarg> ConnectionStateChanged;
internal ConnectionStateEventArg state = new ConnectionStateEventArg();
protected int succ = 0;
protected int fail = 0;
void PingCompletedCallback(object sender,
System.Net.NetworkInformation.PingCompletedEventArgs e)
{
if (e.Reply != null)
{
reply = e.Reply;
state.ExactReason = e;
_serverConnectionState = e.Reply.Status.ToString();
state.NewReply = e.Reply.Status;
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
{
if (succ > 4)
{
this.Color = Color.PaleGreen;
this.RotationSpeed = 400;
state.NewState = ConnectionStatus.ConnectedPer;
}
else
{
this.RotationSpeed = 80;
this.Color = Color.Blue;
state.NewState = ConnectionStatus.ConnectedTem;
}
succ++;
fail = 0;
ServerConnected = true;
}
else if (reply.Status == System.Net.NetworkInformation.IPStatus.TimedOut)
{
if (fail > 4)
{
this.RotationSpeed = 400;
this.Color = Color.LightPink;
state.NewState = ConnectionStatus.DisconnectedPer;
}
else
{
this.RotationSpeed = 80;
this.Color = Color.Orange;
state.NewState = ConnectionStatus.DisconnectedTem;
}
ServerConnected = false;
fail++;
succ = 0;
}
else
{
this.RotationSpeed = 80;
this.Color = Color.DarkRed;
succ = 0;
ServerConnected = false;
state.NewState = ConnectionStatus.Other;
fail++;
}
for (int i = 0; i < e.Reply.Buffer.Length; i++)
{
if (e.Reply.Buffer[i] != (byte)buf[i])
{
this.Color = Color.Purple;
state.NewState = ConnectionStatus.TransmitMismach;
}
}
}
else
{
this.RotationSpeed = 400;
this.Color = Color.Black;
succ = 0;
ServerConnected = false;
state.NewState = ConnectionStatus.Other;
fail++;
}
if (this.ConnectionStateChanged != null)
this.ConnectionStateChanged(this, state);
}
GSM Handler
Perhaps you need one of these for sending SMS:


GSM Modems are working with AT Command. Here is a component that I wrote for GSM Modem Sending and receiving SMS:
public class GSMManager : Component
{
public List<smsmessage> RecivedMessages = new List<smsmessage>();
public List<smsmessage> SendingMessages = new List<smsmessage>();
Timer timer = new Timer();
public GSMManager()
{
timer.Interval = 1000;
}
bool GO = false;
bool OK = false;
bool ERROR = false;
public System.IO.Ports.SerialPort comPort = new System.IO.Ports.SerialPort();
public bool SMSEnable = true;
public void SMSConfig()
{
if (comPort.IsOpen == false)
{
comPort.PortName =
global::LoadingCircleTester.Properties.Settings.Default.COMPortName;
comPort.BaudRate = 115200;
comPort.Parity = Parity.None;
comPort.StopBits = StopBits.One;
comPort.DataBits = 8;
comPort.ReadBufferSize = 10000;
comPort.ReadTimeout = 1000;
comPort.WriteBufferSize = 10000;
comPort.WriteTimeout = 10000;
comPort.RtsEnable = true;
}
if (comPort.IsOpen == false)
{
comPort.DataReceived -= new SerialDataReceivedEventHandler
(comPort_ReadIncommingSMS);
comPort.RtsEnable = false ;
comPort.Close();
try { comPort.Open(); }
catch (Exception ex)
{
if (SMSEnable == true)
{
SMSEnable = false;
MessageBox.Show("Port(" + comPort.PortName + ")
Could not be Opened \n\r Check your GSM Device Connection");
}
SMSEnable = false;
return;
}
comPort.RtsEnable = true;
comPort.DataReceived += new SerialDataReceivedEventHandler
(comPort_ReadIncommingSMS);
}
comPort.DiscardInBuffer();
comPort.DiscardOutBuffer();
string s = "";
comPort.Write("AT+CMGF=1" + (char)13);
GO = false; ERROR = false; OK = false;
for (int i = 0; i < 100; i++)
{
if (OK == true)
break;
if (ERROR == true)
{
SMSEnable = true;
}
System.Threading.Thread.Sleep(15); if (i == 100)
{ ERROR = true; SMSEnable = false; }
}
SMSEnable = true;
return ;
}
void SMS_is_Disable()
{
}
public SMSMessage ReadExistingSMS(int MessageIndex)
{
DateTime t1 = DateTime.Now;
Readed = "";
SMS_is_Disable();
comPort.Write("AT+CMGR=" + MessageIndex.ToString() + (char)13);
GO = false; ERROR = false; OK = false;
for (int i = 0; i < 100; i++)
{
if (OK == true)
break;
if (ERROR == true)
{
return null;
}
System.Threading.Thread.Sleep(15); if (i == 100) ERROR = true;
}
SMSMessage mess = new SMSMessage();
int k = 0;
bool messErr = false;
int l = 0;
for (int j = 0; j < 3; j++)
{
if (k == -1 || l == -1) { messErr = true; continue; }
k = Readed.IndexOf("\"", l);
if (k == -1 || l == -1) { messErr = true; continue; }
l = Readed.IndexOf("\"", k + 1);
if (k == -1 || l == -1) { messErr = true; continue; }
string inc = Readed.Substring(k + 1, l - k - 1);
if (j == 1 && messErr == false)
mess.PhoneNo = inc;
if (j == 2 && messErr == false)
mess.Time = DateTime.Parse(inc);
l++;
}
if (messErr == false)
{
mess.Message = Readed.Substring(l + 2, Readed.Length - l - 2 - 8);
mess.Status = SMSMessage.SMSMessageStatus.Recived;
DateTime t2 = DateTime.Now;
return mess;
}
return null;
}
public enum ReadIncommingSMSType
{
REC_UNREAD = 0,
REC_READ = 1,
STO_UNSENT = 2,
STO_SENT = 3,
ALL = 4
}
public void DeleteExistingSMS(int index)
{
SMS_is_Disable();
Readed = "";
comPort.Write("AT+CMGD=" + index.ToString() + (char)13);
GO = false; ERROR = false; OK = false;
for (int i = 0; i < 100; i++)
{
if (OK == true)
break;
if (ERROR == true)
{
}
System.Threading.Thread.Sleep(15); if (i == 100) ERROR = true;
}
}
public string SendSMS(SMSMessage sms)
{
return SendSMS(sms.PhoneNo, sms.Message);
}
public string callservice;
public string SendSMS(string PhoneNo, string Message )
{
SMS_is_Disable();
string s = "";
comPort.Write("AT+CSCA=\""+callservice+"\"" + (char)13);
GO = false; ERROR = false; OK = false;
for (int i = 0; i < 100; i++)
{
if (OK == true)
break;
if (ERROR == true)
{
MessageBox.Show("ERROR");
return "ERROR";
}
System.Threading.Thread.Sleep(15); if (i == 100) ERROR = true;
}
comPort.Write("AT+CMGS=\"" + PhoneNo + "\"" + (char)13);
GO = false; ERROR = false; OK = false;
for (int i = 0; i < 100; i++)
{
if (OK == true)
break;
if (ERROR == true)
{
MessageBox.Show("ERROR");
return "ERROR";
}
System.Threading.Thread.Sleep(15); if (i == 100) ERROR = true;
}
s += "\r" + comPort.ReadExisting();
comPort.Write(Message + (char)26);
GO = false; ERROR = false; OK = false;
for (int i = 0; i < 100; i++)
{
if (OK == true)
{
break;
}
if (ERROR == true)
{
MessageBox.Show("ERROR");
return "ERROR";
}
System.Threading.Thread.Sleep(15); if (i == 100) ERROR = true;
}
s += "\r" + comPort.ReadExisting();
comPort.Close();
return s;
}
public List<smsmessage> ReadIncommingSMS(ReadIncommingSMSType type)
{
DateTime t1 = DateTime.Now;
Readed = "";
SMS_is_Disable();
if (SMSEnable == false) return null;
switch (type)
{
case ReadIncommingSMSType.STO_SENT:
comPort.Write("AT+CMGL=\"STO SENT\"" + (char)13);
break;
case ReadIncommingSMSType.REC_UNREAD:
comPort.Write("AT+CMGL=\"REC UNREAD\"" + (char)13);
break;
case ReadIncommingSMSType.ALL:
comPort.Write("AT+CMGL=\"ALL\"" + (char)13);
break;
case ReadIncommingSMSType.REC_READ:
comPort.Write("AT+CMGL=\"REC READ\"" + (char)13);
break;
case ReadIncommingSMSType.STO_UNSENT:
comPort.Write("AT+CMGL=\"STO UNSENT\"" + (char)13);
break;
}
GO = false; ERROR = false; OK = false;
for (int i = 0; i < 100; i++)
{
if (OK == true)
break;
if (ERROR == true)
{
return null;
}
System.Threading.Thread.Sleep(15); if (i == 100)
{ ERROR = true; this.SMSEnable = false; return null; }
}
List<smsmessage> messList = new List<smsmessage>();
string[] re = Readed.Split(new string[] { "+CMGL:" },
StringSplitOptions.RemoveEmptyEntries);
for (int i = 1; i < re.Length; i++)
{
int k = 0;
bool messErr = false;
int l = 0;
string Str = re[i];
SMSMessage mess = new SMSMessage();
string ind = Str.Substring(0, Str.IndexOf(",")).Trim();
mess.index = int.Parse(ind);
for (int j = 0; j < 3; j++)
{
if (k == -1 || l == -1) { messErr = true; continue; }
k = Str.IndexOf("\"", l);
if (k == -1 || l == -1) { messErr = true; continue; }
l = Str.IndexOf("\"", k + 1);
if (k == -1 || l == -1) { messErr = true; continue; }
string inc = Str.Substring(k + 1, l - k - 1);
if (j == 1 && messErr == false)
mess.PhoneNo = inc;
if (j == 2 && messErr == false)
mess.Time = DateTime.Parse(inc);
l++;
}
if (messErr == false)
{
mess.Message = Str.Substring(l + 2, Str.Length - l + 2 - 8+2);
mess.Message = mess.Message.Replace("\r\nOK", "");
mess.Status = SMSMessage.SMSMessageStatus.Recived;
messList.Add(mess);
}
}
return messList;
}
string Readed = "";
void comPort_ReadIncommingSMS(object sender, SerialDataReceivedEventArgs e)
{
SMS_is_Disable();
System.IO.Ports.SerialPort SP = (System.IO.Ports.SerialPort)sender;
string s = SP.ReadExisting();
Readed+=s;
if (Readed.EndsWith("OK\r\n") || Readed.EndsWith("\r\n> ") ||
Readed.EndsWith(((char)26).ToString()))
OK = true;
if (Readed.EndsWith("ERROR\r\n"))
ERROR = true;
}
}
Remember this is a very old code of mine. It requires a lot more changes. I hope this helps you.
Visit HexWay for more of my software.