LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

loop to wait indefinitely in a dll, is there a more elegant or a less resource consumer way to program it?

Hi there,

 

I am developing a driver for a 3rd party electronic card to work in real time. In order to obtain the driver, I programmed a dll in labwindows to detect interrupts and the dll is called from labVIEW. The dll basically waits until an interrupt occurs in a PXI and when it detects one the program returns some data to labview and change the configuration of the card to a safe mode. In order to wait the interrupt, a loop has been programmed with the Sleep function until a interrupt is detected:

 

while (flag == 0) {
      Sleep (1000);  
}

 

And when an interrupt is detected, the interrupt handler (the function that immediately attends the interrupt) performs some operations and at the end it changes the value of the variable flag (flag=1;), so the program will exit from the loop and will continue the execution. Probably this isn't the best way to program it but at least I tested it and it's working fine. If I check the total load of the PXI with the program running while it's waiting the resources consumed are not to high; it changes from 0 to 1%. 

 

I am not an expert in labwindows, so I would like to know if there is a better way to perform the same task without the loop, like for example, wait for an event to occur instead of the loop and when the interrupt handler is going to finish generate this event to continue the execution. Also, if an interrupt is detected, I would like to continue the execution of the function without waiting the time configured when I call the function Sleep (imagine that an interrupt occurs just 1 ms after the Sleep function and the interrupt handler is called. The program attends the interrupt and after it returns to the function and the program has to wait the rest of the time configured)

 

Thanks for the help,

Jaime

 

 

 

 

0 Kudos
Message 1 of 4
(3,581 Views)

@JPolo wrote:

 

... I programmed a dll in labwindows to detect interrupts and the dll is called from labVIEW. The dll basically waits until an interrupt occurs in a PXI ... In order to wait the interrupt, a loop has been programmed with the Sleep function until a interrupt is detected. 


Hi,

 

Interrupts are literally invented to avoid the looping.

 

 

Why don't you let the interrupt handler do the task directly instead of changing the flag?

 

S. Eren BALCI
IMESTEK
0 Kudos
Message 2 of 4
(3,546 Views)

Hi Eren,

 

When an interrupt occurs, the program immediately calls the interrupt handler without wait any loop but maybe in my first post I didn´t explain my problem clearly. 

 

I execute the function interrupt with a call library function node in labview. The function configures the card, enables interrupts and waits for an interrupt with the loop.

 

char* interrupt(void)

{

1. Open visa communications

2.Install handler interrupt --> status = viInstallHandler (instr, VI_EVENT_PXI_INTR, IntrHandler, VI_NULL);   // the function IntrHandler will be called when an interrupt occurs

3. Enable event PXI interrupt

4. Wait

 

while (flag == 0)

        {

              Sleep (1000);

        }

 

5. Visualize the data coming from the interrupt (registers and values measured with the card)

6. Uninstall handler interrupt

7. Close visa session

 

}

 

When an interrupt is detected, immediately the next function; IntrHandler, is executed. 

 

ViStatus _VI_FUNCH IntrHandler(ViSession instr, ViEventType etype, ViEvent event, ViAddr userhandle)

{

1. Disable some functions of the card to avoid damages. 

2. Read registers and put them in a buffer

3. Change the value of flag ---> flag = 1;

}

 

When the interrupt has been attended, the program returns to the function interrupt, exits from the loop and closes the communications with the card.

 

Here is the program in labview, http://forums.ni.com/ni/attachments/ni/270/12158/1/capture.png  As you can see the function is called just when it starts and then executes the sequence that I described before. So my question is if it would be possible to detect in labview when the interrupt has been attended or if there is a way to wait for an interrupt inside the function without program the loop. Because I don´t know when a interrupt could happen and if i don´t wait for the interrupt in the code, the program will close the communication with the card and it will return void data to labview.

 

I tried to explain my problem more clearly, but of course, if it is not clear enough, don´t doubt to ask me.

Cheers,

Jaime

 

 

 

 

0 Kudos
Message 3 of 4
(3,539 Views)

Thank you for the clarification.

 

The interrupt() function you call from LabView is doing too much work.

It has 3 different parts, so I would divide it into 3 functions.

 

  1. initialize() function, which consists of only the first 3 items of interrupt() and you should call it once at the beginning of execution.
  2. uninitialize() function, which consists of items 6 and 7 of interrupt() you should call it just before terminating the execution.
  3. process() function, which consists of items 1 and 2 from IntrHandler() and item 5 of interrupt().

The wait loop is not required.

You will configure the interrupt handler in initialize() function and it will run when an interrupt occurs.

What you should do is to return from the interrupt handler as quick as possible, but the new process() function will possible take considerable time to execute.

 

For that purpose, in CVI, one would use a deferred call to a thread in CVI. This passes the work of the process() function to a worker thread.

But it looks like your main code is in LabView, not in CVI. If that is the case, I cannot be very helpful from that point forward.

 

Otherwise, you can call PostDeferredCall or PostDeferredCallToThread, whichever suits you better.

The wait loop must be implemented by the thread you post process() function to.

This is accomplished either implicitly in RunUserInterface() function's event processing loop or explicitly coded as a

{Sleep(some); ProcessSystemEvents();} kind of loop in a separate thread.

 

Hope that was clear enough.

Regards,

S. Eren BALCI
IMESTEK
0 Kudos
Message 4 of 4
(3,483 Views)