Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Roll Your Own SMS Gateway : Page 3

You can do a lot of fun applications using an SMS gateway but unless you work for a major television network, good luck getting a return on your investment. In this article, we'll show you a great alternative to an expensive SMS gateway using a Pocket PC and a Bluetooth connection.




Application Security Testing: An Integral Part of DevOps

Developing the Server
Now that we have developed the Pocket PC application, let’s turn our attention to the server application. Using Visual Studio 2005, create a new Windows application and name it SMS_Desktop. Populate the default Form1 with the controls shown in Figure 5.

For the TextBox control, set the ScrollBars property to Vertical and the Multiline property to True.

The four ProgressBar controls are used to show the number of votes for each choice (participant)—A, B, C, or D.

For simplicity's sake, you will use the Application Settings feature of Windows Forms 2.0 to store the vote count for each choice (in real-life you would use a database). Right-click on the project name in Solution Explorer and select Properties. Select the Settings tab and add the four application settings as shown in Figure 6.

Figure 5. Populate Form1 with the various controls as shown.
Figure 6. Add four application settings to the project.

In the code-behind of Form1, declare the following constants and variable:

Public Class Form1 '---the constants for the commands--- Const VOTE_COMMAND As String = "VOTE" Const SEND_COMMAND As String = "SEND" Private WithEvents serialPort As New IO.Ports.SerialPort

In the Form1_Load event, you will first call the UpdateBars() subroutine to initialize the progress bars with the current vote count for each choice (I will define this shortly). You will also open the serial connection with the Pocket PC over a Bluetooth connection:

Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load '---update the progressbars--- UpdateBars() '---close the serial port if open--- If serialPort.IsOpen Then serialPort.Close() End If '---open the serial port to connect to server--- Try With serialPort .PortName = "COM12" .BaudRate = 9600 .Parity = IO.Ports.Parity.None .DataBits = 8 .StopBits = IO.Ports.StopBits.One .Handshake = IO.Ports.Handshake.None End With serialPort.Open() Catch ex As Exception MsgBox(ex.ToString) End Try End Sub

Author's Note: I am assuming that COM12 is used by the Bluetooth serial port on the computer. Change this COM port according to the port number used on your computer.

Handle the DataReceived event when incoming data is received at the serial port:

'---data received from Pocket PC--- Private Sub DataReceived( _ ByVal sender As Object, _ ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _ Handles serialPort.DataReceived '---update it in the textbox--- TextBox1.BeginInvoke(New _ myDelegate(AddressOf updateTextBox), _ New Object() {}) End Sub

In this case, you simply display the received data in the TextBox1 control.

In the updateTextBox() control, incoming data is first read and then added to the TextBox1 control. You then write the data to a log file using the WriteToLog() subroutine. Finally, you will tabulate the vote count using the ProcessData() subroutine:

'---delegate for updating the TextBox control--- Public Delegate Sub myDelegate() '---display data received from Pocket PC--- Public Sub updateTextBox() '---for receiving plan ASCII text--- Dim data As String = serialPort.ReadLine '---e.g. "+123456789,VOTE A 987-65-4329"--- '---append it to the TextBox control--- With TextBox1 .AppendText(data & vbCrLf) .ScrollToCaret() End With '---write to log file--- WriteToLog(data) '---process the received data--- ProcessData(data) End Sub

In the WriteToLog() subroutine, you simply write the received data to a text file for archival purpose:

Private Sub WriteToLog(ByVal str As String) My.Computer.FileSystem.WriteAllText("C:\SMSVotes.txt", _ str, True) End Sub

The ProcessData() subroutine splits the received data so as to extract the choice voted by the sender. It then updates the application setting for each choice:

'---tabulate the scores--- Private Sub ProcessData(ByVal str As String) Dim fields() As String = str.Split(",") '---fields(0) is caller number--- '---fields(1) contains the data, e.g. VOTE A 987-65-4329 Dim subFields() As String = fields(1).Split(" ") '---subfields(0) is Vote, subfields(1) is choice--- With My.Settings Select Case UCase(subFields(1)) Case "A" .ChoiceA += 1 Case "B" .ChoiceB += 1 Case "C" .ChoiceC += 1 Case "D" .ChoiceD += 1 End Select .Save() End With '---send a reply to the user--- SendReply(fields(0), "Thank you for your vote!") '---update the progress bars--- UpdateBars() End Sub

Upon tabulating the vote, you can also send a reply back to the sender. This is accomplished by the SendReply() subroutine. Here, you simply write a SEND command to the Pocket PC via the serial port:

'---send message to the Pocket PC to ask it ' to send a SMS to the user--- Private Sub SendReply( _ ByVal recipientNumber As String, _ ByVal message As String) '---.e.g. "SEND:+123456789:Thank you for your vote!"--- serialPort.Write( _ SEND_COMMAND & ":" & recipientNumber & _ ":" & message & vbCrLf) End Sub

The UpdateBars() subroutine updates the value of the four ProgressBar controls:

'---update the progressbars--- Private Sub UpdateBars() With My.Settings Label1.Text = "Choice A (" & .ChoiceA & " vote(s) )" ProgressBar1.Value = .ChoiceA Label2.Text = "Choice B (" & .ChoiceB & " vote(s) )" ProgressBar2.Value = .ChoiceB Label3.Text = "Choice C (" & .ChoiceC & " vote(s) )" ProgressBar3.Value = .ChoiceC Label4.Text = "Choice D (" & .ChoiceD & " vote(s) )" ProgressBar4.Value = .ChoiceD End With End Sub

Finally, the Reset button resets the vote count for each choice:

'---reset all the scores--- Private Sub btnReset_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnReset.Click With My.Settings .ChoiceA = 0 .ChoiceB = 0 .ChoiceC = 0 .ChoiceD = 0 .Save() End With UpdateBars() End Sub

Figure 7 shows data received from the Pocket PC and the various vote counts.

Figure 7. The screen shot shows messages received by the server and the vote counts.

Testing the System
To test the system:

  • Pair up your Pocket PC with your computer using Bluetooth.
  • Establish a serial connection between the Pocket PC and computer. This article assumes that your Pocket PC will use COM6 and your computer will use COM12. Remember to change it based on your own COM port number.
  • Deploy the Pocket PC application onto your Windows Mobile 5.0 Pocket PC and launch it.
  • Ensure that the Pocket PC application is running before you launch the Windows application on the server computer.
  • Use another mobile phone and send an SMS message to the Pocket PC. After a while, you should be able to see the Pocket PC showing the received message. At the same time, the message would also be received by the application running on the server.
  • Upon showing the updated vote, you should be able to receive an acknowledgement message from the Pocket PC.
In this article, you have seen how to build a simple SMS gateway for tabulating vote count. With some creativity and exploration, you can build more interesting applications using the SMS gateway. If you have a good idea for using the SMS gateway, send it my way at the email address in my bio at the bottom of this page.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date