代码如下(这个程序是通过GSM MODEM 收发短信息,所有需要的函数与变量、类型、常数均已声明。): FORM CODE: Option Explicit Public states1 As Boolean Public states2 As Boolean Public outbuff As String Public inchar As Long Public outchar As Long Public telnum As String Public inbuff As String Public inp As IntegerPrivate Sub Command1_Click() CloseHandle hCommDev End End SubPrivate Sub Form_Activate() Dim str As String Dim i As Long Dim OLapped As OVERLAPPED Dim ErrorCode As Long Dim EvMask As Long OLapped.hEvent = 0 OLapped.Internal = 0 OLapped.InternalHigh = 0 OLapped.offset = 0 OLapped.OffsetHigh = 0 telnum = """13928714621""" Open "d:\my job\comm\output.txt" For Input As #1 Do While Not EOF(1) Line Input #1, outbuff start: RTB1.Text = outbuff + Chr(13) + Chr(10) states1 = WriteFile(hCommDev, outbuff, Len(outbuff), outchar, OLapped) FlushFileBuffers hCommDev If states1 <> True Then ErrorCode = GetLastError MsgBox "写串口错误!错误号:" & ErrorCode End If back: WaitCommEvent hCommDev, EvMask, OLapped If EvMask = EV_RXCHAR Then states2 = ReadFile(hCommDev, inbuff, 100, inchar, OLapped) If states2 <> True Then ErrorCode = GetLastError MsgBox "读串口错误!错误号:" & ErrorCode End If str = inbuff i = InStr(1, str, "OK", vbTextCompare) inp = InStr(1, str, ">", vbTextCompare) If i = 0 And inp = 0 Then GoTo back Else RTB1.Text = RTB1.Text + Left(inbuff, inchar) + Chr(13) + Chr(10) End If Else GoTo start End If Loop Close #1 End SubPrivate Sub Form_Load() Dim cDcb As DCB Dim CTimeOuts As COMMTIMEOUTS Dim Secu As SECURITY_ATTRIBUTES Secu.bInheritHandle = 0 Secu.lpSecurityDescriptor = 0 Secu.nLength = 12 hCommDev = CreateFile("COM2", GENERIC_READ Or GENERIC_WRITE, 0&, Secu, OPEN_EXISTING, 0&, 0&) If (hCommDev = -1) Then MsgBox "串口初始化错误!" End If BuildCommDCB "COM2:19200,n,8,1", cDcb SetCommState hCommDev, cDcb SetCommMask hCommDev, EV_RXCHAR Or EV_TXEMPTY CTimeOuts.ReadIntervalTimeout = MAXDWORD CTimeOuts.ReadTotalTimeoutMultiplier = 0 CTimeOuts.ReadTotalTimeoutConstant = 0 CTimeOuts.WriteTotalTimeoutMultiplier = 0 CTimeOuts.WriteTotalTimeoutConstant = 1000 SetCommTimeouts hCommDev, CTimeOuts PurgeComm hCommDev, PURGE_TXCLEAR PurgeComm hCommDev, PURGE_RXCLEAR End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) Dim k As Integer k = InStr(1, inbuff, ">", vbTextCompare) If inp <> 0 Then If KeyAscii = 13 Then 'Do While (Text1.Text <> buff) RTB1.Text = RTB1.Text + Text1.Text outbuff = Text1.Text & Chr(&H1A) WriteFile hCommDev, outbuff, Len(outchar), outchar, Null 'Loop 'Else ' Text1.Refresh ' MSComm1.Output = Text1.Text & vbCrLf ' buff = Text1.Text End If End If End Sub Public Sub Delay(layout As Single) Dim time As Single time = Timer Do While (Timer < time + layout) Loop End Sub
FORM CODE:
Option Explicit
Public states1 As Boolean
Public states2 As Boolean
Public outbuff As String
Public inchar As Long
Public outchar As Long
Public telnum As String
Public inbuff As String
Public inp As IntegerPrivate Sub Command1_Click()
CloseHandle hCommDev
End
End SubPrivate Sub Form_Activate()
Dim str As String
Dim i As Long
Dim OLapped As OVERLAPPED
Dim ErrorCode As Long
Dim EvMask As Long
OLapped.hEvent = 0
OLapped.Internal = 0
OLapped.InternalHigh = 0
OLapped.offset = 0
OLapped.OffsetHigh = 0
telnum = """13928714621"""
Open "d:\my job\comm\output.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, outbuff
start:
RTB1.Text = outbuff + Chr(13) + Chr(10)
states1 = WriteFile(hCommDev, outbuff, Len(outbuff), outchar, OLapped)
FlushFileBuffers hCommDev
If states1 <> True Then
ErrorCode = GetLastError
MsgBox "写串口错误!错误号:" & ErrorCode
End If
back:
WaitCommEvent hCommDev, EvMask, OLapped
If EvMask = EV_RXCHAR Then
states2 = ReadFile(hCommDev, inbuff, 100, inchar, OLapped)
If states2 <> True Then
ErrorCode = GetLastError
MsgBox "读串口错误!错误号:" & ErrorCode
End If
str = inbuff
i = InStr(1, str, "OK", vbTextCompare)
inp = InStr(1, str, ">", vbTextCompare)
If i = 0 And inp = 0 Then
GoTo back
Else
RTB1.Text = RTB1.Text + Left(inbuff, inchar) + Chr(13) + Chr(10)
End If
Else
GoTo start
End If
Loop
Close #1
End SubPrivate Sub Form_Load()
Dim cDcb As DCB
Dim CTimeOuts As COMMTIMEOUTS
Dim Secu As SECURITY_ATTRIBUTES
Secu.bInheritHandle = 0
Secu.lpSecurityDescriptor = 0
Secu.nLength = 12
hCommDev = CreateFile("COM2", GENERIC_READ Or GENERIC_WRITE, 0&, Secu, OPEN_EXISTING, 0&, 0&)
If (hCommDev = -1) Then
MsgBox "串口初始化错误!"
End If
BuildCommDCB "COM2:19200,n,8,1", cDcb
SetCommState hCommDev, cDcb
SetCommMask hCommDev, EV_RXCHAR Or EV_TXEMPTY
CTimeOuts.ReadIntervalTimeout = MAXDWORD
CTimeOuts.ReadTotalTimeoutMultiplier = 0
CTimeOuts.ReadTotalTimeoutConstant = 0
CTimeOuts.WriteTotalTimeoutMultiplier = 0
CTimeOuts.WriteTotalTimeoutConstant = 1000
SetCommTimeouts hCommDev, CTimeOuts
PurgeComm hCommDev, PURGE_TXCLEAR
PurgeComm hCommDev, PURGE_RXCLEAR
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
Dim k As Integer
k = InStr(1, inbuff, ">", vbTextCompare)
If inp <> 0 Then
If KeyAscii = 13 Then
'Do While (Text1.Text <> buff)
RTB1.Text = RTB1.Text + Text1.Text
outbuff = Text1.Text & Chr(&H1A)
WriteFile hCommDev, outbuff, Len(outchar), outchar, Null
'Loop
'Else
' Text1.Refresh
' MSComm1.Output = Text1.Text & vbCrLf
' buff = Text1.Text
End If
End If
End Sub
Public Sub Delay(layout As Single)
Dim time As Single
time = Timer
Do While (Timer < time + layout)
Loop
End Sub