回复 2楼 风吹过b
Dim px(5000) As Single, py(5000) As Single
Dim ab(5) As Byte
Public DataFromCom As Integer
Public DataFromComLast As Integer '上次的串口值
Public Data As Single
Public TimeCount As Integer
Public Buffer As Variant
Public w As Integer
Public k As Integer
Public j As Integer
Private Sub Command2_Click()
Form2.Show vbModal
End Sub
Private Sub Form_activate() '定义坐标系`
Picture1.Scale (-30, 10500)-(760, -1000)
Picture1.Line (750, 0)-(-30, 0), vbBlack
Picture1.Line (0, 10000)-(0, -1000)
Dim i As Integer
'标x轴刻度
For i = 0 To 750 Step 30
Picture1.Line (i, 10000)-(i, 0)
Picture1.CurrentX = i - 16: Picture1.CurrentY = -20: Picture1.Print i
Next i
'标y轴刻度
For i = 0 To 10000 Step 1000
Picture1.Line (750, i)-(0, i)
Next i
End Sub
Private Sub Command1_Click()
With MSComm1
If Command1.Caption = "打开" Then '判断通信口是否打开
MSComm1.PortOpen = True '打开通信口
Timer1.Enabled = True
Command1.Caption = "关闭"
FileName = Year(Date) & "-" & Month(Date) & "-" & Day(Date) & "-" & Hour(Time) & "-" & Minute(Time) & ".txt"
Open "c:\采集数据\" & FileName For Append As #1
Else
MSComm1.PortOpen = False
Command1.Caption = "打开"
Timer1.Enabled = False
Close #1
TimeCount = 0
If Err Then '错误处理
MsgBox "串口通信无效"
Text1.Text = ""
End If
End If
End With
End Sub
Private Sub MSComm1_OnComm()
With MSComm1
Select Case
Case comEvReceive '是接收事件
Buffer = MSComm1.Input '读取一个字节
ab(1) = Buffer(0) '转换字节数据类型数组
If ab(1) = &HFF Then '判断是否为数据开始标志
MSComm1.RThreshold = 0 '关闭OnComm 事件接收
Do
DoEvents
Loop Until MSComm1.InBufferCount >= 4
Buffer = MSComm1.Input
ab(2) = Buffer(0) - 48
Buffer = MSComm1.Input
ab(3) = Buffer(0) - 48
Buffer = MSComm1.Input
ab(4) = Buffer(0) - 48
Buffer = MSComm1.Input
ab(5) = Buffer(0) - 48
b1 = CInt(ab(2))
b2 = CInt(ab(3))
b3 = CInt(ab(4))
b4 = CInt(ab(5))
Data = 20 * (b1 + b2 / 10 + b3 / 100 + b4 / 1000)
DataFromCom = 100 * Data
MSComm1.RThreshold = 1 '打开OnComm 事件接收
End If
Text1.Text = Data
Case Else
End Select
End With
End Sub
Private Sub Timer1_Timer()
Picture1.Cls
DataFromComLast = DataFromCom
Randomize
Picture1.ScaleMode = 0
Picture1.Scale (-30, 10500)-(760, -1000)
Picture1.Line (750, 0)-(-30, 0), vbBlack
Picture1.Line (0, 10000)-(0, -1000)
Dim i As Integer
'标x轴刻度
For i = 0 To 750 Step 30
Picture1.Line (i, 10000)-(i, 0)
Picture1.CurrentX = i - 16: Picture1.CurrentY = -200: Picture1.Print i
Next i
'标y轴刻度
For i = 0 To 10000 Step 1000
Picture1.Line (750, i)-(0, i)
Next i
If TimeCount >= 0 And TimeCount <= 750 Then
For u = TimeCount To TimeCount
x = u
px(u) = x '给各数据点横坐标赋值
py(u) = DataFromCom
'给各数据点纵坐标赋值
Next u
Else
For t = 1 To 749
py(t) = py(t + 1)
px(t) = t
Next t
py(750) = DataFromCom
End If
k = 1
For t = 2 To 749
If (py(t - 1) < py(t) And py(t - 2) > py(t - 1)) Or (py(t - 1) > py(t) And py(t - 2) < py(t - 1)) Then
k = k + 1
End If
Next t
TimeCount = TimeCount + 1
DrawRealLine Picture1, TimeCount, DataFromCom, DataFromComLast '画出实时的曲线
Print #1, TimeCount / 2, Data
Text2.Text = k
End Sub
Private Sub DrawRealLine(picX As PictureBox, TimeCountX As Integer, DataFromComX As Integer, DataFromComLastX As Integer)
If TimeCountX - 1 > 0 And TimeCountX - 1 < 749 Then
For i = 1 To TimeCountX
picX.Line -(px(i - 1), py(i - 1)), vbRed
Next i
Else
For i = 1 To 750
picX.Line -(px(i - 1), py(i - 1)), vbRed
Next i
End If
End Sub