如何用vb实现对串口数据的实时曲线绘制

2024-12-26 08:45:30
推荐回答(1个)
回答1:

Option Explicit

Dim DataFromCom As Integer '从串口读过来的实时值
Dim DataFromComLast As Integer '上次的串口值
Dim TimeCount As Integer

Private Sub Form_Load()
PicScale Pic '调整图像框的坐标系
PicMidleLine Pic '在图像框中画一条中线
End Sub
Private Sub PicScale(picX As PictureBox) '调整图像框的坐标系
picX.Scale (0, picX.ScaleHeight)-(picX.ScaleWidth, -picX.ScaleHeight)
End Sub

Private Sub PicMidleLine(picX As PictureBox) '在图像框中画一条中线
picX.Line (0, 0)-(picX.ScaleWidth, 0), vbGreen '画出中线
End Sub

Private Sub DrawRealLine(picX As PictureBox, TimeCountX As Integer, DataFromComX As Integer, DataFromComLastX As Integer)

If TimeCountX - 1 > 0 Then
picX.Line ((TimeCountX - 1) * 10, DataFromComLastX)-(TimeCountX * 10, DataFromComX), vbWhite
End If

End Sub

Private Sub Timer1_Timer()
DataFromComLast = DataFromCom
Randomize
DataFromCom = 3000 * Rnd
TimeCount = TimeCount + 1
DrawRealLine Pic, TimeCount, DataFromCom, DataFromComLast '画出实时的曲线
End Sub