Stuart McCall
7/29/2012 5:06:00 PM
Hi
Having trouble sending SMS messages from Win7 PC via smartphone from a vb6
app.
Bluetooth connection is working fine. Phone is set up as GSM modem on PC's
COM4:
Using Hyperterminal (copied from a WinXP box) I can send messages just fine
using AT commands, eg:
AT+CMGF=1<enter>
OK
AT+CMGS="+44123456789"<enter>
>
This is the message body<ctrl-z>
The OK and the > prompt are returned from the modem.
So, having established that the connection and the AT commands are working
fine, I attempt to replicate the behaviour from my app. Here's what I
currently have:
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Declare Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long)
Private Declare Function WriteFile& Lib "kernel32" _
(ByVal hFile As Long, _
lpBuffer As Any, _
ByVal nNumberOfBytesToWrite&, _
lpNumberOfBytesWritten&, _
ByVal lpOverlapped&)
Private Declare Function CreateFile& Lib "kernel32" _
Alias "CreateFileA" _
(ByVal lpFileName$, _
ByVal dwDesiredAccess&, _
ByVal dwShareMode&, _
ByVal lpSecurityAttributes&, _
ByVal dwCreationDisposition&, _
ByVal dwFlagsAndAttributes&, _
ByVal hTemplateFile&)
Private Declare Function CloseHandle& Lib "kernel32" _
(ByVal hObject&)
Private Declare Function FlushFileBuffers& Lib "kernel32" _
(ByVal hFile&)
Public Sub SendSMS(PhoneNumber$, Message$)
Dim s$
TxModem "AT+CMGF=1" & vbCrLf
TxModem "AT+CMGS=" & _
Chr(34) & PhoneNumber & Chr(34) & vbCrLf
Sleep 100
TxModem Message & Chr(26)
End Sub
Function TxModem(cmd$, Optional CommPort$ = "COM4")
Dim openPort As Long, retVal As Long
openPort = CreateFile(CommPort, GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
ByVal 0&, OPEN_EXISTING, 0, 0)
If openPort = -1 Then
MsgBox "Unable to open com port " & CommPort
Exit Function
End If
retVal = WriteBytes(openPort, cmd)
If retVal = 0 Then
MsgBox "Unable to write to modem"
Exit Function
End If
CloseHandle openPort
End Function
Private Function WriteBytes(openPort As Long, _
cmd As String) As Long
Dim a(256) As Byte, retBytes As Long, i As Long
'
For i = 0 To Len(cmd) - 1
a(i) = Asc(Mid(cmd, i + 1, 1))
Next
WriteBytes = WriteFile(openPort, a(0), _
Len(cmd), retBytes, 0)
FlushFileBuffers openPort
End Function
Using that code everything seems to be sent to the phone except the EOF
terminator ctrl-z which is ASCII 26.
Does anyone have any idea why the EOF isn't sent? Or is something else
wrong?
As an aside, I've been trying to locate a serial port monitor that will a)
let my app open COM4: while it is being monitored and b) will work on Win7
64bit home premium. No luck so far..
Thanks in advance
Stuart McCall (retired MS Access & VB6 developer, now hobbyist)