《2022年vb串口数据采集及曲线动态显示 .pdf》由会员分享,可在线阅读,更多相关《2022年vb串口数据采集及曲线动态显示 .pdf(4页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、串口数据采集及曲线动态显示这个程序主要用到了串口通信和曲线画图一主要控件Msomm 控件一个, 两个 timer 控件, textbox 控件, combo 控件,picture 控件, command控件二实现的功能能1.串口采集数据2.用曲线动态显示数据3. 显示时间4.鼠标点击读数(确定所点击位置的横纵坐标)三界面四程序Dim T As Variant Dim c As Variant Dim Y As Integer Private Declare Sub Sleep Lib kernel32 (ByVal dwMilliseconds As Long) API,用 sleep实现延时
2、延时函数Function delay(number) Call Sleep(number) DoEvents End Function Private Sub Combo1_Click() 选择端口If Combo1.Text = COM1 Then MSComm1.CommPort = 1 ElseIf Combo1.Text = COM2 Then 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 4 页 - - - - - - - - - MSComm1.CommPo
3、rt = 2 ElseIf Combo1.Text = COM3 Then MSComm1.CommPort = 3 ElseIf Combo1.Text = COM4 Then MSComm1.CommPort = 4 Else MSComm1.CommPort = 5 End If End Sub Private Sub Command3_Click() 停止采集Timer2.Enabled = False MSComm1.PortOpen = False Command2.Caption = 打开串口 End Sub Private Sub Form_Load() 初始化MSComm1.
4、Settings = 9600,N,8,1 MSComm1.InputMode = 0 MSComm1.RThreshold = 1 MSComm1.SThreshold = 0 MSComm1.InBufferSize = 1024 MSComm1.OutBufferSize = 512 MSComm1.InBufferCount = 0 MSComm1.OutBufferCount = 0 Combo1.AddItem COM1 Combo1.AddItem COM2 Combo1.AddItem COM3 Combo1.AddItem COM4 Combo1.AddItem COM5 P
5、icture1.Scale (-100, 100)-(100, -100) Command1.Caption = 采集数据 Command2.Caption = 打开串口 Timer1.Interval = 1000 Timer2.Interval = 1000 Timer2.Enabled = False End Sub Private Sub Command2_Click() If Command2.Caption = 打开串口 Then MSComm1.PortOpen = True Command2.Caption = 关闭串口 ElseIf Command2.Caption = 关闭
6、串口 Then 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 4 页 - - - - - - - - - MSComm1.PortOpen = False Command2.Caption = 打开串口 End If End Sub Private Sub MSComm1_OnComm() 触发事件If MSComm1.InBufferCount 0 Then Text4.Text = MSComm1.Input c = Val(Text4.Text) delay (2
7、0) End If End Sub Private Sub draw() Picture1.FontSize = 15 设置字体大小Picture1.DrawWidth = 1 Picture1.ForeColor = QBColor(12) Picture1.Cls Picture1.Line (-88, -88)-(-88, 88) 纵坐标Picture1.Line (-89, 86)-(-88, 88) 纵坐标箭头及y 值Picture1.Line (-87, 86)-(-88, 88) Picture1.PSet (-86, 88) Picture1.Print y Picture1.
8、Line (-88, 0)-(88, 0) 横坐标Picture1.Line (86, 1)-(88, 0) 横坐标箭头及x 值Picture1.Line (86, -2)-(88, 0) Picture1.PSet (88, -1) Picture1.Print x Picture1.DrawWidth = 4 Picture1.FontSize = 8 For Y = -80 To 80 Step 10 Picture1.PSet (-88, Y) Picture1.Print (Y) Next Y T = -88 Timer2.Enabled = True End Sub Private
9、 Sub Picture1_MouseDOWN(Button As Integer, Shift As Integer, B As Single, A As Single) 光标线读数 Text1.Text = B Text2.Text = A 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 4 页 - - - - - - - - - End Sub Private Sub Command1_Click() 开始采集Call draw End Sub Private Sub Timer2_Timer() 画图Picture1.DrawWidth = 1 Picture1.PSet (T, c), vbGreen T = T + 0.5 If T 88 Then Call draw End If End Sub Private Sub Timer1_Timer() 显示时间Text3.Text = CStr(Time$() End Sub 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 4 页 - - - - - - - - -
限制150内