Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How to Set Tasks in separate forms from where they run

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.

 

 

 


Labview 8.5
Meas Studio 2008
0 Kudos
Message 1 of 5
(3,997 Views)

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().

Warm regards,

pBerg
0 Kudos
Message 2 of 5
(3,948 Views)
Thanks for the response. You are correct about the reading of a value that is not yet assigned. However during running of the app, you can see that the offending code is immediately after the line that calls the config procedure. This means that the variable cannot be called before it is assigned as you suggest. This error is not at runtime. It is because the compiler cannot understand that it is flagging a condition that cannot exist while the program runs. The error prevents the compiling of the program.
Labview 8.5
Meas Studio 2008
0 Kudos
Message 3 of 5
(3,904 Views)
Anyone? Anyone at all?
Labview 8.5
Meas Studio 2008
0 Kudos
Message 4 of 5
(3,864 Views)

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.

Warm regards,

pBerg
0 Kudos
Message 5 of 5
(3,839 Views)