Wednesday, December 12, 2012

VB6 SMS Gateway: Menambahkan Fitur Auto Reply

Mengenai cara membalas SMS secara otomatis melalui aplikasi VB6 - Sebelumnya kita telah membahas mengenai cara menerima SMS baru klik disini, nah sekarang kita akan menambahkan fitur auto reply melalui AT Commands dengan aplikasi VB6 yang kita buat sendiri. Adapun contoh kode VB6 membalas SMS otomatis adalah seperti di bawah ini:
Option Explicit

Dim strBuffer As String
Dim blnFirstLoad As Boolean

Private Sub Command1_Click()
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
With MSComm1
.PortOpen = True
.Output = "AT+CMGS=" & Chr(34) & TxtNumber.Text & Chr(34) & vbCrLf
.Output = TxtMessage.Text & Chr(26)
End With
End Sub

Private Sub Form_Load()
With MSComm1
.CommPort = 7 'port disesuaikan atau beri kode auto detect port modem
.Settings = "115200,N,8,1"
.Handshaking = comRTS
.RTSEnable = True
.DTREnable = True
.RThreshold = 1
.SThreshold = 1
.NullDiscard = True
.InputMode = comInputModeText
.InputLen = 0
.PortOpen = True
End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Sub

Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
strBuffer = strBuffer & MSComm1.Input
Do
strBuffer = strBuffer & MSComm1.Input
Loop While MSComm1.InBufferCount
If InStr(1, strBuffer, "+CMGR") Then
If InStr(1, strBuffer, "OK") Then
Text1.Text = strBuffer
End If
End If
If InStr(1, strBuffer, "+CMTI") > 0 Then
If Right(strBuffer, 1) = vbLf Then
Dim s() As String
s = Split(strBuffer, ",")
Debug.Print s(UBound(s))
ReadSMSByIndex Trim$(s(UBound(s)))
Delay 1
Command1_Click 'Auto reply
strBuffer = ""
End If
End If
End Select
End Sub

Private Sub ReadSMSByIndex(Index As Integer)
strBuffer = ""
MSComm1.Output = "AT+CMGR=" & Index & vbCrLf 'baca SMS yang berada di index ke-1
End Sub

Private Sub Delay(ByVal HowLong As Date)
Dim endDate As Date
endDate = DateAdd("s", HowLong, Now)
While endDate > Now
DoEvents
Wend
End Sub
Demikian contoh kode VB6 untuk membalas SMS baru secara otomatis, Anda dapat memodifikasi kodenya untuk disesuaikan dengan kebutuhan.