?? frmini.frm
字號:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Frmini
BorderStyle = 1 'Fixed Single
Caption = "系統連接測試"
ClientHeight = 2205
ClientLeft = 3600
ClientTop = 2130
ClientWidth = 3195
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 2205
ScaleWidth = 3195
Begin VB.Timer Timer1
Enabled = 0 'False
Interval = 800
Left = 240
Top = 480
End
Begin VB.Timer Timer2
Enabled = 0 'False
Interval = 15000
Left = 240
Top = 1080
End
Begin VB.CommandButton QuitCommand
Caption = "退出系統"
Height = 375
Left = 840
TabIndex = 4
Top = 1680
Width = 1695
End
Begin VB.CommandButton RunCommand
Caption = "系統連接與初始化"
Height = 375
Left = 840
TabIndex = 3
Top = 1080
Width = 1695
End
Begin VB.ComboBox Combo1
BeginProperty DataFormat
Type = 0
Format = "0"
HaveTrueFalseNull= 0
FirstDayOfWeek = 0
FirstWeekOfYear = 0
LCID = 2052
SubFormatType = 0
EndProperty
Height = 300
ItemData = "Frmini.frx":0000
Left = 840
List = "Frmini.frx":0002
TabIndex = 2
Top = 480
Width = 1695
End
Begin MSCommLib.MSComm Comm1
Left = 2520
Top = 0
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
Begin VB.TextBox Text1
Height = 2055
Left = 4560
TabIndex = 0
Text = "Text1"
Top = 600
Width = 2415
End
Begin VB.Label Label1
Caption = "請選擇您使用的端口:"
Height = 255
Left = 0
TabIndex = 5
Top = 120
Width = 1935
End
Begin VB.Label Label4
Appearance = 0 'Flat
BackColor = &H80000005&
BackStyle = 0 'Transparent
Caption = "Label4"
ForeColor = &H80000008&
Height = 255
Left = 4920
TabIndex = 1
Top = 2640
Width = 1095
End
End
Attribute VB_Name = "Frmini"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim IntNo As Integer
Dim arr_return() As Variant
Private Sub Combo1_Click()
On Error GoTo err
portitem = CInt(Combo1.Text) 'PortItem為全局變量,保存串行端口號
Comm1.CommPort = portitem
Comm1.PortOpen = True '打開端口
Exit Sub
err:
MsgBox "對不起,您選擇的端口已經被打開,請選擇別的端口", vbOKOnly, "通知"
End Sub
Private Sub Form_Load()
Dim aa As Integer
For aa = 1 To 10
Combo1.AddItem aa
Next
IntNo = 0
End Sub
Private Sub RunCommand_Click()
Timer2.Enabled = True ' Timer2控件開始計時
If Comm1.PortOpen = False Then ' 如果端口沒有打開,則打開端口
Comm1.CommPort = portitem
Comm1.PortOpen = True
End If
Comm1.Output = "AT" + Chr(13) + Chr(10) ' 發送AT指令,GSM模塊應當返回"OK"
Timer1.Enabled = True ' Timer1控件開始計時
End Sub
Private Sub QuitCommand_Click()
If Comm1.PortOpen = True Then
Comm1.PortOpen = False
End If
Unload Me
End
End Sub
Private Sub Timer1_Timer()
If Comm1.InBufferCount > 0 Then
Timer1.Enabled = False 'Timer1停止計時
Call getdata '子函數,讀取串口的短消息,存放到arr_return()數組中
'arr_return()為一維數組,存放的是短消息每個字節的16進制數
If IntNo = 0 Then ' IntNo 存放當前刪除的短消息數量,初始值為0
If arr_return(UBound(arr_return) - 4) = Hex(Asc("O")) And _
arr_return(UBound(arr_return) - 3) = Hex(Asc("K")) Then
'判斷是否接收到"ok"字符串,接收到證明連接正常
Comm1.Output = "AT+CMGF=1" + Chr(13) + Chr(10) '使用AT指令設置短消息的格式為TEX 'T類型
IntNo = IntNo + 1
Timer1.Enabled = True
Else
Timer1.Enabled = True
End If
Else
If IntNo = 16 Then '如果IntNo計數達到16,則測試完畢
MsgBox "測試完畢:初始化已經結束!", vbOKOnly, "通知"
Unload Me
MDIForm1.Show '打開主窗口
Else
If arr_return(UBound(arr_return) - 4) = Hex(Asc("O")) And _
arr_return(UBound(arr_return) - 3) = Hex(Asc("K")) Then
If IntNo < 10 Then '按刪除短消息的數量分為兩種:大于10和小于10
Comm1.Output = "AT+CMGD=" + Chr(IntNo + 48) + Chr(13) + Chr(10) '刪除短消息
IntNo = IntNo + 1
Timer1.Enabled = True
Else
Comm1.Output = "AT+CMGD=1" + Chr(IntNo + 38) + Chr(13) + Chr(10)
IntNo = IntNo + 1
Text1.Text = IntNo
Timer1.Enabled = True
End If
End If
End If
End If
End If
End Sub
Public Function getdata()
Dim Count As Integer
Count = 0
Comm1.InputLen = 1 '設置Comm1的InputLen屬性為1,讀取緩沖區時一個一個字節的讀取
While Comm1.InBufferCount > 0 '循環讀取緩沖區中的數據
ReDim Preserve arr_return(Count + 1) '動態定義數組的上限
arr_return(Count) = Hex(Asc(Comm1.Input))
Count = Count + 1
Wend
End Function
Private Sub Timer2_Timer()
Dim IntChk As String
If IntNo <> "16" Then '判斷是否已經刪除完畢:如果IntNo<>"16",則還沒有初始化完畢,出錯!
Timer2.Enabled = False
IntChk = MsgBox("對不起,初始化發生錯誤,是否重新初始化?", vbOKCancel, "通知")
If IntChk = vbOK Then '如果選擇"OK",重新初始化
IntNo = 0
Timer2.Enabled = True
Call RunCommand_Click
Else
Unload Me
MDIForm1.Show
End If
End If
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -