一个与modem通讯的ocx的问题

xjbx
博客专家认证
2003-05-28 05:55:14
我作了一个与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 Function

Public 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
...全文
69 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
laodan 2003-05-30
  • 打赏
  • 举报
回复
DTREnable和RTSEnable为true
xjbx 2003-05-30
  • 打赏
  • 举报
回复
谢谢!
xjbx 2003-05-29
  • 打赏
  • 举报
回复
up
xjbx 2003-05-28
  • 打赏
  • 举报
回复
没人知道么?

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧