四.软件设计源程序(波形参见图4、图5)
'定义所用变量
Dim a(11) As Single
Dim d As Integer
Dim n As Integer
Dim m As Integer
Dim Buffer As Variant
'初始化
Private Sub Form_Load()
'一屏显示的12 个数赋初值0
For i = 0 To 11
a(i) = 0
Next i
'定义坐标原点
Form1.Scale (-10, 250)-(220, -15)
'画X、Y 轴
Line (0, 0)-(220, 0)
Line (0, 0)-(0, 250)
'加坐标刻度和数值
CurrentX = 200: CurrentY = 13: Print "X(s)"
CurrentX = 5: CurrentY = 245: Print "Y(V)"
For i = 0 To 220 Step 20
CurrentX = i: CurrentY = 3: Line -(i, 0)
If i 0 Then
CurrentX = -14: CurrentY = i: Print i / 10
CurrentX = 230: CurrentY = i: Line -(0, i)
End If
Next i
'mscomm控件初始化
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0
MSComm1.RThreshold = 0
End Sub
‘接收并显示数据
Private Sub Command1_Click()
’无条件循环接收数据
Do
'接收数据
Do Until MSComm1.InBufferCount = 1
Loop
Buffer = MSComm1.Input
For i = LBound(Buffer) To UBound(Buffer)
d = Buffer(i)
Next i
'判是否第一屏12 个数据,设标志量m
If m 0 Then
CurrentX = -14: CurrentY = i: Print i / 10
CurrentX = 230: CurrentY = i: Line -(0, i)
End If
Next i
'连点成波形
For i = 0 To m - 2
Line (20 * i, 230 * a(i) / 20)-(20 * (i + 1), 230 * a(i + 1) / 20), RGB(240, 0, 0)
Next I
Loop
End Sub