小弟最近刚学VB,我用MSComm控件获取电子称上的数据,我在以前软件的基础上更改了下获取公式,但是现在问题是刚用的时候,很快就能获取到数据,现在要2分钟左右才行,还有越来越慢的趋势,请高手给指导下,代码是:
Private Sub MSComm1_OnComm()
Dim llc As Variant
Dim a(6) As Long
On Error GoTo errmsgSelect Case MSComm1.CommEvent
Case comEvReceive
llc = MSComm1.Input
lld = llc
If lld(1) = 2 Then
a(1) = (lld(1 + 2) - 48) * 100000
a(2) = lld(1 + 3) - 48
a(0) = a(2) * 10000
a(3) = (lld(1 + 4) - 48) * 1000
a(4) = (lld(1 + 5) - 48) * 100
a(5) = lld(1 + 6) - 48
a(5) = a(5) * 10
a(6) = lld(1 + 7) - 48
ss = a(1) + a(0) + a(3) + a(4) + a(5) + a(6)
End If
If Option1.Value = True Then
SQL = "select * from uwhssk where 原料='" & Combo1.Text & "' "
Adodc2.CommandType = adCmdText
Adodc2.RecordSource = SQL
Adodc2.Refresh
If Adodc2.Recordset.EOF = False Then
xs = Adodc2.Recordset(2)
Else
xs = 0
End If
ss = Int(ss * xs)
End If
If ss >= 80000 Then
ss = 80000
End If If ss = 0 Then
Text12.Text = "重选品种"
Else
SQL = "select * from uwhssk where 原料='" & Combo1.Text & "' "
Adodc2.CommandType = adCmdText
Adodc2.RecordSource = SQL
Adodc2.Refresh
If Adodc2.Recordset.EOF = False Then
If Text7.Text <> "" Then
Text12.Text = ss
Else
Text12.Text = "输入车号"
End If
End If
End If If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Select
Exit Sub
errmsg:
MsgBox "非法操作"
End Sub
时间控件:
Private Sub Timer1_Timer()
Dim lla As VariantIf MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End IfMSComm1.CommPort = 1
MSComm1.Settings = "2400,n,8,1"
MSComm1.InputLen = 0
MSComm1.InBufferSize = 36
MSComm1.OutBufferSize = 36
MSComm1.InputMode = comInputModeBinary
MSComm1.SThreshold = 0
MSComm1.RTSEnable = FalseIf MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
Do
DoEvents
Loop Until MSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
MSComm1.RThreshold = 36
MSComm1.RTSEnable = True
Label10.Caption = Date + TimeEnd Sub
我是不是还要加上清缓存的代码啊?
Private Sub MSComm1_OnComm()
Dim llc As Variant
Dim a(6) As Long
On Error GoTo errmsgSelect Case MSComm1.CommEvent
Case comEvReceive
llc = MSComm1.Input
lld = llc
If lld(1) = 2 Then
a(1) = (lld(1 + 2) - 48) * 100000
a(2) = lld(1 + 3) - 48
a(0) = a(2) * 10000
a(3) = (lld(1 + 4) - 48) * 1000
a(4) = (lld(1 + 5) - 48) * 100
a(5) = lld(1 + 6) - 48
a(5) = a(5) * 10
a(6) = lld(1 + 7) - 48
ss = a(1) + a(0) + a(3) + a(4) + a(5) + a(6)
End If
If Option1.Value = True Then
SQL = "select * from uwhssk where 原料='" & Combo1.Text & "' "
Adodc2.CommandType = adCmdText
Adodc2.RecordSource = SQL
Adodc2.Refresh
If Adodc2.Recordset.EOF = False Then
xs = Adodc2.Recordset(2)
Else
xs = 0
End If
ss = Int(ss * xs)
End If
If ss >= 80000 Then
ss = 80000
End If If ss = 0 Then
Text12.Text = "重选品种"
Else
SQL = "select * from uwhssk where 原料='" & Combo1.Text & "' "
Adodc2.CommandType = adCmdText
Adodc2.RecordSource = SQL
Adodc2.Refresh
If Adodc2.Recordset.EOF = False Then
If Text7.Text <> "" Then
Text12.Text = ss
Else
Text12.Text = "输入车号"
End If
End If
End If If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Select
Exit Sub
errmsg:
MsgBox "非法操作"
End Sub
时间控件:
Private Sub Timer1_Timer()
Dim lla As VariantIf MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End IfMSComm1.CommPort = 1
MSComm1.Settings = "2400,n,8,1"
MSComm1.InputLen = 0
MSComm1.InBufferSize = 36
MSComm1.OutBufferSize = 36
MSComm1.InputMode = comInputModeBinary
MSComm1.SThreshold = 0
MSComm1.RTSEnable = FalseIf MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
Do
DoEvents
Loop Until MSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
MSComm1.RThreshold = 36
MSComm1.RTSEnable = True
Label10.Caption = Date + TimeEnd Sub
我是不是还要加上清缓存的代码啊?
解决方案 »
- 急!!!怎么要下面的代码实现循环
- 当软件A获取机器的硬盘序列号或者CPU序列号时,如何编写一欺骗程序,让软件A得到的序列号是欺骗程序输出的序列号?
- 如何接收动态库中的函数char ** GetString() 的字符串数组
- 有谁知道这种语法的术语是什么
- text控件在加入滚动条后,如何使显示的内容总为最新的内容?
- 帮我看看事务提交出错
- 刚学游标,试着写了一下,不过运行出错呢.
- 如何用鼠标拖拽的方式实现MSFlexGrid的两行(/两列)调换顺序?
- 各位大侠,请问一些关于打包问题
- 如何修改个句话?VB ADO连接MDB问题,只抢不散!
- VB中,子窗体的菜单会复盖掉主窗体的菜单,有什么办法解决
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 出现”字符串的语法错误,在查询表达式.....中“.请高手帮帮忙!!!
http://download.csdn.net/source/1262066