LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using .NET StreamReader ReadAsync

I am trying to use the .NET streamReader to read byte arrays from a device. This works fine when using the Read() function.

 

BramB_0-1700730969346.png

The buffer is (partly) filled with all available data, but when connection is lost when entering the Read function, Labview get locked (off course).

Therefore I want to use the ReadAsync() method.

BramB_1-1700731213486.png

This works fine as the task is completed and I see the number of available bytes in Result, IsCompleted is True and Status = Ran to Completion. But how can I extract the read bytes. The buffer output of the ReadAsync method contains all zero off course because at that point the task hadn't been finished.

 

BramB_0-1700732145485.png

 

In .NET documentation the following example in C# is shown using the await opeartor, but I can't find out how I can implement this in Labview.

 

0 Kudos
Message 1 of 6
(1,025 Views)

You probably should tell it what to read, and if it succeeds, you don't get an error.

wiebeCARYA_0-1700734223768.png

 

Mildly tested, not completely working. Because I use the ReadAsync, I probably have to wait... As it is I still get zeros.

 

The do something like await, you probably use getAwaiter:

wiebeCARYA_1-1700734394110.png

 

0 Kudos
Message 2 of 6
(1,014 Views)

Note that a .net wait (any wait) will block LabVIEW for the wait time. I've even seen blocked FPs, event structures not working and other weirdness.

 

I'd use a LabVIEW wait if possible. 

 

The async .net proccess shouldn't be blocking LabVIEW.

0 Kudos
Message 3 of 6
(1,011 Views)

Thanks for the suggestions, but I couldn't get it working perfectly either. I have implemented some extra check with the ordinarly Read function and that works fine upto now. But it is not completely save I guess in case connection is lost right between the 'EndOfStream" check en the Read.

 

 

BramB_2-1701418496252.png

 

 

 

0 Kudos
Message 4 of 6
(897 Views)

@BramB wrote:

But it is not completely save I guess in case connection is lost right between the 'EndOfStream" check en the Read.


You can check with a breakpoint between the nodes.

 

I wouldn't be too surprised if the streamreader handles that asynchronously. Reading data might just work even if the writer was closed.

0 Kudos
Message 5 of 6
(890 Views)

@BramB wrote:

I am trying to use the .NET streamReader to read byte arrays from a device. This works fine when using the Read() function.

 

If I may, what sort of device is this and why do you wish to use a StreamReader to read "byte arrays" from it?

 

Are you able to implement this reliably in .NET? If so, the easiest way may be to just build a wrapper library for it.

 

Unfortunately, I don't think you can really use ReadAsync() in LabVIEW. LabVIEW is being a little too helpful here by allowing you to pass an U16 Array as the buffer argument. I say this because I do believe this would work if you could pass the buffer argument as a .NET object, but I do not believe LabVIEW allows this. The issue is that the method call is already automatically marshaling back and forth between the LabVIEW array and the .NET char[]. What you really want is to hold a reference to the .NET char[] object and then convert it to LabVIEW data after the async operation completes.

 

There might be a way to force it to work, but I suspect there may be a better solution.

0 Kudos
Message 6 of 6
(868 Views)