VB的一个问题求指导!
各位,帮忙看看这个程序,出现下图所示错误:通信端口出错,首先我已经排除了硬件相关的可能,保证路是通的,就是不知道哪里出错导致了问题!!!!Private Sub Get_data_Click()
If press = 1 Then
MsgBox "正在通信!慢点!!", vbExclamation + vbOKOnly, "通信信息"
press = 0
Exit Sub
End If
press = 1
Frame2.Visible = True
Frame1.Enabled = False
Frame3.Enabled = True
Frame4.Enabled = True
Frame2.Enabled = True
Frame5.Enabled = True
Frame4.Caption = "取参数"
'Frame5.Visible = False
Out_ACV = 0
sent1(2) = Combo6.ListIndex '改变机号
For i = 0 To 6 '计算效验
Out_ACV = Out_ACV + sent1(i + 1)
Next i
sent1(8) = Int((Out_ACV Mod 65536) / 256) '修改校验?????高位计算时不知道为何是02H?????/
sent1(9) = Out_ACV Mod 256
' For i = 0 To 10 '计算效验
' Print Hex(sent1(i))
'Next i
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
MSComm1.CommPort = Combo1.ListIndex + 1 '选择串口初始化
MSComm1.Settings = Str$(1200 * (2 ^ Combo2.ListIndex)) & "," & "n,8,1" '串口格式设置
On Error GoTo ren
MSComm1.PortOpen = True
GoTo rrn
ren:
MsgBox "通信端口设置错!!", vbExclamation + vbOKOnly, "通信信息"
press = 0
Exit Sub
Resume Next
rrn:
DS = GetTickCount
Delay = 4000
MSComm1.Output = sent1 '发出命令
MSComm1.InBufferCount = 0
Do While MSComm1.InBufferCount < 58
If GetTickCount - DS > Delay Then
StatusBar1.Panels(1).Text = "读运行参数失败!!"
MsgBox "通信超时!!", vbExclamation + vbOKOnly, "通信信息" '提示窗口
MSComm1.PortOpen = False '关闭口并退出
Timer1.Enabled = False '关闭口定时取参数
Timer2.Enabled = False
Frame3.Enabled = False
Frame4.Enabled = False
press = 0
'Exit Do
Exit Sub
Else
DoEvents
StatusBar1.Panels(1).Text = "正在读取运行参数!!"
End If
Loop
get1 = MSComm1.Input '读取当前设备运行参数给get1()
'****************************************************************************************************
MSComm1.OutBufferCount = 0
MSComm1.Output = sent4 '先获取参数
get4 = MSComm1.Input
MSComm1.PortOpen = False '通信结束关串口
StatusBar1.Panels(1).Text = " OK !!"
'************************************************************************************************************
Text1(24).Text = Str((Val(get4(7)) * 256 + Val(get4(8)))) '获取参数显示
For i = 0 To 12 '参数部分
Text1(i + 25).Text = Str((Val(get4(2 * i + 11)) * 256 + Val(get4(2 * i + 12))))
Next i
For i = 0 To 15 '16个电压
Text1(i + 40).Text = Str((Val(get4(2 * i + 37)) * 256 + Val(get4(2 * i + 38))))
Next i
Text1(38).Text = Str((Val(get4(69)))) '地址
Text1(39).Text = Str((Val(get4(70)))) '串数
For i = 0 To 31
Text1(i + 56).Text = Text1(i + 24).Text
Next i
For i = 24 To 55
Text1(i).Locked = True
Next i
'************************************************************************************************************
Text1(0).Text = Str((Val(get1(7)) * 256 + Val(get1(8))) / 1000)
For i = 0 To 14
Text1(i + 1).Text = Str(((Val(get1(9 + i * 2)) * 256 + Val(get1(10 + i * 2))) - (Val(get1(7 + i * 2)) * 256 + Val(get1(8 + i * 2)))) / 1000)
Next i
'For i = 0 To 14
'Text1(i + 1).Text = Str((Val(get1(9 + i * 2)) * 256 + Val(get1(10 + i * 2))) / 1000)
'Next i
'For i = 0 To 3
'Text1(i).Text = Str((Val(get1(2 * i + 7)) * 256 + Val(get1(2 * i + 8))) * 18125 / 4096 / 1000)
'Next i
'For i = 0 To 3
'Text1(i + 4).Text = Str((Val(get1(2 * i + 15)) * 256 + Val(get1(2 * i + 16))) * 36875 / 4096 / 1000)
'Next i
'For i = 0 To 3
'Text1(i + 8).Text = Str((Val(get1(2 * i + 23)) * 256 + Val(get1(2 * i + 24))) * 73333 / 4096 / 1000)
'Next i
'For i = 0 To 3
'Text1(i + 12).Text = Str((Val(get1(2 * i + 31)) * 256 + Val(get1(2 * i + 32))) * 73861 / 4096 / 1000)
'Next i
Text1(16).Text = Str((Val(get1(39)) * 256 + Val(get1(40)))) / 100 ' * 41.667 / 4096)
Text1(17).Text = Str((Val(get1(41)) * 256 + Val(get1(42)))) / 100 '* 41.667 / 4096)
Text1(18).Text = Str(Val(get1(43)) - 50) '* 250 / 4096) Val(get1(44)) * 256 +
'Text1(19).Text = Str(((Val(get1(45)) * 256 + Val(get1(46))) * 2500 / 4095) - 986) * 1 / 3.35 + 10
Text1(19).Text = Str(Val(get1(44)) - 50) 'Val(get1(46)) * 256) +
Text1(20).Text = Str((Val(get1(37)) * 256 + Val(get1(38))) / 1000) '总电压
'Text1(21).Text = Hex(Out_ACV Mod 65536) '发送效验和
'Text1(22).Text = Hex(get1(51) * 256 + get1(52)) '接收效验和
'Text1(21).Text = Str(Hex(get1(50) Mod 16)) 'zt0状态字
Text1(22).Text = Str(Val(get1(53)) * 256 + Val(get1(54))) '软件序号,即版本号
'Out_ACV = 0
'For i = 0 To 48
'Out_ACV = Out_ACV + get1(2 + i)
'Next i
'Text1(23).Text = Hex(Out_ACV Mod 65536)
Text1(23).Text = Str(Val(get1(51)) * 256 + Val(get1(52))) '容量百分比
For j = 0 To 1
For i = 0 To 7
If ((get1(45 + j) And 2 ^ i) = 2 ^ i) Then
Shape1(32 + i + j * 8).FillColor = &HFF& '正常变绿,关声音
' Timer2.Enabled = False
GBZ2 = GBZ2 And (&HFF - 2 ^ i) '外部告警判别1---8
Else
Shape1(32 + i + j * 8).FillColor = &HC000& '告警时变红,并启动声音
'Timer2.Enabled = True
GBZ2 = GBZ2 Or (2 ^ i)
End If
Next i
Next j
For i = 0 To 2
If ((get1(47) And 2 ^ i) = 2 ^ i) Then
Shape1(48 + i).FillColor = &HFF& '正常变绿,关声音
' Timer2.Enabled = False
GBZ2 = GBZ2 And (&HFF - 2 ^ i) '外部告警判别1---8
Else
Shape1(48 + i).FillColor = &HC000& '告警时变红,并启动声音
'Timer2.Enabled = True
GBZ2 = GBZ2 Or (2 ^ i)
End If
Next i
For j = 0 To 2
For i = 0 To 7
If ((get1(48 + j) And 2 ^ i) = 2 ^ i) Then
Shape1(0 + i + j * 8).FillColor = &HFF& '正常变绿,关声音
' Timer2.Enabled = False
GBZ2 = GBZ2 And (&HFF - 2 ^ i) '外部告警判别1---8
Else
Shape1(0 + i + j * 8).FillColor = &HC000& '告警时变红,并启动声音
'Timer2.Enabled = True
GBZ2 = GBZ2 Or (2 ^ i)
End If
Next i
Next j
press = 0
Timer1.Enabled = True '启动定时timer1取参数
End Sub --------------------编程问答-------------------- 逻辑错误,你需要在OnComm事件触发时根据状态去读数据,而不是什么时候想读就能正常读到数据的。 --------------------编程问答-------------------- ...既没看到图,也没看到楼主问题在哪, 能否先归纳一下重点么
补充:VB , 控件