01-15-2009 04:44 AM
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
Solved! Go to Solution.
01-15-2009 05:50 AM
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
02-05-2009 08:25 AM
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?
02-05-2009 08:48 AM
02-05-2009 09:09 AM
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
02-05-2009 09:29 AM
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.
02-05-2009 09:38 AM
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
02-05-2009 09:54 AM
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 ();
02-05-2009 10:21 AM
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.
02-05-2009 10:54 AM
I have NI Developer Suite witch includes Interface for Win32, but I still can't build the code.
Well, that's another post 😉
I'll go with CVI built in functions.
Thanks for your help.
Regards,
Daniel Coelho