01-11-2012 06:50 AM - edited 01-11-2012 06:55 AM
#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>
#include <visa.h>
int __stdcall InitLibrary(void);
ViStatus _VI_FUNCH IntrHandler(ViSession vi, ViEventType eventType, ViEvent context, ViAddr userhandle)
{
printf("An event was received.\n");
return VI_SUCCESS;
}
int __stdcall DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
if (InitCVIRTE (hinstDLL, 0, 0) == 0)
return 0; /* out of memory */
InitLibrary();
break;
case DLL_PROCESS_DETACH:
CloseCVIRTE ();
break;
}
return 1;
}
ViSession defaultRM;
ViSession session;
int __stdcall InitLibrary(void)
{
ViStatus status;
status = viOpenDefaultRM (&defaultRM);
if (status < VI_SUCCESS) {
printf("Could not open a session to the VISA Resource Manager!\n");
return 0;
}
status = viFindRsrc(defaultRM, "PXI?*INSTR{VI_ATTR_MANF_ID==0x1234 && VI_ATTR_MODEL_CODE==0x1234}", &findList, &numInstrs, instrDescriptor);
if (status < VI_SUCCESS){
printf ("An error occurred while finding resources.\n");
return status;
}
status = viOpen(defaultRM, instrDescriptor, VI_NULL, VI_NULL, &session);
if (status < VI_SUCCESS) {
printf ("An error occurred opening a session to %s\n",instrDescriptor);
return status;
}
status = viInstallHandler(session, VI_EVENT_PXI_INTR, IntrHandler, VI_NULL);
if (status < VI_SUCCESS) {
printf("Could not install the interrupt handler.\n");
viClose (defaultRM);
return -1;
}
printf("Installed the interrupt handler.\n");
status = viEnableEvent(session, VI_EVENT_PXI_INTR, VI_HNDLR, VI_NULL);
/// MY SYSTEM STOPPED HERE!
if (status < VI_SUCCESS) {
printf("Could not enable the interrupt event (0x%x).\n", status);
viClose (defaultRM);
return -1;
}
printf("Enabled the interrupt event.\n");
return 1;
}
Solved! Go to Solution.
01-12-2012 08:04 AM - edited 01-12-2012 08:05 AM
I already wrote that when I call the function viEnableEvent with the parameter VI_QUEUE, the event is attached.
If I activate VI_QUEUE and VI_HNDLR like:
viEnableEvent(session, VI_EVENT_PXI_INTR, VI_QUEUE|VI_HNDLR, VI_NULL);
I get an error code BFFF00A4h (VI_ERROR_NSUP_MECH, The specified mechanism is not supported by the given event type.) and the RT target restarts automatically after 2 seconds.
Maybe this information will help...
Thanks
Hauke Webermann
02-15-2012 06:56 AM
When the DLLMain thread call viEnableEvent, I think it is created a special thread with the event handler function and the DLLMain thread suspends on a condition or semaphore. So the main thread waits for the new thread is started. The new thread sends an event or a signal to the DLLMain thread and wakes it up. But I called the viEnableEvent in my initialization in the DLLMain during DLL_PROCESS_ATTACH. During this time no other threads can run and so the main thread waits for a thread which can’t start.
As a solution I have now created a thread that’s starts even after the initialization process and initialize the interrupt event.