12-31-2009 11:36 AM
MS 2008 in VBasic
I am trying to write a data acq pgm that is portable between a few dif D/A systems I have. I have taken code from NI examples to set up channels (singl channel read ai). I choped up this code to attempt to separate it into 2 parts: 1) configure the tasks and 2) perform the tasks. I require this separation so I can have a separate config page where hdwr setups are loaded, saved and edited (for using different sensors, sig conditioners, etc).
My attempts have so far been a failure. I cannot successfully define tasks on one form and run them on another. I am sure this an issue with variable scope but I don't understand the workings of the code that assigns 'mytask'. I can't seem to pass the variable 'PhysChan' between forms. The button that clicks to call 'Config' opens the configuration page where the variable is defined. The problem arises when I try to debug or compile. The compiler does not recognise that the 'ConfigForm' is supposed to set the task the after execution of the ConfigForm, we return to the MainForm where the task is to be perfomed. I get a compiler error that the PhyChan variable is null. Compiler is trying to compile the MainForm in its entirety although I need it to see that its compilation is dependent on the ConfigForm. That is beyond my meager knowledge of VS. Is there a straightforward solution to this?
Module MainModule
Public mytask1, mytask2, mytask3 As Task
Public reader1, reader2, reader3 As AnalogMultiChannelReader
Public data1(), data2(), data3() As Double
Public PhysChan1, PhysChan2, PhysChan3 As String
Public PassedConfig As Boolean
End Module
Public Class MainForm
Inherits System.Windows.Forms.Form
Public Sub MainForm_load(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
mytask1 = New Task()
mytask2 = New Task()
mytask3 = New Task()
reader1 = New AnalogMultiChannelReader(mytask1.Stream)
reader2 = New AnalogMultiChannelReader(mytask2.Stream)
reader3 = New AnalogMultiChannelReader(mytask3.Stream)
PassedConfig = False
End Sub
Public Sub RunBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunBtn.Click
Try
RunBtn.Enabled = False
data1 = reader1.ReadSingleSample()
data2 = reader2.ReadSingleSample()
data3 = reader3.ReadSingleSample()
TextBox1.Text = data1(0)
TextBox2.Text = data2(0)
TextBox3.Text = data3(0)
Catch exception As DaqException
MessageBox.Show(exception.Message)
Finally
'mytask1.Dispose()
End Try
RunBtn.Enabled = True
End Sub
Private Sub ConfigBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfigBtn.Click
ConfigForm.Show()
mytask1 = New Task()
mytask1.AIChannels.CreateVoltageChannel(PhysChan1, "", _
AITerminalConfiguration.Rse, Convert.ToDouble(-10), _
Convert.ToDouble(10), AIVoltageUnits.Volts)
reader1 = New AnalogMultiChannelReader(mytask1.Stream)
mytask1.Control(TaskAction.Verify)
mytask2 = New Task()
mytask2.AIChannels.CreateVoltageChannel(PhysChan2, "", _
AITerminalConfiguration.Rse, Convert.ToDouble(-10), _
Convert.ToDouble(10), AIVoltageUnits.Volts)
reader2 = New AnalogMultiChannelReader(mytask2.Stream)
mytask2.Control(TaskAction.Verify)
mytask3 = New Task()
mytask3.AIChannels.CreateVoltageChannel(PhysChan3, "", _
AITerminalConfiguration.Rse, Convert.ToDouble(-10), _
Convert.ToDouble(10), AIVoltageUnits.Volts)
reader3 = New AnalogMultiChannelReader(mytask3.Stream)
mytask3.Control(TaskAction.Verify)
End Sub
End Class
Public Class ConfigForm
Public Sub ConfigForm_load(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
mytask1 = New Task()
mytask2 = New Task()
mytask3 = New Task()
reader1 = New AnalogMultiChannelReader(mytask1.Stream)
reader2 = New AnalogMultiChannelReader(mytask2.Stream)
reader3 = New AnalogMultiChannelReader(mytask3.Stream)
End Sub
Public Sub Done_click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles DoneBtn.Click
PhysChan1 = PhysChanCbo1.Text
PhysChan2 = PhysChanCbo2.Text
PhysChan3 = PhysChanCbo3.Text
mytask1 = New Task()
mytask1.AIChannels.CreateVoltageChannel(PhysChan1, "", _
AITerminalConfiguration.Rse, Convert.ToDouble(-10), _
Convert.ToDouble(10), AIVoltageUnits.Volts)
reader1 = New AnalogMultiChannelReader(mytask1.Stream)
mytask1.Control(TaskAction.Verify)
mytask2 = New Task()
mytask2.AIChannels.CreateVoltageChannel(PhysChan2, "", _
AITerminalConfiguration.Rse, Convert.ToDouble(-10), _
Convert.ToDouble(10), AIVoltageUnits.Volts)
reader2 = New AnalogMultiChannelReader(mytask2.Stream)
mytask2.Control(TaskAction.Verify)
mytask3 = New Task()
mytask3.AIChannels.CreateVoltageChannel(PhysChan3, "", _
AITerminalConfiguration.Rse, Convert.ToDouble(-10), _
Convert.ToDouble(10), AIVoltageUnits.Volts)
reader3 = New AnalogMultiChannelReader(mytask3.Stream)
mytask3.Control(TaskAction.Verify)
PassedConfig = True
Me.Hide()
End Sub
End Class
I apologize for the long code attachments. I could not see a way to pare it down and keep the intention of the code.
01-07-2010 02:37 PM
Howdy walter,
If I understand your question correctly, it looks like your problem lies in the ConfigBtn_Click callback in MainForm. In that function, you're passing the value of PhysChan1 to CreateVoltageChannel() before PhysChan1 is assigned a value. Currently, a value for PhysChan1 is assigned only in your ConfigForm, so if MainForm tries to use PhysChan1 before ConfigForm has a chance to assign it a value, you would receive an error like what you describe.
My suggestion would be to either (a) ensure that ConfigForm always runs before you try to read the value from PhysChan1 or (b) add some code in MainForm that will assign an appropriate value to PhysChan1 before it is read in CreateVoltageChannel().
01-13-2010 08:36 AM
01-16-2010 07:12 AM
01-18-2010 05:31 PM
hi walter,
Perhaps if you posted a ZIP of your very simplified version of your code that exhibits your problem, we could take a look at it. (Basically, provide a ZIP containing a very simple project we can literally open and attempt to compile so we can see the problem you describe.)
Sorry for the confusion.