我作了一个与modem通讯的ocx控件,在一个程序里加入这个ocx,调用控件“InitializeModem”方法(发出“at”指令),却无法得到返回的值(strtemp的值为空),我用超级终端看见实际上已经返回了一个“ok”。如果我把下面的程序不作成ocx控件,而直接作成一个exe程序的话,执行起来就问题。请问这是什么原因造成的?谢谢。
代码如下:Option Explicit
Public bOK As Boolean
Const prex = "0891"
Const midx = "11000D91"
Const sufx = "000800"'初始化modem
Public Function InitializeModem(port As Integer) As Integer
Dim strtemp As String
Dim X As Integer
bOK = False
If Not MSComm1.PortOpen Then
MSComm1.CommPort = port
On Error Resume Next
MSComm1.PortOpen = True
If Err.number = 8005 Then
'端口无法打开,请检查是否被占用
InitializeModem = 1
Exit Function
End If
End If
ModemSendLine ("AT" + vbCr)
strtemp = ModemWaitResponse(2)
Select Case strtemp
Case "OK":
InitializeModem = 0
bOK = True
Case "ERROR":
'端口初始化失败
InitializeModem = 2
Exit Function
Case Else
InitializeModem = 3
'GSM modem 没有响应,请检查数据线和电源线
Exit Function
End Select
'ModemSendLine ("AT+CGSN")
End Function’向modem发at指令
Public Sub ModemSendLine(strText As String)
' send a string of text to the modem, with a CR and LF at the end.
MSComm1.Output = strText + vbCrLf
End Sub’发出指令后返回的消息
Public Function ModemWaitResponse(seconds As Integer) As String
' Wait for a modem response. Timeout after "seconds" has expired.
' Returns the response string if successful
' returns "" if not successful
Dim TimeNow As Date
Dim TimeEnd As Date
Dim strInput As String
Dim strtemp As String
Dim Buffer As Variant
TimeNow = Now()
TimeEnd = DateAdd("s", seconds, Now())
strInput = ""
Do While (Now() < TimeEnd)
DoEvents
If MSComm1.InBufferCount > 0 Then
Buffer = MSComm1.Input
strInput = strInput + Buffer
' Check for a recognized modem command response
strtemp = CheckForMessage(strInput) If strtemp <> "" Then
ModemWaitResponse = strtemp
ClearModemBuffer
Exit Function
End If
DoEvents
End If
Loop
' ModemWaitResponse = ""
End Function’处理获得返回的消息
Public Function CheckForMessage(strtemp As String) As String
' Check for a recognized modem response message within a string
' Return the response message if found, otherwise return a blank string
Dim ResponseArray
Dim Count As Integer
Dim X As Integer
ResponseArray = Array("OK", "CONNECT", "NO CARRIER", "ERROR", "BUSY", "NO ANSWER", "NO DIAL", "RING", ">")
Count = UBound(ResponseArray)
For X = 0 To Count
If InStr(UCase(strtemp), ResponseArray(X)) Then
DoEvents
CheckForMessage = ResponseArray(X)
' ShowData txtTerm, (ResponseArray(X) & vbCrLf)
' MsgBox CheckForMessage
Exit Function
End If
Next X
End FunctionPublic Sub ClearModemBuffer()
' Make sure all information has been received from the modem. Display any
' remaining characters.
Dim Buffer As Variant
Dim X As Integer ' clear out the rest of the buffer.
If MSComm1.InBufferCount > 0 Then
For X = 1 To MSComm1.InBufferCount
Buffer = MSComm1.Input
Next X
End If
End Sub
代码如下:Option Explicit
Public bOK As Boolean
Const prex = "0891"
Const midx = "11000D91"
Const sufx = "000800"'初始化modem
Public Function InitializeModem(port As Integer) As Integer
Dim strtemp As String
Dim X As Integer
bOK = False
If Not MSComm1.PortOpen Then
MSComm1.CommPort = port
On Error Resume Next
MSComm1.PortOpen = True
If Err.number = 8005 Then
'端口无法打开,请检查是否被占用
InitializeModem = 1
Exit Function
End If
End If
ModemSendLine ("AT" + vbCr)
strtemp = ModemWaitResponse(2)
Select Case strtemp
Case "OK":
InitializeModem = 0
bOK = True
Case "ERROR":
'端口初始化失败
InitializeModem = 2
Exit Function
Case Else
InitializeModem = 3
'GSM modem 没有响应,请检查数据线和电源线
Exit Function
End Select
'ModemSendLine ("AT+CGSN")
End Function’向modem发at指令
Public Sub ModemSendLine(strText As String)
' send a string of text to the modem, with a CR and LF at the end.
MSComm1.Output = strText + vbCrLf
End Sub’发出指令后返回的消息
Public Function ModemWaitResponse(seconds As Integer) As String
' Wait for a modem response. Timeout after "seconds" has expired.
' Returns the response string if successful
' returns "" if not successful
Dim TimeNow As Date
Dim TimeEnd As Date
Dim strInput As String
Dim strtemp As String
Dim Buffer As Variant
TimeNow = Now()
TimeEnd = DateAdd("s", seconds, Now())
strInput = ""
Do While (Now() < TimeEnd)
DoEvents
If MSComm1.InBufferCount > 0 Then
Buffer = MSComm1.Input
strInput = strInput + Buffer
' Check for a recognized modem command response
strtemp = CheckForMessage(strInput) If strtemp <> "" Then
ModemWaitResponse = strtemp
ClearModemBuffer
Exit Function
End If
DoEvents
End If
Loop
' ModemWaitResponse = ""
End Function’处理获得返回的消息
Public Function CheckForMessage(strtemp As String) As String
' Check for a recognized modem response message within a string
' Return the response message if found, otherwise return a blank string
Dim ResponseArray
Dim Count As Integer
Dim X As Integer
ResponseArray = Array("OK", "CONNECT", "NO CARRIER", "ERROR", "BUSY", "NO ANSWER", "NO DIAL", "RING", ">")
Count = UBound(ResponseArray)
For X = 0 To Count
If InStr(UCase(strtemp), ResponseArray(X)) Then
DoEvents
CheckForMessage = ResponseArray(X)
' ShowData txtTerm, (ResponseArray(X) & vbCrLf)
' MsgBox CheckForMessage
Exit Function
End If
Next X
End FunctionPublic Sub ClearModemBuffer()
' Make sure all information has been received from the modem. Display any
' remaining characters.
Dim Buffer As Variant
Dim X As Integer ' clear out the rest of the buffer.
If MSComm1.InBufferCount > 0 Then
For X = 1 To MSComm1.InBufferCount
Buffer = MSComm1.Input
Next X
End If
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货