VB6 Receive SMS thru GSM Modem

screen

Option Explicit

Dim message As Integer

Public oFSO As New FileSystemObject

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public commport1 As String

Private Sub Form_Load()

If commport1 = "" Then

MsgBox "Please select the Comm Port First", vbCritical

Exit Sub

End If

With MSComm1

.CommPort = commport1

.Settings = "9600,N,8,1"

.Handshaking = comRTS

.RTSEnable = True

.DTREnable = True

.RThreshold = 1

.SThreshold = 1

.InputMode = comInputModeText

.InputLen = 0

.PortOpen = True 'must be the last

End With

End Sub

Private Sub MSComm1_OnComm()

Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()

receivedsms

Timer1.Enabled = False

End Sub

Public Sub receivedsms()

Dim STR As String

Dim cmd, log_Str As String

Dim lines() As String, i As Integer

Dim str1 As String

Dim str2 As String

Dim msgno, mobile, msgreceived As String

Dim j As Integer

cmd = "AT+CMGL=" & Chr(34) & "ALL" & Chr(34)

MSComm1.Output = "AT" & vbCrLf

Sleep 500

MSComm1.Output = cmd & vbCrLf 'This line can be removed if your modem will always be in Text Mode..."

Sleep 1500

STR = MSComm1.Input & vbCrLf

Sleep 1500

'List1.AddItem STR

'txtweight.Text = STR

lines() = Split(STR, vbCrLf)

For i = 0 To UBound(lines) - 1

If InStr(1, lines(i), "+CMGL:") Then

Sleep 500

str2 = lines(i)

List2.AddItem str2

If InStr(1, str2, "REC READ") Then

str1 = "REC READ"

j = 12

Else

str1 = "REC UNREAD"

j = 14

End If

msgno = Mid(str2, InStr(1, str2, "+CMGL:") + 7, (InStr(1, str2, str1) - (InStr(1, str2, "+CMGL:") + 9)))

mobile = Mid(lines(i), InStr(1, lines(i), str1) + j, 12)

msgreceived = lines(i + 1)

List2.AddItem "msgno=> " & msgno & " mobile=> " & mobile & "message=> " & msgreceived

cmd = "AT+CMGD=" & msgno

MSComm1.Output = cmd & vbCrLf

mobile = Replace(mobile, "+", "")

If IsNumeric(mobile) Then

'///////Here I am sending SMS back using a third party API

Inet1.OpenURL ("http://example.com/eadgn.php?source=sms&msisdn=" & mobile & "&shortcode=55333300&msg=" & msgreceived)

End If

Sleep 500

End If

Next

End Sub

Public Sub create_log(logstr As String)

Dim logpath As String

Dim txtfile As TextStream

Dim strFolderPath As String

strFolderPath = App.Path & "\log"

If Dir(strFolderPath, vbDirectory) = "" Then

MkDir strFolderPath

End If

logpath = App.Path & "\log\log_" & Format(Date, "yyyymmdd") & ".ini"

If Not oFSO.FileExists(logpath) Then

'CREATING LOG FILE FOR AGENT

Set txtfile = oFSO.CreateTextFile(logpath, True)

txtfile.Close

End If

Set txtfile = oFSO.OpenTextFile(logpath, ForAppending)

txtfile.WriteLine "###########################################################################################################"

txtfile.WriteLine " => " & logstr

txtfile.Close

Set txtfile = Nothing

End Sub