?? frmcheck.frm
字號:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form frmCheck
Caption = "串口檢測"
ClientHeight = 4740
ClientLeft = 60
ClientTop = 345
ClientWidth = 4710
LinkTopic = "Form1"
ScaleHeight = 4740
ScaleWidth = 4710
StartUpPosition = 3 '窗口缺省
Begin VB.Timer tmrTimer2
Left = 3960
Top = 3120
End
Begin VB.Timer tmrTimer1
Left = 3960
Top = 2400
End
Begin VB.Frame Frame3
Height = 1215
Left = 720
TabIndex = 9
Top = 2280
Width = 2775
Begin VB.Label Label4
Caption = "0"
Height = 255
Left = 1200
TabIndex = 13
Top = 840
Width = 975
End
Begin VB.Label Label3
Caption = "0"
Height = 255
Left = 1200
TabIndex = 12
Top = 360
Width = 975
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "接收次數(shù):"
Height = 180
Left = 240
TabIndex = 11
Top = 840
Width = 900
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "發(fā)送次數(shù):"
Height = 180
Left = 240
TabIndex = 10
Top = 360
Width = 900
End
End
Begin VB.Frame Frame2
Caption = "波特率選擇"
Height = 1695
Left = 2520
TabIndex = 5
Top = 360
Width = 1215
Begin VB.OptionButton Boud3
Caption = "1200"
Height = 255
Left = 240
TabIndex = 8
Top = 1080
Width = 735
End
Begin VB.OptionButton Boud2
Caption = "4800"
Height = 255
Left = 240
TabIndex = 7
Top = 720
Width = 735
End
Begin VB.OptionButton Boud1
Caption = "9600"
Height = 255
Left = 240
TabIndex = 6
Top = 360
Width = 735
End
End
Begin MSCommLib.MSComm MSComm1
Left = 3960
Top = 600
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
RThreshold = 1
End
Begin VB.Frame Frame1
Caption = "端口選擇"
Height = 1695
Left = 480
TabIndex = 2
Top = 360
Width = 1215
Begin VB.OptionButton COM2
Caption = "COM2"
Height = 375
Left = 240
TabIndex = 4
Top = 960
Width = 735
End
Begin VB.OptionButton COM1
Caption = "COM1"
Height = 255
Left = 240
TabIndex = 3
Top = 480
Width = 735
End
End
Begin VB.CommandButton cmdReturn
Caption = "返 回"
Height = 615
Left = 2520
TabIndex = 1
Top = 3840
Width = 1335
End
Begin VB.CommandButton cmdStart
Caption = "開始測試"
Height = 615
Left = 480
TabIndex = 0
Top = 3840
Width = 1335
End
End
Attribute VB_Name = "frmCheck"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim OK, Start As Boolean 'OK 為通信正常標(biāo)志位, Start 為開始測試標(biāo)志位
Private Sub Boud1_Click()
sBoud = "9600,n,8,1"
MSComm1.Settings = sBoud
End Sub
Private Sub Boud2_Click()
sBoud = "4800,n,8,1"
MSComm1.Settings = sBoud
End Sub
Private Sub Boud3_Click()
sBoud = "1200,n,8,1"
MSComm1.Settings = sBoud
End Sub
Private Sub cmdReturn_Click()
If Start Then '判斷是否已經(jīng)按下測試鍵
Start = False '若已經(jīng)按下則執(zhí)行停止鍵的功能,測試鍵標(biāo)志清零
cmdStart.Enabled = True '激活測試鍵按鈕
cmdReturn.Caption = "返 回" '停止標(biāo)簽改回返回
Else
If MSComm1.PortOpen Then MSComm1.PortOpen = False '若沒有按下測試鍵,則執(zhí)行返回鍵功能
frmCheck.Hide '隱藏串口檢測窗口
frmMain.Show '顯示主窗口
End If
End Sub
Private Sub cmdStart_Click()
Label3.Caption = 0 '發(fā)送次數(shù)清零
Label4.Caption = 0 '接收次數(shù)清零
If MSComm1.PortOpen Then
MsgBox sCom + "正在被其它程序使用,請更換其它串口", 0, "串行通信" '???????
Else
OK = True '通信正常標(biāo)志位置 1
Start = True '測試鍵標(biāo)志置 1
cmdReturn.Caption = "停 止" '返回鍵按鈕標(biāo)簽改為停止
cmdStart.Enabled = False '測試鍵按鈕無效
MSComm1.PortOpen = True '打開端口
MSComm1.Output = "1" '發(fā)送測試串口指令 1
tmrTimer2.Interval = 100 '設(shè)置定時(shí)器2 的定時(shí)間隔為100ms
tmrTimer2.Enabled = True '開定時(shí)器2
While Not OK
Wend '等待定時(shí)器2 定時(shí)結(jié)束
tmrTimer2.Interval = 1000 '設(shè)置定時(shí)器2 的定時(shí)間隔為1000ms
OK = True '標(biāo)志置 1
tmrTimer1.Enabled = True '開定時(shí)器1
End If
End Sub
Private Sub COM1_Click()
MSComm1.CommPort = 1
sPort = 1
sCom = "COM1"
End Sub
Private Sub COM2_Click()
MSComm1.CommPort = 2
sPort = 2
sCom = "COM2"
End Sub
Private Sub Form_Load()
Dim buf$ '用于暫存接收字符
Dim Time%
OK = True
Start = False
Label3.Caption = 0 '發(fā)送次數(shù)清零
Label4.Caption = 0 '接收次數(shù)清零
tmrTimer1.Interval = 10 '計(jì)時(shí)器1定時(shí)10ms
tmrTimer1.Enabled = False '關(guān)計(jì)時(shí)器1
tmrTimer2.Interval = 1000 '計(jì)時(shí)器2定時(shí)1000ms
tmrTimer2.Enabled = False '關(guān)計(jì)時(shí)器2
COM1.Value = True '默認(rèn)使用 COM1口
Boud3.Value = True '默認(rèn)使用 1200波特
sCom = "COM1" '串口變量指向 COM1
MSComm1.CommPort = sPort '使用 COM1
MSComm1.Settings = sBoud '使用 1200波特,無校驗(yàn),8位數(shù)據(jù)位,1位停止位
MSComm1.InputLen = 0 '讀取接收緩沖區(qū)的全部字符
MSComm1.InputMode = comInputModeText 'Input讀取的數(shù)據(jù)是文本
End Sub
Private Sub tmrTimer1_Timer()
tmrTimer1.Enabled = False '關(guān)定時(shí)器1
MSComm1.Output = "A" '發(fā)送測試字符'A'
Label3.Caption = Label3.Caption + 1 '發(fā)送次數(shù)自加 1
Do
tmrTimer2.Enabled = True '開定時(shí)器2
DoEvents '接受其它事件的響應(yīng),主要是定時(shí)器2 的響應(yīng)
Loop While ((MSComm1.InBufferCount = 0) And OK) '若接收到數(shù)據(jù)或定時(shí)器2響應(yīng)則跳出循環(huán),若定時(shí)器2響應(yīng)則說明串口不通
tmrTimer2.Enabled = False '關(guān)定時(shí)器2
If MSComm1.Input = "A" Then
Label4.Caption = Label4.Caption + 1 '若接收到的字符是"A",則通信正常,接收次數(shù)自加 1
Else
OK = False '若接收到的字符不是"A",則通信不正常,可能是波特率不匹配,清標(biāo)志位
End If
If Not OK Then
MsgBox sCom + "是否連接?" + Chr(32) + "波特率是否正確?", 0, "串行通信" '若通信不正常,則顯示提示框
Start = False '測試標(biāo)志位清零
cmdStart.Enabled = True '顯示測試鍵按鈕
cmdReturn.Caption = "返 回" '停止標(biāo)簽改為返回
If MSComm1.PortOpen Then MSComm1.PortOpen = False '如果串口被打開則關(guān)閉
GoTo ToEnd1 '跳過開定時(shí)器1 的操作
End If
If (Label4.Caption >= 100) Or (Not Start) Then
MsgBox sCom + "工作正常!", 0, "串行通信" '若已經(jīng)測試了100次(或按下停止鍵按鈕),則顯示通信成功提示框
MSComm1.Output = "B" '發(fā)送測試終止指令 "B"
cmdStart.Enabled = True '激活測試鍵按鈕
Start = False '清測試標(biāo)志位
cmdReturn.Caption = "返 回" '停止標(biāo)簽改為返回
If MSComm1.PortOpen Then MSComm1.PortOpen = False '如果串口被打開則關(guān)閉
GoTo ToEnd1 '跳過開定時(shí)器1 的操作
End If
tmrTimer1.Enabled = True '開定時(shí)器1
ToEnd1:
End Sub
Private Sub tmrTimer2_Timer()
tmrTimer2.Enabled = False
OK = False
End Sub
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -