|
这个是串口的,与你的有些不同,可以参考一下。
Option Explicit
Dim NowX As Integer '现在的X轴位置
Dim MaxPlotNo As Long '最长的X轴范围
Dim PreValue As Single '前一个测量值
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'当选择通信端口的Combo控件被选中后激活此事件
'若用户改变通信端口时,关闭通信端口
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmbCOM_Click()
'若通信端口号码和现在的选择一样时就不必理会,直接跳出此子程序
If cmbCOM.ListIndex + 1 = MSComm1.CommPort Then Exit Sub
Timer1.Enabled = False '关闭定时器
TimeDelay 100
If MSComm1.PortOpen Then
MSComm1.PortOpen = False '关闭通信端口
End If
lblMsg.Caption = "已停止检测并关闭通讯端口"
cmdOpenCOM.Enabled = True '允许使用【打开通信端口】按钮
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'单击【结束】按钮后激活此事件
'使用End命令将系统结束
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdEnd_Click()
End
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'单击【打开通信端口】按钮后激活此事件
'将MSComm控件的参数设置好,并打开
'激活【开始检测】按钮
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdOpenCOM_Click()
'判断端口号码是否落在1--16之间
If cmbCOM.ListIndex >= 0 And cmbCOM.ListIndex <= 16 Then
MSComm1.CommPort = cmbCOM.ListIndex + 1
Else
MsgBox "指定通信端口时发生错误!", vbCritical + vbOKOnly, "系统信息"
Exit Sub
End If
'激活错误检测机制
On Error GoTo comErr
MSComm1.Settings = "9600,n,8,1" '设定通信参数
MSComm1.PortOpen = True '打开通信端口
cmdOpenCOM.Enabled = False '将此按钮设为禁用状态
cmdStart.Enabled = True '激活【开始检测】按钮
lblMsg.Caption = "可单击【开始检测】按钮,执行检测的工作。"
Exit Sub
comErr:
MsgBox "打开通信端口时发生错误!请确定通信端口存在且正常。", vbCritical + vbOKOnly, "系统信息"
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'单击【开始检测】按钮后激活此事件
'将定时器激活或关闭,并显示对应的文字在按钮上,以指示用户操作
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdStart_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
cmdStart.Caption = "停止检测"
Else
cmdStart.Caption = "开始检测"
lblMsg.Caption = "已停止检测"
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'窗体的Load事件
'输入图形暂时设为灰色,表示无状态信息进入
'将通讯端口号码及站号填入Combo控件;并默认二者的选项是第一个
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Form_Load()
Dim i%
MaxPlotNo = 100
cmbCOM.Clear
cmbCOM.AddItem "COM1"
cmbCOM.AddItem "COM2"
cmbCOM.AddItem "COM3(USB)"
cmbCOM.AddItem "COM4(USB)"
cmbCOM.AddItem "COM5"
cmbCOM.AddItem "COM6"
cmbCOM.AddItem "COM7"
cmbCOM.AddItem "COM8"
cmbCOM.AddItem "COM9"
cmbCOM.AddItem "COM10"
cmbCOM.AddItem "COM11"
cmbCOM.AddItem "COM12"
cmbCOM.AddItem "COM12"
cmbCOM.AddItem "COM14"
cmbCOM.AddItem "COM15"
cmbCOM.AddItem "COM16"
cmbCOM.ListIndex = 0
cmdStart.Enabled = False
'以下设定绘图范围,(Xmin,YMax)-(XMax,YMin)
picTEMP.Scale (0, 1000)-(MaxPlotNo, 0)
picTEMP.DrawWidth = 2 '使用两个像素宽度的画笔
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'定时器的Timer事件引发后,就不断地执行其中的程序。
'将模拟读值命令送出,再取得返回字符串并判断。
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Timer1_Timer()
Dim Buf$, ValueStr As Single, Pos1%
Buf$ = Buf$ + MSComm1.Input
TimeDelay 2150
ValueStr = Val(Mid(Buf, Pos1 + 1, 7)) '分离出正号以后的数值
lblValue.Caption = Format(ValueStr, "00.00") & "℃" '显示在画面上
If NowX = 0 Then
picTEMP.Cls '清除图形
picTEMP.PSet (0, ValueStr) '设定起点
Else
'以下判断现在的读值是否大于前一次的读值,若是,则以红色绘线
'若否,则以蓝色绘线
If ValueStr > PreValue + 0.01 Then
picTEMP.Line -(NowX, ValueStr), RGB(255, 0, 0) '由上一次的位置画至此点
Else
picTEMP.Line -(NowX, ValueStr), RGB(0, 0, 255) '由上一次的位置画至此点
End If
End If
PreValue = ValueStr
NowX = NowX + 1 '位置加1
If NowX > MaxPlotNo Then NowX = 0 '超过范围则数值归零
End Sub
Declare Function GetTickCount Lib "kernel32" () As Long
Sub TimeDelay(t As Long)
'时间延迟子程序,单位是毫秒(ms)
Dim TT&
TT = GetTickCount()
Do
DoEvents
Loop Until GetTickCount() - TT >= t
End Sub
'等待RS字符串返回,或是时间到达
'Comm是通信控件名称
'RS是欲等待的字符
'DT是最长的等待时间
'正常时返回值是所得的完整字符串,不正常时返回值是空字符串
Function WaitRS(Comm As MSComm, RS As String, DT As Long) As String
Dim Buf$, TT As Long
Buf = ""
TT = GetTickCount
Do
Buf = Buf & Comm.Input
Loop Until InStr(1, Buf, RS) > 0 Or GetTickCount - TT >= DT
If InStr(1, Buf, RS) > 0 Then
WaitRS = Buf
Else
WaitRS = ""
End If
End Function |
|