菜鸟求助有关VB串口通信的界面显示问题。我需要将单片机采集到的0--5V电压传送到PC机上,通过界面显示出来,为什么0V和5V时能正常显示,而在0-5V间就不能
0--5V内的电压不能正常显示。以下是我的上位机程序,求哪位高手帮我看看哪有问题Public i As Integer
Private pdate(50) As Variant
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 2 'COM2
MSComm1.Settings = "9600,n,8,1" '1200BSP,无校验位,8个数据位,1个停止位
MSComm1.PortOpen = True '打开串口
MSComm1.InputMode = comInputModeBinary '以2进制形式接收数据
MSComm1.RThreshold = 1 '1个字符引发事件
i = 0
Picture1.Scale (0, 6)-(50, 0)
End Sub
Private Sub MSComm1_OnComm()
Dim indata As Variant
Dim bte(0) As Byte
Select Case MSComm1.CommEvent '通讯事件发生
Case comEvReceive
indata = MSComm1.Input
bte(0) = AscB(indata) '读入数据
test = bte(0) / 255 * 5 '测量值/255*实际量程
ptest = Round(test, 2) '小数点后3位四舍五入
Text2.Text = ptest
pdate(i) = ptest
MSComm1.InBufferCount = 0 '接收缓冲区清空
If i < 50 Then
i = i + 1
Exit Sub
Else
Picture1.Cls
i = 0
Do Until i > 50
If i = 0 Then
Picture1.PSet (i, pdate(i))
Else
Picture1.Line -(i, pdate(i))
End If
i = i + 1
Loop
i = 0
End If
End Select
End Sub
Private Sub Timer1_Timer()
Dim num As Integer
Dim outbte(0) As Byte
outbte(0) = CByte(1) '发送01查询单片机
MSComm1.OutBufferCount = 0 '发送缓冲区清空
MSComm1.Output = outbte() '动态数组
End Sub
找这个人问问
If indata <> Null Then
Option Explicit
Private i As Integer
Private pdate(50) As Variant
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 2 'COM2
MSComm1.Settings = "9600,n,8,1" '1200BSP,无校验位,8个数据位,1个停止位
MSComm1.InputMode = comInputModeBinary '以2进制形式接收数据
MSComm1.RThreshold = 1 '1个字符引发事件
MSComm1.InputLen = 1
MSComm1.PortOpen = True '打开串口
i = 0
Picture1.Scale (0, 6)-(50, 0)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If MSComm1.PortOpen Then MSComm1.PortOpen = False
End Sub
Private Sub MSComm1_OnComm()
Dim intP As Integer
Dim indata As Variant
Dim bte(0 To 49) As Byte
Dim test As Double
Dim ptest As Double
Select Case MSComm1.CommEvent '通讯事件发生
Case comEvReceive
MSComm1.RThreshold = 0
Sleep 20
For intP = 0 To MSComm1.InBufferCount
If indata <> Null Then indata = Null
indata = MSComm1.Input
bte(intP) = Asc(indata(0)) '读入数据
test = bte(intP) / 255 * 5 '测量值/255*实际量程
ptest = Round(test, 2) '小数点后3位四舍五入
Text2.Text = ptest
Next intP
'绘图部分先略去,解决了接受再说
MSComm1.RThreshold = 1
End Select
End Sub
Private Sub Timer1_Timer()
Dim num As Integer
Dim outbte(0) As Byte
outbte(0) = CByte(1) '发送01查询单片机
MSComm1.OutBufferCount = 0 '发送缓冲区清空
MSComm1.Output = outbte '动态数组
End Sub
编译的时候报错,类型不对
是我疏忽了,那就不用判断,直接:indata=null
补充:VB , 资源