Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

NI VISA Async EndRead() issue with TCPIP connection (C#)

Hi all, so I've been working on talking to another PC on a TCPIP session, and I've been having issues reading responses.  The session opens, and any commands I send are received and the PC responds as it is supposed to.  The problem is that I am unable to "read" that response in code - all I am looking for is "OK".  Any attempts I have made result in a IviVisaTimeoutException being thrown.  I also noticed recently that only the RawIO interface works; FormattedIO does not, even though the write calls "succeed".  This morning, I tried an async read, and I got somewhere with it:

 

[code]

char[] b = new char[1024];
IVisaAsyncResult ivar = dev.Session.RawIO.BeginRead(1024);
ivar.AsyncWaitHandle.WaitOne();
long l = 0;
l = dev.Session.RawIO.EndRead(ivar);
for (int a = 0; a < ivar.Buffer.Length; a++)
    {
    if (ivar.Buffer[a] != 0)
        {
            b[a] = Convert.ToChar(ivar.Buffer[a]);
        }
    else
        {
            break;
        }
    }

[/code]

 

I have to use BeginRead(int count), because BeginRead(byte[] buffer) does not write to the buffer - presumably, because the EndRead() call never completes.  BeginRead(int count) does its job, because when the WaitOne() call executes, I can see 'ivar' being updated with the results.  The ivar buffer contains the "79" and "75" characters, which correspond to "OK" - that's what I want.  If the EndRead() call executes, I instantly get a n"IviVisaTimeoutException".  However, if I skip the "EndRead()" call in the debugger and go through the loop, I can translate the bytes to chars as desired.  I'm just not sure why the EndRead() call bombs.  Could anyone perhaps provide some insight?

0 Kudos
Message 1 of 2
(440 Views)

Update:

 

I used this code:

 

string response = String.Empty;


IVisaAsyncResult ivar = Session.RawIO.BeginRead(1);
bool bSignal = ivar.AsyncWaitHandle.WaitOne();
long l = Session.RawIO.EndRead(ivar);
response += Convert.ToChar(ivar.Buffer[0]);

 

This works.  I can read the "OK", but, beyond that, if I attempt to read and it times out (probably because nothing is there), all subsequent read attempts will fail.  It's like some sort of status is set but I can't find where......I have to close the session and reopen it.

0 Kudos
Message 2 of 2
(409 Views)