This will be my first VB.net posting as far as I could remember.
After several hours of trial and error (good thing I had unlimited sms plan for this one) I successfully able to get it work properly. This was also my first USB programming experience. Well due to the strict “language requirements” by the academe people I’ve initially written this class from C# where it all began then converted them to a usable and clean code in VB.net. There are plenty of codes out there that tackles SMS/GSM programming. But I could not find a well suitable, as simple as possible, stable and reliable code for VB.net. As a lot of people say that its easy for them to communicate with GSM Modems as long as you know the fundamentals of AT commands but I found more issues to consider before and one of them was getting the status result from the Phone provider after sending SMS from the modem. So here it is…
'/////////////////////////////////////////////// 'SMS Class ' ' Class for sending SMS with return status ' (C)opyright 2011 by www.michael-yap.com ' All Rights Reserved. '/////////////////////////////////////////////// Option Explicit On Imports System Imports System.Threading Imports System.ComponentModel Imports System.Windows.Forms Imports System.IO.Ports Public Class SMS Private WithEvents SMSPort As SerialPort Public receiveNow As AutoResetEvent Public Sub New(ByRef COMMPORT As String) receiveNow = New AutoResetEvent(False) SMSPort = New SerialPort With SMSPort .PortName = COMMPORT .BaudRate = 9600 .Parity = Parity.None .DataBits = 8 .StopBits = StopBits.One .Handshake = Handshake.RequestToSend .DtrEnable = True .RtsEnable = True .NewLine = vbCrLf End With AddHandler SMSPort.DataReceived, AddressOf port_DataReceived End Sub Public Sub SendSMS(ByVal CellNumber As String, ByVal SMSMessage As String) Dim MyMessage As String = Nothing 'Check if Message Length < = 160 If SMSMessage.Length <= 160 Then MyMessage = SMSMessage Else MyMessage = Mid(SMSMessage, 1, 160) End If If IsOpen = True Then Dim recievedData As String = ExecCommand("AT", 300, "No phone connected") recievedData = ExecCommand("AT+CMGF=1", 300, "Failed to set message format.") Dim command As String = "AT+CMGS=" & CellNumber & vbCr recievedData = ExecCommand(command, 300, "Failed to accept phoneNo") command = MyMessage + Char.ConvertFromUtf32(26) + "\r" recievedData = ExecCommand(command, 4000, "Failed to send message") End If End Sub Private Function ExecCommand(ByVal command As String, ByVal responseTimeout As Integer, ByVal errorMessage As String) As String Try SMSPort.DiscardOutBuffer() SMSPort.DiscardInBuffer() receiveNow.Reset() SMSPort.WriteLine(command) Dim input As String = ReadResponse(responseTimeout) If ((input.Length = 0) Or ((Not input.EndsWith("> ")) And (Not input.Contains("OK")))) Then Throw New ApplicationException("No success message was received.") End If Return input Catch ex As Exception Throw ex End Try End Function Private Sub port_DataReceived(ByVal sender As Object, e As SerialDataReceivedEventArgs) Try If e.EventType = SerialData.Chars Then receiveNow.Set() End If Catch ex As Exception End Try End Sub Public Function ReadResponse(ByVal timeout As Integer) As String Dim buffer As String = String.Empty Try Do While (Not buffer.Contains("OK") And Not buffer.EndsWith("> ") And Not buffer.Contains("ERROR")) If receiveNow.WaitOne(timeout, False) Then Thread.Sleep(500) ' you need to adjust this settings depending on how your modem can ' able to get response to the provider, its recommended also to use the ' thread timeout instead of adding this. sorry its a quick fix :( buffer += SMSPort.ReadExisting() Else If (buffer.Length > 0) Then Throw New ApplicationException("Response received is incomplete.") Else Throw New ApplicationException("No data received from phone.") End If End If Loop Catch ex As Exception Throw ex End Try Return buffer End Function Public ReadOnly Property IsOpen() As Boolean Get If SMSPort.IsOpen = True Then IsOpen = True Else IsOpen = False End If End Get End Property Public Sub Open() If IsOpen = False Then SMSPort.Open() End If End Sub Public Sub Close() If IsOpen = True Then SMSPort.Close() End If End Sub End Class
a sample usage will be
'send sms Dim Retries as Integer=1 Dim SMSEngine = New SMS("COM3") 'replace with your actual modem USB/Serial Port Dim validnumber as String = "+639221234567" ' replace with your number, be sure it includes ' the country code SMSEngine.Open() Try SMSEngine.SendSMS(validnumber, textmessage) Catch ex As Exception If Retries >= 3 Then Console.Write("Failed after 3 retries: " & ex.Message) 'you can customize this base on the failed result status Else Retries + = 1 End If End Try SMSEngine.Close()
BTW: I used Wavecom Fastrack GSM USB Modem as my SMS gateway.









