求数据采集卡多通道模拟量输入VB程序解释说明
DimErrCdeAsLong'ErrorcodeDimszErrMsgAsString*80'ErrorstringDimDriverHandleAsLongDimMA...
Dim ErrCde As Long ' Error code
Dim szErrMsg As String * 80 ' Error string
Dim DriverHandle As Long
Dim MAIConfig As PT_MAIConfig
Dim lpMAIVoltageIn As PT_MAIVoltageIn
Dim voltage(0 To 2) As Single
Dim GainArray(0 To 2) As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 800
End Sub
Private Sub Command2_Click()
'Step 5: close device
ErrCde = DRV_DeviceClose(DriverHandle)
If (ErrCde <> 0) Then
DRV_GetErrorMessage ErrCde, szErrMsg
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")
Print "step5"
End If
End
End Sub
Private Sub Command3_Click()
Timer1.Enabled = False
End Sub
Private Sub Form_Load()
GainArray(0) = 0
GainArray(1) = 0
GainArray(2) = 0
Timer1.Enabled = False
'Step 1: open device
ErrCde = DRV_DeviceOpen(0, DriverHandle) ' Make sure device number = 0
If (ErrCde <> 0) Then
DRV_GetErrorMessage ErrCde, szErrMsg
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")
Print "step1"
Exit Sub
End If
'Step 2: configure input range
MAIConfig.NumChan = 3 ' channel number: 3
MAIConfig.StartChan = 0
MAIConfig.GainArray = DRV_GetAddress(GainArray(0))
ErrCde = DRV_MAIConfig(DriverHandle, MAIConfig)
If (ErrCde <> 0) Then
DRV_GetErrorMessage ErrCde, szErrMsg
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")
Print "step2"
Exit Sub
End If
End Sub
Private Sub Timer1_Timer()
'Step 3: read value
lpMAIVoltageIn.NumChan = MAIConfig.NumChan
lpMAIVoltageIn.StartChan = MAIConfig.StartChan
lpMAIVoltageIn.GainArray = DRV_GetAddress(GainArray(0))
lpMAIVoltageIn.TrigMode = 0
Sum = 0#
For i = 0 To 999
lpMAIVoltageIn.VoltageArray = DRV_GetAddress(voltage(0))
ErrCde = DRV_MAIVoltageIn(DriverHandle, lpMAIVoltageIn)
If (ErrCde <> 0) Then
DRV_GetErrorMessage ErrCde, szErrMsg
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")
Print "step3"
Exit Sub
End If
Sum = Sum + voltage(0) ^ 2
Next i
voltage(0) = (Sum / 1000#) ^ (0.5)
'Step 4: display value
Text1.Text = Format(voltage(0), "####0.000000")
Text2.Text = Format(voltage(1), "####0.000000")
Text3.Text = Format(voltage(2), "####0.000000")
End Sub 展开
Dim szErrMsg As String * 80 ' Error string
Dim DriverHandle As Long
Dim MAIConfig As PT_MAIConfig
Dim lpMAIVoltageIn As PT_MAIVoltageIn
Dim voltage(0 To 2) As Single
Dim GainArray(0 To 2) As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 800
End Sub
Private Sub Command2_Click()
'Step 5: close device
ErrCde = DRV_DeviceClose(DriverHandle)
If (ErrCde <> 0) Then
DRV_GetErrorMessage ErrCde, szErrMsg
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")
Print "step5"
End If
End
End Sub
Private Sub Command3_Click()
Timer1.Enabled = False
End Sub
Private Sub Form_Load()
GainArray(0) = 0
GainArray(1) = 0
GainArray(2) = 0
Timer1.Enabled = False
'Step 1: open device
ErrCde = DRV_DeviceOpen(0, DriverHandle) ' Make sure device number = 0
If (ErrCde <> 0) Then
DRV_GetErrorMessage ErrCde, szErrMsg
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")
Print "step1"
Exit Sub
End If
'Step 2: configure input range
MAIConfig.NumChan = 3 ' channel number: 3
MAIConfig.StartChan = 0
MAIConfig.GainArray = DRV_GetAddress(GainArray(0))
ErrCde = DRV_MAIConfig(DriverHandle, MAIConfig)
If (ErrCde <> 0) Then
DRV_GetErrorMessage ErrCde, szErrMsg
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")
Print "step2"
Exit Sub
End If
End Sub
Private Sub Timer1_Timer()
'Step 3: read value
lpMAIVoltageIn.NumChan = MAIConfig.NumChan
lpMAIVoltageIn.StartChan = MAIConfig.StartChan
lpMAIVoltageIn.GainArray = DRV_GetAddress(GainArray(0))
lpMAIVoltageIn.TrigMode = 0
Sum = 0#
For i = 0 To 999
lpMAIVoltageIn.VoltageArray = DRV_GetAddress(voltage(0))
ErrCde = DRV_MAIVoltageIn(DriverHandle, lpMAIVoltageIn)
If (ErrCde <> 0) Then
DRV_GetErrorMessage ErrCde, szErrMsg
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")
Print "step3"
Exit Sub
End If
Sum = Sum + voltage(0) ^ 2
Next i
voltage(0) = (Sum / 1000#) ^ (0.5)
'Step 4: display value
Text1.Text = Format(voltage(0), "####0.000000")
Text2.Text = Format(voltage(1), "####0.000000")
Text3.Text = Format(voltage(2), "####0.000000")
End Sub 展开
1个回答
展开全部
Dim ErrCde As Long ' Error code
Dim szErrMsg As String * 80 ' Error string
Dim DriverHandle As Long
Dim MAIConfig As PT_MAIConfig
Dim lpMAIVoltageIn As PT_MAIVoltageIn
Dim voltage(0 To 2) As Single
Dim GainArray(0 To 2) As Integer '以上定义变量
Private Sub Command1_Click()
Timer1.Enabled = True'启动定时器timer1
Timer1.Interval = 800'设置定时器timer1的时间间隔是800毫秒
End Sub
Private Sub Command2_Click()
'Step 5: close device 关闭设备
ErrCde = DRV_DeviceClose(DriverHandle)'发送关闭设备指令,返回错误代码
If (ErrCde <> 0) Then'假如有错误
DRV_GetErrorMessage ErrCde, szErrMsg'获取错误代码和错误信息
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")’显示错误信息
Print "step5" '在窗体上显示step5,大概就是第五步有问题
End If
End
End Sub
Private Sub Command3_Click()
Timer1.Enabled = False'停止运行定时器timer1
End Sub
Private Sub Form_Load()
GainArray(0) = 0
GainArray(1) = 0
GainArray(2) = 0
Timer1.Enabled = False
'Step 1: open device 打开设备
ErrCde = DRV_DeviceOpen(0, DriverHandle) ' Make sure device number = 0
'执行打开设备命令,返回错误代码
If (ErrCde <> 0) Then'假如有错误
DRV_GetErrorMessage ErrCde, szErrMsg'获取错误代码和错误信息
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")'显示错误信息
Print "step1"'显示step1
Exit Sub'退出子程序,就是不在执行这个子程序的以下代码
End If
'Step 2: configure input range 配置输入范围
MAIConfig.NumChan = 3 ' channel number: 3 通道数3个
MAIConfig.StartChan = 0 '开始通道为0
MAIConfig.GainArray = DRV_GetAddress(GainArray(0))’获取地址
ErrCde = DRV_MAIConfig(DriverHandle, MAIConfig)'配置设置命令,返回错误代码
If (ErrCde <> 0) Then
DRV_GetErrorMessage ErrCde, szErrMsg
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")
Print "step2"
Exit Sub
End If
End Sub
Private Sub Timer1_Timer()
'Step 3: read value 从驱动程序读取量
lpMAIVoltageIn.NumChan = MAIConfig.NumChan
lpMAIVoltageIn.StartChan = MAIConfig.StartChan
lpMAIVoltageIn.GainArray = DRV_GetAddress(GainArray(0))
lpMAIVoltageIn.TrigMode = 0
Sum = 0#
For i = 0 To 999’多读几遍,可能是有时候读不到
lpMAIVoltageIn.VoltageArray = DRV_GetAddress(voltage(0))
ErrCde = DRV_MAIVoltageIn(DriverHandle, lpMAIVoltageIn)
If (ErrCde <> 0) Then'有错误时
DRV_GetErrorMessage ErrCde, szErrMsg’获取信息
Response = MsgBox(szErrMsg, vbOKOnly, "Error!!")'显示信息
Print "step3"
Exit Sub’退出子程序
End If
Sum = Sum + voltage(0) ^ 2
Next i
voltage(0) = (Sum / 1000#) ^ (0.5)
'Step 4: display value 显示读回来电压
Text1.Text = Format(voltage(0), "####0.000000")
Text2.Text = Format(voltage(1), "####0.000000")
Text3.Text = Format(voltage(2), "####0.000000")
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询