Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How does DataSocket.SyncRead really work

I'm trying to synchronously read a text string from a DSTP data socket.  According to the documentation SyncRead is supposed to wait for the data to be updated before returning a false signifying a successful read, it will return a true if the read times out.  I've tried using using this method and it always returns true without actually waiting for the timeout period. 
 
I've got a series of VI's running on a cFP unit.  These VI's poll for information from the DataSocketServer and when they receive it successfully they write an acknowledgement to another DataSocketServer variable.  I've opened two different sockets one with the Write AccessMode anf the other with Read AccessMode. 
 
Here is an example of what I'm trying to do
 

public bool SendData(double data)

{

    // make sure there is no data already waiting for us

    bool result = _rx_socket.SyncRead(100000);

 

    // Send the data

    _tx_socket.Data.Value = data;

    _tx_socket.Update();

 

    // Wait for the acknowledgement

    result = _rx_socket.SyncRead(10000);

    return (result && _rx_socket.Data.Value.ToString() == "OK")

}

When I run this code the first SyncRead returns immediately and _rx_socket.Data.Value contains the value written to the socket data by the cFP VI, the result variable is set to true.  The second SyncRead also returns immediately and _rx_socket.Data.Value contains the old value written to the socket data by the cFP VI, the result variable is set to true. 

According to the docs 'If the buffer contains unprocessed data, the next value is dequeued into the Data

property and the return value is false'.  This doesn't seem to be the case.  Do I need to call the update method as well and if so do I do it before or after the call the SyncRead. 

If I open a socket with the AutoRead access mode I get the correct data, I would prefer to not to go down that path however to avoid having to run a separate thread and do all the required synchronization.

0 Kudos
Message 1 of 5
(3,825 Views)
Hi kgallagher,
 
I will try to create a small datasocket app to see if I get the same behaviour on my side. On a side note along with setting the accessmode to read/write, ensure you are also setting the ReadMode property to "Synchronous".
 
I will post here soon. Thank you for your patience.
 
SijinK
National Instruments.
0 Kudos
Message 2 of 5
(3,801 Views)
Hi kgallagher,
 
I wanted to follow up. Two things, ensure that you are setting the ReadMode property to "Synchronous". This is the common cause for a lot of errors. Also another note. When you perform a Write, you need to call the update method, then before you perform a read (SyncRead) you need to call the update method again.
 
I am sure that this should fix your issue.
 
SijinK
National Instruments
0 Kudos
Message 3 of 5
(3,794 Views)
Unfortunately it does not.  I have set the ReadMode to 'Synchronous' (an aside the docs don't mention this property has to be set prior to connecting or it is ignored) and it still doesn't work.  I have tried it with the AccessMode set both Read and ReadAutoUpdate as well as with calling and not calling Update.  I always get the same results it returns immediately with False, indicating a successful read.  This is actually impossible because there are no processes that have opened my particular socket with write access much less actually writing to the socket.  The Data property is always an integer data type with a value of 0.  
 
Did you try this out and did it work.?
If you open a socket and don't write to it does the SyncConnect function timeout? 
 
As a work around I'm going to have set the ReadMode to Asynchronous and AccessMode to ReadAutoUpdate and then set some sort of event, flag, etc once I get data in the DataSocket.DataUpdated event.  My first pass at this does not work because everything is running in the same thread and I'm assuming my WaitHandle.Wait(timeout) function is blocking the socket from actually sending the DataUpdated event.  Is there a way to have the DataSocket fire event from a different thread than the one that created the socket (The SynchronizationObject property is null)?  Is there any sample code out there for trying to accomplish this?  I'd greatly appreciate any help with a workaround.
 
0 Kudos
Message 4 of 5
(3,779 Views)

Hi KGallagher,

I believe that the one setting that you are missing is to set the AccessMode to ReadBufferedAutoUpdate.  Before, if you were just doing a simple read, the data was not being buffered so there was no data to pick up.  This way, it will automatically buffer all the data being written.  I have put together a small example for you.  All I have done is change the datasocket example that ships with MeasurementStudio so that it uses a SyncRead.  I made sure to also set the ReadMode to Synchronous and the AccessMode to ReadBufferedAutoUpdate.  If you start the Writer and select Connect(AutoUpdate) then you can press the Connect(AutoUpdate) on the Reader and then everytime you press "Update" the SyncRead will read data from the buffer.  (I have placed Message Boxes within the code to show whether we have received data.  You can also select to Connect (Mannual Update) on both Reader and Writer and then select Update on the Writer and then the Reader and receive data.  If you were not to select Update on the Writer but then selected Update on the Reader, there would be no data in the buffer thus you will see a timeout.

Try this example out and let me know if you have any more question.

 

Thanks,
Caroline Tipton
Data Management Product Manager
National Instruments
0 Kudos
Message 5 of 5
(3,753 Views)