近一段时间,应一研究生(机械工程)的要求帮忙做个采集温度和计算摩擦系数的数据采集系统,系统采用研华的PCI采集卡1710lL。在这方面研华的资料比较少,研究了几个晚上终于弄成了一个采集数据的程序。没有使用研华的控件。
首先是初始化通道,设置程序显示界面及曲线图初始化,导入研华驱动公共文件。
1 GainArray(0) = 0 2 GainArray(1) = 0 3 GainArray(2) = 0 4 GainArray(3) = 0 5 Call tabinit '初始化表格 6 For i = 1 To 60 7 cbotime.AddItem (i) 8 Next 9 Picture1.BackColor = QBColor(15) 10 Picture1.Scale (-5, 2000)-(6000, -2000) '定义坐标系 11 Picture1.DrawWidth = 2 12 Picture1.CurrentX = -2 13 Picture1.CurrentY = -1 14 Picture1.Print 0 15 Picture1.Line (0, 2000)-(0, -2000), QBColor(1) '画竖轴 16 Picture1.Line (-5, 0)-(6000, 0), QBColor(1) '画横轴 17 18 drawtype = 1 '初始绘制连续的曲线图 19 Mindata = 0: Maxdata = 3 '下限、上限温度报警初始值 20 21 baohuzhi(0).Text = Mindata '显示下限 22 baohuzhi(1).Text = Maxdata '上限温度值 23 'alarm1.FillColor = QBColor(10) '报警指示灯初始颜色 24 'alarm2.FillColor = QBColor(10) 25 num = 1 26 27 '----------------多通道设置1------------------- 28 ErrCde = DRV_DeviceOpen(1, DriverHandle1) '打开模拟量输入端口 设备号0(为安装在系统中的板卡号000,001) 打开第0个采集卡 29 If (ErrCde <> 0) Then 30 DRV_GetErrorMessage ErrCde, szErrMsg 31 Response = MsgBox("通道1:" + szErrMsg, vbOKOnly, "Error!!") 32 Exit Sub 33 End If 34 35 ErrCde = DRV_DeviceOpen(1, DriverHandle2) '打开数字量输出端口 36 If (ErrCde <> 0) Then 37 DRV_GetErrorMessage ErrCde, szErrMsg 38 Response = MsgBox("通道1:" + szErrMsg, vbOKOnly, "Error!!") 39 Exit Sub 40 End If 41 '------------------- 42 With MAIConfig_T 43 .NumChan = 4 '4通道 44 .StartChan = 0 '从0 通道开始 45 .GainArray = DRV_GetAddress(GainArray(0)) 'Array(0, 0, 0,0) '各通道增益值(0为+-5V) 46 End With 47 ErrCde = DRV_MAIConfig(DriverHandle1, MAIConfig_T) 48 If (ErrCde <> 0) Then 49 DRV_GetErrorMessage ErrCde, szErrMsg 50 Response = MsgBox("多通道设置错误:" + szErrMsg, vbOKOnly, "Error!!") 51 Exit Sub 52 End If 53 54 MAIVoltageIn.NumChan = MAIConfig_T.NumChan 55 MAIVoltageIn.StartChan = MAIConfig_T.StartChan 56 MAIVoltageIn.GainArray = DRV_GetAddress(GainArray(0)) '得到值0 57 MAIVoltageIn.TrigMode = 0
使用单通道和多通道的初始化有点不同,本以为初始化多个单通道就能实现多通道的但是不行,获取的数据根本不是实际的数据。所以只能设置多通道的方法来获取数据。画曲线子程序
1 '画连续曲线/间断散点图 2 Sub DrawCurve() 3 On Error GoTo hh 4 If num = 0 Or num = 1 Then Exit Sub 5 'Picture1.Cls 6 Picture1.DrawWidth = 1 7 8 '0黑,1蓝,2绿,3青,4红,5洋红,6黄,7白 9 10 gX0 = (num - 1) * 6: gX1 = num * 6 ' 11 wY0 = T_Data(num - 1) * 3: wY1 = T_Data(num) * 3 12 mY0 = M_Data(num - 1) * 3: mY1 = M_Data(num) * 3 13 Select Case drawtype 14 Case 1 15 16 Picture1.Line (gX0, wY0)-(gX1, wY1), QBColor(4) '温度值 17 18 Picture1.Line (gX0, mY0)-(gX1, mY1), QBColor(1) '摩擦值 19 20 Case 2 21 22 Picture1.PSet (gX1, wY1), QBColor(4) '温度描点 23 24 Picture1.PSet (gX1, mY1), QBColor(1) '温度描点 25 26 End Select 27 28 hh: Exit Sub 29 End Sub
采集数据使用一个定时器定时读取PCI上内存地址的数据。在界面上增加选择采集时间,时间越短,采样数据越准确,可根据采样率要求自行选择采集周期。使用一个Timer定时器。
1 Private Sub Tim_Reader_Timer() 2 Dim u As String 3 If num > 1000 Then 4 5 '到显示不下时先保存数据,然后重新画 6 If ChkSave.value = 1 Then 7 Tim_Reader.Enabled = False 8 '----------------- 9 Randomize 10 filesName = App.Path + "/DataSaves/" + Format(Now, "YYYYMMDDHHMMSS") & Int(Rnd(100) * 100) & ".txt" 11 If (fs.FileExists(filesName)) Then 12 '存在就另外随机创建文件 13 filesName = App.Path + "/DataSaves/" + Format(Now, "YYYYMMDDHHMMSS") & Int(Rnd(100) * 100) & ".txt" 14 fs.CreateTextFile (filesName) 15 Else 16 fs.CreateTextFile (filesName) 17 End If 18 F3 = FreeFile 19 Open filesName For Output As #F3 20 Print #F3, "压力值" & "," & "温度值" & "," & "摩擦系数", " & "; 高温度值; " " 21 For j = 1 To num - 1 22 Print #F3, F_Data(j) & "," & T_Data(j) & "," & M_Data(j), " & T1_Data(j)" 23 Next 24 Close #F3 25 26 Tim_Reader.Enabled = True 27 End If 28 29 Call renew 30 31 End If 32 'T_Data(1200) As Single ' 温度收集数据 33 'M_Data(1200) As Single '摩擦系数数据 34 'F_Data(1200) As Single '压力数据 35 '---------------------- 36 MAIVoltageIn.VoltageArray = DRV_GetAddress(volval(0)) 37 ErrCde = DRV_MAIVoltageIn(DriverHandle1, MAIVoltageIn) '获取多通道数据 38 If (ErrCde <> 0) Then 39 DRV_GetErrorMessage ErrCde, szErrMsg 40 Response = MsgBox("1:" + szErrMsg, vbOKOnly, "Error!!") 41 Exit Sub 42 End If 43 T_Data(num) = Val(volval(0)) ' (Val(voltage) - 1) * 50 '标度变换(温度值) 44 wendu(0).Text = Format$(T_Data(num), "0.0") 45 '---------------------- 46 MAIVoltageIn.VoltageArray = DRV_GetAddress(volval(0)) 47 ErrCde = DRV_MAIVoltageIn(DriverHandle1, MAIVoltageIn) '获取多通道数据 48 If (ErrCde <> 0) Then 49 DRV_GetErrorMessage ErrCde, szErrMsg 50 Response = MsgBox("2:" + szErrMsg, vbOKOnly, "Error!!") 51 Exit Sub 52 End If 53 M_Data(num) = Val(volval(1)) ' (Val(voltage_m) - 1) * 50 '标度变换(摩擦值) 54 mochaxishu(0).Text = Format$(M_Data(num), "0.0") 55 '--------------------------- 56 MAIVoltageIn.VoltageArray = DRV_GetAddress(volval(0)) 57 ErrCde = DRV_MAIVoltageIn(DriverHandle1, MAIVoltageIn) '获取多通道数据 58 If (ErrCde <> 0) Then 59 DRV_GetErrorMessage ErrCde, szErrMsg 60 Response = MsgBox("3:" + szErrMsg, vbOKOnly, "Error!!") 61 Exit Sub 62 End If 63 F_Data(num) = Val(volval(2)) ' (Val(voltage_f) - 1) * 50 '标度变换(压力值) 64 force.Text = Format$(F_Data(num), "0.0") 65 '---------------------- 66 MAIVoltageIn.VoltageArray = DRV_GetAddress(volval(0)) 67 ErrCde = DRV_MAIVoltageIn(DriverHandle1, MAIVoltageIn) '获取多通道数据 68 If (ErrCde <> 0) Then 69 DRV_GetErrorMessage ErrCde, szErrMsg 70 Response = MsgBox("3:" + szErrMsg, vbOKOnly, "Error!!") 71 Exit Sub 72 End If 73 T1_Data(num) = Val(volval(3)) ' (Val(voltage_f) - 1) * 50 '标度变换(压力值) 74 gaowen(4).Text = Format$(F_Data(num), "0.0") 75 '---------------------- 76 77 'ErrCde = DRV_AIVoltageIn(DriverHandle5, AIVoltageIn_F) '获取AI3通道数据(实型电压值) 78 'If (ErrCde <> 0) Then 79 'DRV_GetErrorMessage ErrCde, szErrMsg 80 'Response = MsgBox(szErrMsg, vbOKOnly, "Error!!") 81 'Exit Sub 82 'End If 83 'F_Data(num) = (Val(voltage_f) - 1) * 50 '标度变换(压力值) 84 'force.Text = Format$(F_Data(num), "0.0") 85 '---------------------- 86 msgrd_show.Row = num 87 msgrd_show.Col = 1 88 msgrd_show.CellAlignment = vbCenter 89 msgrd_show.Text = Now() 90 msgrd_show.Col = 2 91 msgrd_show.CellAlignment = vbCenter 92 msgrd_show.Text = T_Data(num) 'filedata(num - 2) 温度值 93 msgrd_show.Col = 3 94 msgrd_show.CellAlignment = vbCenter 95 msgrd_show.Text = M_Data(num) 'filedata(num - 1) 摩擦系数值 96 msgrd_show.Col = 4 97 msgrd_show.CellAlignment = vbCenter 98 msgrd_show.Text = T1_Data(num) 'filedata(num - 1) 摩擦系数值 99 100 101 'Call alarm '不做报警提示 102 Call Calculates '调用计算极值、平均值子程序 103 Call DrawCurve '调用绘图子程序 104 num = num + 1 105 End Sub
运行时界面初始如下图所示: