void VisaEmitter::EIO_Open(GenericBaton* data) { char temp[QUERY_STRING_SIZE]; ViStatus status = -1; if (this->isConnected) { _snprintf(temp, sizeof(temp), "Already connected %s", session); ErrorCodeToString(temp, status, data->errorString); return; } status = viOpenDefaultRM(&defaultRM); if (status < VI_SUCCESS) { _snprintf(temp, sizeof(temp), "Opening RM"); ErrorCodeToString(temp, status, data->errorString); return; } status = viOpen(defaultRM, data->command, VI_NULL, this->timeoutMiliSeconds, &session); if (status < VI_SUCCESS) { _snprintf(temp, sizeof(temp), "Opening session %s", data->command); ErrorCodeToString(temp, status, data->errorString); return; } status = viSetAttribute(session, VI_ATTR_TMO_VALUE, this->timeoutMiliSeconds); if (status < VI_SUCCESS) { _snprintf(temp, sizeof(temp), "Setting timeout to %d", this->timeoutMiliSeconds); ErrorCodeToString(temp, status, data->errorString); return; } this->isConnected = true; // status = viSetAttribute(instr, VI_ATTR_SEND_END_EN, VI_TRUE); // terminate reads on a carriage return 0x0a 0x0d // LF (Line feed, '\n', 0x0A, 10 in decimal) // Carriage return, '\r', 0x0D, 13 in decimal // status = viSetAttribute(session, VI_ATTR_TERMCHAR, 0x0A); //status = viSetAttribute(session, VI_ATTR_TERMCHAR_EN, VI_TRUE); if (this->assertREN) { viGpibControlREN(session, VI_GPIB_REN_ASSERT); } if (this->enableSRQ) { m_async = uv_async_t(); m_async.data = this; uv_async_init(uv_default_loop(), &m_async, reinterpret_cast<uv_async_cb>(aCallback)); isAsyncInitialized = true; status = viInstallHandler(session, VI_EVENT_SERVICE_REQ, callback, this->uniqueSRQhandlerIdentification); if (status >= VI_SUCCESS) { status = viEnableEvent(session, VI_EVENT_SERVICE_REQ, VI_HNDLR, VI_NULL); } if (status < VI_SUCCESS) { _snprintf(temp, sizeof(temp), "Post AfterOpenSuccess session %s", data->command); ErrorCodeToString(temp, status, data->errorString); return; } this->installedSRQHanlder = true; } _snprintf_s(data->result, _countof(data->result), _TRUNCATE, "%d", session); }
int main (void) { /* * First we open a session to the VISA resource manager. We are * returned a handle to the resource manager session that we must * use to open sessions to specific instruments. */ status = viOpenDefaultRM (&defaultRM); if (status < VI_SUCCESS) { printf("Could not open a session to the VISA Resource Manager!\n"); exit (EXIT_FAILURE); } /* * Next we use the resource manager handle to open a session to a * GPIB instrument at device 2. A handle to this session is * returned in the handle inst. Please consult the NI-VISA User Manual * for the syntax for using other instruments. */ status = viOpen (defaultRM, "GPIB::2::INSTR", VI_NULL, VI_NULL, &inst); /* * Now we install the handler for asynchronous i/o completion events. * To install the handler, we must pass our instrument session, the type of * event to handle, the handler function name and a user handle * which acts as a handle to the handler function. */ status = viInstallHandler (inst, VI_EVENT_IO_COMPLETION, AsyncHandler, uhandle); /* Now we must actually enable the I/O completion event so that our * handler will see the events. Note, one of the parameters is * VI_HNDLR indicating that we want the events to be handled by * an asynchronous event handler. The alternate mechanism for handling * events is to queue them and read events off of the queue when * you want to check them in your program. */ status = viEnableEvent (inst, VI_EVENT_IO_COMPLETION, VI_HNDLR, VI_NULL); /* * Now the VISA write command is used to send a request to the * instrument to generate a sine wave. This demonstrates the * synchronous read operation that blocks the application until viRead() * returns. Note that the command syntax is instrument specific. */ /* * Here you specify which string you wish to send to your instrument. * The command listed below is device specific. You may have to change * command to accommodate your instrument. */ strcpy(stringinput,"SOUR:FUNC SIN; SENS: DATA?\n"); BytesToWrite = strlen(stringinput); status = viWrite (inst, (ViBuf)stringinput,BytesToWrite, &rcount); /* * Next the asynchronous read command is called to read back the * date from the instrument. Immediately after this is called * the program goes into a loop which will terminate * on an i/o completion event triggering the asynchronous callback. * Note that the asynchronous read command returns a job id that is * a handle to the asynchronous command. We can use this handle * to terminate the read if too much time has passed. */ status = viReadAsync (inst, data, 1024, &job); printf("\n\nHit enter to continue."); letter = getchar(); /* * If the asynchronous callback was called and the flag was set * we print out the returned data otherwise we terminate the * asynchronous job. */ if (stopflag == VI_TRUE) { /* RtCount was set in the callback */ printf ("Here is the data: %*s", RtCount, data); } else { status = viTerminate (inst, VI_NULL, job); printf("The asynchronous read did not complete.\n"); } printf ("\nHit enter to continue."); fflush(stdin); getchar(); /* * Now we close the instrument session and the resource manager * session to free up resources. */ status = viClose(inst); status = viClose(defaultRM); return 0; }