LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to check if can device is busy writing or reading

Solved!
Go to solution

Hi, I'm using NI USB 8472 Low Speed CAN and I want to check if the device is being used,

because while I send a frame to the CAN device I have a continuous loop sending another frame and I want to know that I'm not writing or reading at the same time as my continuous loop.

This loop is done with an asynctimer.

How can I check if the device is being used to prevent writing or reading at the same time?

 

Regards,

 

Daniel Coelho

 

Daniel Coelho
VISToolkit - http://www.vistoolkit.com - Your Real Virtual Instrument Solution
Controlar - Electronica Industrial e Sistemas, Lda
0 Kudos
Message 1 of 13
(1,914 Views)

Use a common communications function, that is used by all of your threads in a program, and protect the function by using a Critical Section approach. This prevents simultaneous access from different parts of your program.

 

JR

0 Kudos
Message 2 of 13
(1,910 Views)

Hi,

are you talking about using semaphores?

On my program I have a section that is called by the user to write to the CAN device and I have another section that writes every 300 ms using an asynchronous timer.

Are you saying that my write funcion must have a semaphore before and after writing?

Daniel Coelho
VISToolkit - http://www.vistoolkit.com - Your Real Virtual Instrument Solution
Controlar - Electronica Industrial e Sistemas, Lda
0 Kudos
Message 3 of 13
(1,838 Views)
Both Semaphores and Critical Section require the use of SDK functions. In case you want, you can use CVI built-in locking functions that are easier to use: CmtNewLock, CmtGetLock, CmtReleaseLock and CmtDiscardLock.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 13
(1,835 Views)
Solution
Accepted by topic author Daniel Coelho

Whether you choose to use the CVI Lock mechanism or the SDK Critical Section methods, the net effect is exactly the same: a single function which is used by all parts of your program to access a resource atomically. You don't need semaphores here. For example:

 

CRITICAL_SECTION cs;         // Declare at a scope which encompasses all threads wanting to use the resource

 

void Initialise (void) {

 

    ...

    InitialiseCriticalSection (&cs);    // Do this somewhere soon, before you actually use it

    ...

}

 

void CommFunc (...) {           // This function represents the resource which cannot be accessed simultaneously

 

    EnterCriticalSection (&cs); // Stop any other thread from executing past here

    ...                         // Now this block of code can operate without fear of being re-entered

    LeaveCriticalSection (&cs); // Finished - let other threads use the same code

}

 

As my main programming is in Real Time, I tend to use the SDK components, as above. Roberto favours the CVI ones - I believe their operation in this case is identical.

 

JR

0 Kudos
Message 5 of 13
(1,831 Views)

I think I'll go with the CVI built in functions because what I want to do is really simple.

In truth, I'm currently stopping the asynchrounous timer when I want to write and after that I restart the timer and everything seems to work.

But, just in case, how do I use that SDK? Do I need to download and install that SDK to use it or do I just need to include it on my code?

 

Thanks for all the help.

Daniel Coelho
VISToolkit - http://www.vistoolkit.com - Your Real Virtual Instrument Solution
Controlar - Electronica Industrial e Sistemas, Lda
0 Kudos
Message 6 of 13
(1,829 Views)

If your CVI package includes the SDK component (it is a purchase option - often referred to as FDS, or Full Development System) then all you need to do is add: #include "windows.h"  at the top of your program and off you go. If you are more comfortable with the CVI equivalents then they should work just fine as well - after all, CVI is really just a framework which calls into the Windows kernel anyway. (OK, this is an oversimplification and I'm sure it will upset the old Sun and Linux people - sorry!)

 

JR

0 Kudos
Message 7 of 13
(1,826 Views)

My CVI probably doesn't include SDK component because I tried and it didn't work.

I'm including "windows.h" and I get "Missing prototype" error on InitialiseCriticalSection ();

Daniel Coelho
VISToolkit - http://www.vistoolkit.com - Your Real Virtual Instrument Solution
Controlar - Electronica Industrial e Sistemas, Lda
0 Kudos
Message 8 of 13
(1,821 Views)

The Windows SDK (in last version of CVI it's called "Interface to Win32 API") is included only in the full development system: if you are using the base package you haven't it. A comparison of the different distributions can be found here.

Nevertheless, you will probably find locking functions in the Utility library, which as mentioned by JR are equivalent.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 9 of 13
(1,804 Views)

I have NI Developer Suite witch includes Interface for Win32, but I still can't build the code.

Well, that's another post Smiley Wink

I'll go with CVI built in functions.

 

Thanks for your help.

 

Regards,

 

Daniel Coelho

 

Daniel Coelho
VISToolkit - http://www.vistoolkit.com - Your Real Virtual Instrument Solution
Controlar - Electronica Industrial e Sistemas, Lda
0 Kudos
Message 10 of 13
(1,799 Views)