Click here to Skip to main content
15,300,338 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ok. this is the code i have written to send an sms. i want to add the function to receive a sms on the same modem. i am unable to go on. somebody help please.


VB
'class code
Imports System.IO.Ports
Imports System.Threading
 
Public Class SMSCOMMS
Private WithEvents SMSPort As SerialPort
 
Private SMSThread As Thread
Private ReadThread As Thread
Shared _Continue As Boolean = False
Shared _ContSMS As Boolean = False
Private _Wait As Boolean = False
Shared _ReadPort As Boolean = False
Public Event Sending(ByVal Done As Boolean)
Public Event DataReceived(ByVal Message As String)
 
Public Sub New(ByRef COMMPORT As String)
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

ReadThread = New Thread(AddressOf ReadPort)
End Sub
 
Public Sub SendSMS(ByVal CellNumber As String, ByVal SMSMessage As String)
Dim MyMessage As String = Nothing
If SMSMessage.Length <= 160 Then
MyMessage = SMSMessage
Else
MyMessage = Mid(SMSMessage, 1, 160)
End If
If IsOpen = True Then
SMSPort.WriteLine("AT")
Thread.Sleep(1000)
SMSPort.WriteLine("AT+CMGF=1" & vbCrLf) 'setting modem for sms mode
Thread.Sleep(1000)
'SMSPort.WriteLine("AT+CSCA=""+254722500059" & vbCrLf) 'setting the message service center number
SMSPort.WriteLine("AT+CMGS=""" & CellNumber & """" & vbCrLf)
Thread.Sleep(1000)
'SMSPort.WriteLine("AT+CMGS=" & CellNumber & vbCrLf)
_ContSMS = False
SMSPort.WriteLine(MyMessage & vbCrLf & Chr(26))
Thread.Sleep(1000)
_Continue = False
RaiseEvent Sending(False)
'MessageBox.Show("Successfully sent")
End If
End Sub
 
Private Sub ReadPort()
Dim SerialIn As String = Nothing
Dim RXBuffer(SMSPort.ReadBufferSize) As Byte
Dim SMSMessage As String = Nothing
Dim Strpos As Integer = 0
Dim TmpStr As String = Nothing
Try
 
Catch ex As Exception
 

While SMSPort.IsOpen = True
If (SMSPort.BytesToRead <> 0) And (
SMSPort.IsOpen = True) Then
While SMSPort.BytesToRead <> 0
SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize)
SerialIn =
SerialIn & System.Text.Encoding.ASCII.GetString(
RXBuffer)
If SerialIn.Contains(">") = True Then
_ContSMS = True
End If
If SerialIn.Contains("+CMGS:") = True Then
_Continue = True
RaiseEvent Sending(True)
_Wait = False
SerialIn = String.Empty
ReDim RXBuffer(SMSPort.ReadBufferSize)
End If
End While
RaiseEvent DataReceived(SerialIn)
SerialIn = String.Empty
ReDim RXBuffer(SMSPort.ReadBufferSize)
End If
End While
End Try
End Sub
 
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()
ReadThread.Start()
End If
End Sub
 
Public Sub Close()
If IsOpen = True Then
SMSPort.Close()
End If
End Sub
 
End Class
 
'window form
'Form Code
Imports System
Imports System.Threading
Imports System.ComponentModel
Imports System.IO.Ports
Imports mordemsms.SMSCOMMS
 
Public Class Form1
 
Private Sub sendbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendbtn.Click
Dim sms As SMSCOMMS
sms = New SMSCOMMS("COM12")
sms.Open()
sms.SendSMS(TextBox1.Text, TextBox2.Text)
sms.Close()
End Sub
 


End Class
Posted
Updated 14-May-14 5:05am
v2
Comments
[no name] 14-May-14 11:17am
   
Instead of reposting and cross posting this all over the place maybe you could explain why your are "unable to go on". What does that even mean? Why are you unable to implement a receive data event?

1 solution

Without spending huge time looking at your receive routines you have a problem with your whole idea. Your read can't work like you have done it once one or two characters come in you are likely to come thru the read routine you then do this

SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize)


So lets say "+CG" has arrived you read them then compare to "+CMSG:" and it doesn't match because the rest of the characters haven't got there yet.

REMEMBER THIS IS A SERIAL DEVICE SO YOU HAVE TO LOOK FOR THE END OF A WHOLE LINE AND THEN PARSE IT.

You can programatically set the terminator for these modules but if you haven't set it most use CR or CR+LF as termination of line by default.

You have to hold the receive data in a buffer until you get the terminator and only once you get the terminator then run the parse.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900