我们公司买了一个德国的称,老大给了我一个例程。但是这个例程是一次性收到十六组数据。而我要实现去皮功能不知到从哪下手,求高手指点不胜感谢!例程片段如下:
Private sFormat(8) As String '格式字符串-结果
Private sFormat1(8) As String '格式字符串1-测量数组
Private weightValue(8, 15) As Double '测量数据数组
Private IndexFocus As Integer '四角误差焦点的顺序号
Private NoTest As Integer
Private sNoTest As String
Private maxLoop(8) As Integer '9次测量
Private aveDev(8) As Double '存放平均值的中值
Private sPrnTest(8, 15) As String '存放每次测量数据换为字符串
Private sPrnNote(8, 8) As String '存放每次除测量数据外的数据
Private Sub Form_Load()
If Not MSComm1.PortOpen Then '打开串行口
MSComm1.PortOpen = True
End If
If Err Then
MsgBox "串行通信口没有打开!"
End If
MSComm1.InBufferCount = 0 '接收缓冲区清零
numWeight = 0
IndexFocus = 100 '焦点设置为任意值
LABEL5(1).Caption = " 1"
NoTest = 1
sNoTest = "1"
End SubPrivate Sub MSComm1_OnComm()
Dim sTemp As String
Dim sReceive As String
MSComm1.InputLen = 1
Select Case MSComm1.CommEvent
Case comEvReceive '接收到16个数据
sTemp = MSComm1.Input
If sTemp = "+" Or sTemp = "-" Or sTemp = " " Then '判断是否为天平传送首字节 + 、- 、space
Do While sTemp <> Chr(13) '接收数据,判断是否为LF
sReceive = sReceive & sTemp
sTemp = MSComm1.Input
Loop
Select Case IndexFocus
Case 0 To 14
weightValue(NoTest - 1, IndexFocus) = Val(sReceive)
sPrnTest(NoTest - 1, IndexFocus) = sReceive
Text2(IndexFocus).Text = sReceive
If Text2(3) <> "" Then
If Text2(15) = "" Then
maxLoop(NoTest - 1) = 3
End If
CalResult
End If
Text2(IndexFocus + 1).SetFocus
Case 15
weightValue(NoTest - 1, 15) = Val(sReceive)
sPrnTest(NoTest - 1, 15) = sReceive
Text2(15).Text = sReceive
maxLoop(NoTest - 1) = 15
CalResult
IndexFocus = 100
End Select
End If
Case comRxParity '接收错误
MsgBox "警告: 通 信 格 式 错 ! "
End SelectMSComm1.InBufferCount = 0 '接收缓冲区清零End SubPrivate Sub text2_GotFocus(Index As Integer)
IndexFocus = Index
End Sub
Private Sub cmdNewCal()Dim i As Integer
If Text2(15).Text <> "" Then '最后一个数传输完成
maxLoop(NoTest - 1) = 15
ElseIf Text2(3).Text <> "" Then '第一个ABBA循环完成
maxLoop(NoTest - 1) = 3
Else
Exit Sub
End If
For i = 0 To 15
weightValue(NoTest - 1, i) = Val(Text2(i).Text)‘手工改变的数值写入数组
sPrnTest(NoTest - 1, i) = Text2(i).Text
Next i
CalResult
End Sub
Private Sub CalResult()
Dim diffValue(8) As Double '存放B-A值
Dim aveValue(4) As Double '存放1个ABBA循环的平均值
Dim i As Integer
Dim numPoint As Integer '小数点的位数
Dim sReceive As StringFor i = 0 To maxLoop(NoTest - 1) - 1 Step 4 '首先计算B-A
diffValue(i / 2) = weightValue(NoTest - 1, i) - weightValue(NoTest - 1, i + 1)
diffValue(i / 2 + 1) = weightValue(NoTest - 1, i + 3) - weightValue(NoTest - 1, i + 2)
Next i
For i = 0 To (maxLoop(NoTest - 1) - 1) / 2 Step 2 '计算 B-A 的平均值
aveValue(i / 2) = (diffValue(i) + diffValue(i + 1)) / 2
Next i aveDev(NoTest - 1) = 0
For i = 0 To (maxLoop(NoTest - 1) - 3) / 4 '计算平均值的中值,以g为单位
aveDev(NoTest - 1) = aveDev(NoTest - 1) + aveValue(i)
Next i
aveDev(NoTest - 1) = aveDev(NoTest - 1) / ((maxLoop(NoTest - 1) + 1) / 4)
sReceive = Text2(0).Text '检定完成时,计算小数点位数
i = InStr(1, sReceive, ".", vbTextCompare)
If i <> 0 Then
sTemp = Right$(sReceive, Len(sReceive) - i)
i = InStr(1, sTemp, " ", vbTextCompare)
numPoint = i - 1
Else
numPoint = 0
End If
sFormat1(NoTest - 1) = ""
If numPoint <= 0 Then '设置测量数组输出格式,保存在sFormat1
sFormat1(NoTest - 1) = "#"
Else
For i = 1 To numPoint
sFormat1(NoTest - 1) = sFormat1(NoTest - 1) & "0"
Next i
sFormat1(NoTest - 1) = "########0." & sFormat1(NoTest - 1)
End If
numPoint = numPoint -3 '换为以mg作为单位
sFormat(NoTest - 1) = ""
If numPoint <= 0 Then '设置输出格式,保存在sFormat
sFormat(NoTest - 1) = "#"
Else
For i = 1 To numPoint
sFormat(NoTest - 1) = sFormat(NoTest - 1) & "0"
Next i
sFormat(NoTest - 1) = "########0." & sFormat(NoTest - 1)
End If
Label6.Caption = Format(aveDev(NoTest - 1) * 1000 + Val(Text3.Text), sFormat(NoTest - 1) & "0") '显示砝码修正值
If Text4.Text <> "" Then
Label7.Caption = Format((Val(Label6.Caption) + Val(Text4.Text)) / 2, sFormat(NoTest - 1) & "0")
End If
sPrnNote(NoTest - 1, 0) = mass1.Text '存储测量数据外的数据,检定一组数据后即打印
sPrnNote(NoTest - 1, 1) = Text3.Text
sPrnNote(NoTest - 1, 2) = Text4.Text
For i = 0 To 3
sPrnNote(NoTest - 1, 3 + i) = Text5(i).Text
Next i
sPrnNote(NoTest - 1, 7) = Label6.Caption
sPrnNote(NoTest - 1, 8) = Label7.Caption
End Sub
Private Sub cmdExitW()
Unload Me
End
End Sub
Private sFormat(8) As String '格式字符串-结果
Private sFormat1(8) As String '格式字符串1-测量数组
Private weightValue(8, 15) As Double '测量数据数组
Private IndexFocus As Integer '四角误差焦点的顺序号
Private NoTest As Integer
Private sNoTest As String
Private maxLoop(8) As Integer '9次测量
Private aveDev(8) As Double '存放平均值的中值
Private sPrnTest(8, 15) As String '存放每次测量数据换为字符串
Private sPrnNote(8, 8) As String '存放每次除测量数据外的数据
Private Sub Form_Load()
If Not MSComm1.PortOpen Then '打开串行口
MSComm1.PortOpen = True
End If
If Err Then
MsgBox "串行通信口没有打开!"
End If
MSComm1.InBufferCount = 0 '接收缓冲区清零
numWeight = 0
IndexFocus = 100 '焦点设置为任意值
LABEL5(1).Caption = " 1"
NoTest = 1
sNoTest = "1"
End SubPrivate Sub MSComm1_OnComm()
Dim sTemp As String
Dim sReceive As String
MSComm1.InputLen = 1
Select Case MSComm1.CommEvent
Case comEvReceive '接收到16个数据
sTemp = MSComm1.Input
If sTemp = "+" Or sTemp = "-" Or sTemp = " " Then '判断是否为天平传送首字节 + 、- 、space
Do While sTemp <> Chr(13) '接收数据,判断是否为LF
sReceive = sReceive & sTemp
sTemp = MSComm1.Input
Loop
Select Case IndexFocus
Case 0 To 14
weightValue(NoTest - 1, IndexFocus) = Val(sReceive)
sPrnTest(NoTest - 1, IndexFocus) = sReceive
Text2(IndexFocus).Text = sReceive
If Text2(3) <> "" Then
If Text2(15) = "" Then
maxLoop(NoTest - 1) = 3
End If
CalResult
End If
Text2(IndexFocus + 1).SetFocus
Case 15
weightValue(NoTest - 1, 15) = Val(sReceive)
sPrnTest(NoTest - 1, 15) = sReceive
Text2(15).Text = sReceive
maxLoop(NoTest - 1) = 15
CalResult
IndexFocus = 100
End Select
End If
Case comRxParity '接收错误
MsgBox "警告: 通 信 格 式 错 ! "
End SelectMSComm1.InBufferCount = 0 '接收缓冲区清零End SubPrivate Sub text2_GotFocus(Index As Integer)
IndexFocus = Index
End Sub
Private Sub cmdNewCal()Dim i As Integer
If Text2(15).Text <> "" Then '最后一个数传输完成
maxLoop(NoTest - 1) = 15
ElseIf Text2(3).Text <> "" Then '第一个ABBA循环完成
maxLoop(NoTest - 1) = 3
Else
Exit Sub
End If
For i = 0 To 15
weightValue(NoTest - 1, i) = Val(Text2(i).Text)‘手工改变的数值写入数组
sPrnTest(NoTest - 1, i) = Text2(i).Text
Next i
CalResult
End Sub
Private Sub CalResult()
Dim diffValue(8) As Double '存放B-A值
Dim aveValue(4) As Double '存放1个ABBA循环的平均值
Dim i As Integer
Dim numPoint As Integer '小数点的位数
Dim sReceive As StringFor i = 0 To maxLoop(NoTest - 1) - 1 Step 4 '首先计算B-A
diffValue(i / 2) = weightValue(NoTest - 1, i) - weightValue(NoTest - 1, i + 1)
diffValue(i / 2 + 1) = weightValue(NoTest - 1, i + 3) - weightValue(NoTest - 1, i + 2)
Next i
For i = 0 To (maxLoop(NoTest - 1) - 1) / 2 Step 2 '计算 B-A 的平均值
aveValue(i / 2) = (diffValue(i) + diffValue(i + 1)) / 2
Next i aveDev(NoTest - 1) = 0
For i = 0 To (maxLoop(NoTest - 1) - 3) / 4 '计算平均值的中值,以g为单位
aveDev(NoTest - 1) = aveDev(NoTest - 1) + aveValue(i)
Next i
aveDev(NoTest - 1) = aveDev(NoTest - 1) / ((maxLoop(NoTest - 1) + 1) / 4)
sReceive = Text2(0).Text '检定完成时,计算小数点位数
i = InStr(1, sReceive, ".", vbTextCompare)
If i <> 0 Then
sTemp = Right$(sReceive, Len(sReceive) - i)
i = InStr(1, sTemp, " ", vbTextCompare)
numPoint = i - 1
Else
numPoint = 0
End If
sFormat1(NoTest - 1) = ""
If numPoint <= 0 Then '设置测量数组输出格式,保存在sFormat1
sFormat1(NoTest - 1) = "#"
Else
For i = 1 To numPoint
sFormat1(NoTest - 1) = sFormat1(NoTest - 1) & "0"
Next i
sFormat1(NoTest - 1) = "########0." & sFormat1(NoTest - 1)
End If
numPoint = numPoint -3 '换为以mg作为单位
sFormat(NoTest - 1) = ""
If numPoint <= 0 Then '设置输出格式,保存在sFormat
sFormat(NoTest - 1) = "#"
Else
For i = 1 To numPoint
sFormat(NoTest - 1) = sFormat(NoTest - 1) & "0"
Next i
sFormat(NoTest - 1) = "########0." & sFormat(NoTest - 1)
End If
Label6.Caption = Format(aveDev(NoTest - 1) * 1000 + Val(Text3.Text), sFormat(NoTest - 1) & "0") '显示砝码修正值
If Text4.Text <> "" Then
Label7.Caption = Format((Val(Label6.Caption) + Val(Text4.Text)) / 2, sFormat(NoTest - 1) & "0")
End If
sPrnNote(NoTest - 1, 0) = mass1.Text '存储测量数据外的数据,检定一组数据后即打印
sPrnNote(NoTest - 1, 1) = Text3.Text
sPrnNote(NoTest - 1, 2) = Text4.Text
For i = 0 To 3
sPrnNote(NoTest - 1, 3 + i) = Text5(i).Text
Next i
sPrnNote(NoTest - 1, 7) = Label6.Caption
sPrnNote(NoTest - 1, 8) = Label7.Caption
End Sub
Private Sub cmdExitW()
Unload Me
End
End Sub
解决方案 »
- 救命贴!!!紧急求助!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 请教用数据环境连接ACCESS,做一个登陆系统 (急用,急用)
- wininet.dll写ftp客户端,加上上传进度条.如何写啊
- 邮件收不到?
- 如何关闭IE窗口
- 請問如何実現在文本框中輸入的数据是日期格式,且要求輸入的数据不能違犯常規的邏輯錯誤.
- #100#,如何使用TTS?
- 关于SendMessage()
- 各位水友、各位同盟……我想隐藏我上网的IP!!!怎么办阿?
- ☆★小弟的大作:远程监控"小强",恳请各位测试,凡进都有分★☆★
- 怎么用几行代码就可以实行检测进程?
- sql语言写在VB中时出了什么问题?
Do While sTemp <> Chr(13) '接收数据,判断是否为LF
程序我还没太看明白,不过重量值一般是放在多个字节中,合成正确的重量值后,用皮重减去毛重。协议里应能判断哪组为皮重哪组为毛重。