Public Function LRC(str)
c = 0
l = Len(str) '求出str的长度赋值给l
For c = c + 1 To l
c_data = Mid$(str, c, 2) '在str串中,从c的值开始取2个字符。
d_lrc = d_lrc + Val("&H" + c_data)
c = c + 1
Next c
If d_lrc > &HFF Then
d_lrc = d_lrc Mod &H100
End If
h_lrc = Hex(&HFF - d_lrc + 1)
If Len(h_lrc) > 2 Then
h_lrc = Mid(h_lrc, Len(h_lrc) - 1, 2)
End If
LRC = h_lrc
End Function
下面对数据进行实时采集,数据采集程序界面如下图所示:
Private Sub Timer1_Timer()
'连接数据库
Dim CONN As New ADODB.Connection
Dim DBStr As String
Dim rs As New ADODB.Recordset
DBStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data.mdb;Persist Security Info=False"
CONN.Open DBStr
Dim Mac_Num' Mac_Num是车号
Mac_Num = Combo_MacNum.List(Combo_MacNum.ListIndex)
Debug.Print Mac_Num
sql = "select * from machine where Machine_Num = '" + Mac_Num + "'" '获取车号
rs.Open sql, CONN, 1, 3
Txt_type.Text = rs.Fields("machine_type").Value '获取车的类型(是精梳机还是条并联)
rs.Close
str_type = Mac_Num '
Set CONN = Nothing
DBStr = ""
'发送数据(根据所选车号发送数据)
If MSComm1.PortOpen = True And stakeout_flag And Combo_MacNum.ListIndex >= 0 Then
str_output = str_type + "030064001D"
MSComm1.Output = ":" + str_output + LRC(str_output) + Chr$(13) + Chr$(10)
'接收数据
inputstring = MSComm1.Input
'将接收到的数据分别赋值并显示
Txt_100.Text = change(inputstring, 8, 12) '甲班当日产量
Txt_102.Text = change(inputstring, 16, 20) '甲班累计产量
Txt_104.Text = change(inputstring, 24, 28) '乙班当日产量
Txt_106.Text = change(inputstring, 32, 36) '乙班累计产量
Txt_108.Text = change(inputstring, 40, 44) '丙班当日产量
Txt_110.Text = change(inputstring, 48, 52) '丙班累计产量
Txt_112.Text = change(inputstring, 56, 60) '丁班当日产量
Txt_114.Text = change(inputstring, 64, 68) '丁班累计产量
Txt_116.Text = change(inputstring, 72, 76) '各班当日合计产量
Txt_118.Text = change(inputstring, 80, 84) '各班累计合计产量
If Txt_type.Text = "条并联" Then
Txt_view1.Text = change(inputstring, 104, 108) '条并联绕卷速度
Txt_view2.Text = change(inputstring, 112, 116) '条并联当前长度
Else
Txt_view1.Text = change(inputstring, 88, 92) '精梳机钳次
Txt_view2.Text = change(inputstring, 96, 100) '精梳机条速
End If
str_state = Mid(inputstring, 120, 4) '接收运行状态的返回值,并判断运行状态
If str_state = "0001" Then
Cmd_128.Caption = "运行中"
End If
If str_state = "0000" Then
Cmd_128.Caption = "停止"
End If
End If
End Sub
change(inputstr, start1 As Integer, start2 As Integer)用于转换采集到的数据,其功能是将采集到的产量数据转换为十进制。其代码如下:
Public Function change(inputstr, start1 As Integer, start2 As Integer)
str_low = Mid(inputstr, start1, 4) '读寄存器中的数据赋给低字节
str_high = Mid(inputstr, start2, 4) '读寄存器中的数据赋给高字节
str_hex = str_high + str_low '整合高低字节数据
str_input = Val("&H" + str_hex) '将十六进制数据转换成十进制
If str_input >= -32768 And str_input <= -1 Then '将转换成十进制处于-32768~32767的数据转换为相应的正数
str_input = str_input + 65536
End If
change = str_input / 1000 '返回值 取三位小数
End Function
3)信息查询
信息查询界面如下所示,可对已经保存的生产数据根据时间、班次、产量、车号进行综合查询。
该功能的程序方面比较简单,主要是对数据库的查询操作。以单独对班次查询为例,其代码如下:
'按班次查询子函数,用于判断其搜索关键字是否有效
Private Sub Chk_team_Click()
If Chk_team.Value = 1 Then
Combo_team.Enabled = True
Else
Combo_team.Enabled = False
End If
End Sub
'选择班次子函数,用于判断所选班次
Private Sub Combo_team_Click()
Dim liner_str
Select Case Combo_team.ListIndex
Case 0
liner_str = "甲班"
Case 1
liner_str = "乙班"
Case 2
liner_str = "丙班"
Case 3
liner_str = "丁班"
End Select
End Sub
'班次查询子函数,获取查询字符串
Public Function sch_team()
Dim liner_str
Dim sch_str_team
Select Case Combo_team.ListIndex
Case 0
liner_str = "甲班"
Case 1
liner_str = "乙班"
Case 2
liner_str = "丙班"
Case 3
liner_str = "丁班"
End Select
sch_str_team = "info_liner = '" + liner_str + "'"
sch_team = sch_str_team
End Function
'产量查询函数,用于查询产量并对其进行显示
Private Sub cmd_search_Click()
Dim sch_str
'按班次查询
If Chk_team.Value = 1 Then
sch_str = " where " + sch_team()
End If
'查询结果显示
Dim resource_str
resource_str = " select info_id as 编号,info_mactype as 机器类型 ,info_liner as 班次, "
resource_str = resource_str + " info_sumoutput as 当日产量, info_dayoutput as 累计产量,"
resource_str = resource_str + " info_daytotal as 当日合计产量,info_total as 累计合计产量 "
resource_str = resource_str + " from " + table_str + sch_str
Adodc1.RecordSource = resource_str
Debug.Print Adodc1.RecordSource
Adodc1.Refresh
End Sub