This code below throws a NullReferenceException error time to time. It does not happen always but let's say, at least 2-3 times in 10 tries I get this annoying "System.NullReferenceException" screen.
I'm reading data from a data acquisition card, DATAQ 4208U. When it comes to "stop" command for reading, this error occurs. And the other problem is I am not a master at coding and VB.Net.
the point where it throws the error is at the end, (sure I just quoted the code, it doesnt end there)
Await TargetDevice.ReadDataAsync(cancelRead.Token)
Private Async Sub btnState_Click(sender As Object, e As EventArgs) Handles btnState.Click
If cancelRead IsNot Nothing Then
'Get here if an acquisition process is in progress and we've been commanded to stop
cancelRead.Cancel() 'cancel the read process
cancelRead = Nothing
Await taskRead 'wait for the read process to complete
taskRead = Nothing
Await TargetDevice.AcquisitionStopAsync() 'stop the device from acquiring
Else
'get here if we're starting a new acquisition process
TargetDevice.Channels.Clear() 'initialize the device
ConfigureAnalogChannels()
ConfigureDigitalChannels()
If SampleRateBad() Then
'get here if requested sample rate is out of range
'It's a bust, so...
btnState.Enabled = True
Exit Sub
End If
'otherwise, the selected sample rate is good, so use it. The class automatically adjusts
'decimation factor and the protocol's sample rate denominator to yield a sample rate value as close as possible to
'the value asked for in tbSampleRate.Text. The class also automatically maximizes decimation factor as a function of
'channels' AcquisitionMode settings. For this reason Acquisition mode should be defined for all enabled channels
'before defining sample rate.
TargetDevice.SetSampleRateOnChannels(tbSampleRate.Text)
Try
Await TargetDevice.InitializeAsync() 'configure the device as defined. Errors if no channels are enabled
Catch ex As Exception
'Detect if no channels are enabled, and bail if so.
MessageBox.Show("No enabled analog or digital channels.",
"Configuration Problem", MessageBoxButtons.OK, MessageBoxIcon.Error)
btnState.Enabled = True
Exit Sub
End Try
'now determine what sample rate per channel the device is using from the
'first enabled input channel, and display it
Dim FirstInChannel As Dataq.Devices.DI4208.ChannelIn
Dim NoInputChannels As Boolean = True
For index = 0 To TargetDevice.Channels.Count - 1
If TypeOf TargetDevice.Channels(index) Is Dataq.Devices.IChannelIn Then
FirstInChannel = TargetDevice.Channels(index)
lblDecimation.Text = FirstInChannel.AcquisitionMode.Samples
NoInputChannels = False
Exit For
End If
Next
If NoInputChannels Then
MessageBox.Show("Please configure at least one analog channel or digital port as an input",
"No Inputs Enabled", MessageBoxButtons.OK, MessageBoxIcon.Error)
btnState.Enabled = True
Exit Sub
End If
'Everything is good, so...
btnState.Text = "Stop" 'change button text to "Stop" from "Start"
cancelRead = New CancellationTokenSource() ' Create the cancellation token
Await TargetDevice.AcquisitionStartAsync() 'start acquiring
' NOTE: assumes at least one input channel enabled
' Start a task in the background to read data
taskRead = Task.Run(Async Function()
'capture the first channel programmed as an input (MasterChannel)
'and use it to track data availability for all input channels
Dim MasterChannel As Dataq.Devices.IChannelIn = Nothing
For index = 0 To TargetDevice.Channels.Count
If TypeOf TargetDevice.Channels(index) Is Dataq.Devices.IChannelIn Then
MasterChannel = TargetDevice.Channels(index) ' we have our channel
Exit For
End If
Next
' Keep reading while acquiring data
While TargetDevice.IsAcquiring
' Read data and catch if cancelled (to exit loop and continue)
Try
'throws an error if acquisition has been cancelled
'otherwise refreshes the buffer DataIn with new data
'ReadDataAsync moves data from a small, temp buffer between USB hadrware and Windows
'into the SDK's DataIn buffer. ReadDataAsync should be called frequently to prevent a buffer
'overflow at the hardware level. However, buffer DataIn can grow to the size of available RAM if necessary.
Await TargetDevice.ReadDataAsync(cancelRead.Token)
Catch ex As OperationCanceledException
'get here if acquisition cancelled
Exit While
End Try