Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

CNiVisaSession::Read() Serial asynchrone, is it possible?

Hello,

System: Measurement Studio 6.0

We want to use Serial with VISA in asynchrone mode (with handler if possible). We have made a small program (based on CDialog) to test it, but every time we call CNiVisaSession::Read(,,jobID) (jobID for asynchrone mode!), it returns VisaSuccessIoSync, why? (what conditions are missing; event queue in place of handler; or serial VISA cannot be asynchrone at all?)

In fact, we want that CNiVisaSession::Read() returns immediately and uses asynchrone mode (actually, it blocks).

We use the following code (in class CMyClass):
Header:
class CMyClass: public CDialog
{
....
ViByte m_nSerialBuffer[255];

CNiVisaJobId m_nSerialJobId;
CNiVisaSession m_nSerialSession;

void InitNIVisaSerial();
ViStatus OnNIVisaEvent(CNiVisaEvent& nEvent);
};

Body:
CMyClass():m_nSerialSession("ASRL1::INSTR", VisaExclusiveLock){}
...
void CMyClass::InitNIVisaSerial()// called from OnInitDialog
{
// initialize serial
m_nSerialSession.SetAttribute(VisaSerialBaudRate, (uInt32)9600);
m_nSerialSession.SetAttribute(VisaSerialParity, VisaParityNone);
m_nSerialSession.SetAttribute(VisaSerialDataBits, (uInt16)8);
m_nSerialSession.SetAttribute(VisaSerialStopBits, VisaStopOne);
m_nSerialSession.SetAttribute(VisaSerialFlowControl, VisaFlowNone);

m_nSerialSession.SetAttribute(VisaTimeout, (uInt16)5000); //time out of 5 secs
try {
// we want the 0x0D character termination
m_nSerialSession.SetAttribute(VisaSerialEndIn, VisaEndTermChar);
m_nSerialSession.SetAttribute(VisaTerminationChar, (uInt8)0x0D);

// OnNIVisaEvent is called on completion,ok
CNiVisaEventHandlerId nID;
m_nSerialSession.InstallEventHandler(VisaEventIoComplete, *this,
OnVisaEventHandler(CMyClass, OnNIVisaEvent),
nID);

m_nSerialSession.EnableEvent(VisaEventIoComplete, VisaHandler);
}
catch (CNiVisaException* e)
{
e->ReportError();
e->Delete ();
}
}
.....
// called from Read button
void CMyClass::OnReadBtn()
{
// here we send asynchrone read, but every time
// the command complete in synchrone mode, for which raison?
ViStatus nStatus = m_nSerialSession.Read(m_nSerialBuffer, 255, m_nSerialJobId);

if(nStatus == VisaSuccessIoSync) // go always in this condtion after timeout
AfxMessageBox("Reading synchrone");
}

Thanks a lot,
Alain
0 Kudos
Message 1 of 2
(3,205 Views)
Alain:

NI-VISA performs asynchronous I/O differently based on the bus type and whether you are using the queue or the handler. For Serial, to make the callbacks work efficiently, there is a threshold level that you need to set in MAX. Go to the Tools menu and choose the VISA options. Set the Minimum Async Transfer to a smaller number of bytes (probably 250) and then try your application again. It should now do the I/O in another thread, and that is the thread where your callback should occur. You can verify this in NI Spy.

Dan Mondrik
Senior Software Engineer, NI-VISA
National Instruments
Message 2 of 2
(3,205 Views)