VB串口接收数据画波形

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Dim shuju(102) As Byte '画波形缓存数组
Dim n As Integer '接收指针
Dim key As Byte '接收方式选择开关
Const pi = 3.14159
Dim a '画波形函数变量
Dim qing As String
Dim shu As Byte '定义全局变量,二进制接收数组
Dim ji_shu As Integer


Private Sub clean_Click()
ji_shu = 0
End Sub

Private Sub Command6_Click() '断开连接
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
xianshi.Caption = "串口未连接!!!"
xianshi.ForeColor = vbRed
End Sub

Private Sub exit_Click() '退出程序
'先断开端口再退出程序
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
Unload Me
End
End Sub

Private Sub Form_Load() '初始化界面*************************************
xianshi.Caption = "串口未连接!!!"
xianshi.ForeColor = vbRed
Picture1.Cls
Picture1.BackColor = vbBlack
MSComm1.InputLen = 0 '接收一次则清空**********************很重要的一句***************************************
MSComm1.RThreshold = 100 '1 个字符产生接收事件**********************很重要的一句***************************************
ji_shu = 0

End Sub

Private Sub lian_jie_Click() '连接串口*******************************************
On Error GoTo err

mPort = Val(Combo1.Text)

MSComm1.Settings = Trim(Combo2.Text) & ",n,8,1"
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
xianshi.Caption = "串口已经连接"
xianshi.ForeColor = vbBlue

err:
If err = 8002 Then
MsgBox ("无效的端口号!请检查端口连接!")

End If
End Sub



Public Function hua() '画数组shuju(100)的二进制数的曲线*************************************************
'首先清除picture1内的图形
Picture1.Cls
Picture1.BackColor = vbBlack
'Scale方法设定用户坐标系,坐标原点在Picture1中心
Picture1.ScaleMode = 0
Picture1.ScaleMode = 3
Picture1.Scale (-10, 10)-(10, -10) '用户重新定义坐标为(-10,10)-(10,-10)

'设置绘线宽度
Picture1.DrawWidth = 1
'绘坐标系的X轴及箭头线??
Picture1.Line (-10, 0)-(10, 0), vbBlue '画线
Picture1.Line (9, 0.5)-(10, 0), vbBlue '画线上半箭头
Picture1.Line -(9, -0.5), vbBlue
Picture1.ForeColor = vbGreen
Picture1.Print "X" '在显示完后显示X


'绘坐标系的Y轴及箭头线
Picture1.Line (0, 10)-(0, -10), vbBlue
Picture1.Line (0.5, 9)-(0, 10), vbBlue
Picture1.Line -(-0.5, 9), vbBlue
Picture1.Print "Y"
'指定位置显示原点O
Picture1.CurrentX = 0.5
Picture1.CurrentY = -0.5
Picture1.Print "O"

'重设绘线宽度
Picture1.DrawWidth = 1
'Picture1.PSet (a, Sin(a) * 5), vbRed '难道这是画点程序
Picture1.CurrentX = -10 '指定显示点
Picture1.CurrentY = 0
'用For循环绘点,使其按正弦规律变化。步长值很

小,使其形成动画效果
For a = 0 To 100 '难道类似于a=a+pi
Picture1.Line -(0.21 * a - 10, shuju(a) / 15 - 8), vbRed '这样速度更快
'a = a + 1 '获得随机数函数Int((100 - 0 + 1) * Rnd + 0) / 10)

Next
'指定位置显示描述文字
Picture1.CurrentX = pi / 2
Picture1.CurrentY = -7
Picture1.ForeColor = vbBlack
Picture1.Print "正弦曲线示意"
End Function '******************************************************************************************


Private Sub MSComm1_OnComm() '收到触发函数
Dim fan As Variant
Dim i As Integer
Dim lenth As Integer
lenth = MSComm1.InBufferCount '一般最好加上这么一句,否则容易出错,下标过界
fan = MSComm1.Input
If lenth >= 100 Then

For i = 0 To 99
shuju(i) = fan(i)
Next
End If

MSComm1.RThreshold = 100 '8个字节触发一个接收事件
MSComm1.InputLen = 0 '写一次m=mscomm1.input则清空缓冲区
hua
ji_shu = ji_shu + 1
If ji_shu = 60000 Then
ji_shu = 0
End If

xian.Caption = Str(ji_shu)
End Sub





相关文档
最新文档