LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple instrument measurement recommendations

Hello Menchar,

 

I have to face the problem of different threads trying to communicate with the same instrument. An action on one thread can interfere with the action of another. In my case the Main thread is the problem as it communicates with all connected instrument while other threads created by the NI driver communicate each with a different inst. How to disable/suspend the Main thread ?? This is in .net (c++/CLI, c#).

 

Thanks alot,y.

0 Kudos
Message 11 of 19
(1,681 Views)

Hey yanyan,

 

You may want to explore creating and releasing Locks in your code. Its a little hard to tell if this will work without more description of your application, but you could have your secondary thread create a lock and have the main thread wait until the lock is released to execute. 

 

Here is a great reference to information on multithreading in CVI that you may find useful

 

 

Luke W

0 Kudos
Message 12 of 19
(1,661 Views)

Hey Luke,

 

First of all thank you very much for your quick response, I really appreciate it.

The link you sent me is very informative. The thing is I already briefly read about these kind of locking and got the impression that it's useful only when couple of threads are running the same block of code. Did I get it wrong ?

 

In my case, the callback is being called when device error appears and all it does is to output the device's error queue. The problem is that while this thread is running, the main thread is also running - they both talk to the same instrument and this is a problem. I can't get even a simple query right, as when one thread is writing the query, the other thread might be reading the response.

 

If you think that locks are the answer, I will be very happy as this seem simple to implement.

Thanks again, yanyan 

0 Kudos
Message 13 of 19
(1,656 Views)

Hey yanyan,

 

The locks can be used if several threads are running the same block of code, but it can also be used in other instances when threads are trying to access the same resources. I think thread locking is a great option for you to explore. The help on the function shows a small example of how locks can be used on different sections of code. 

 

 

Hope this helps,

 

Luke W

0 Kudos
Message 14 of 19
(1,645 Views)

Thanks again Luke, I will definitely look into that. This subject requires some learning to do..

By the way, I wanted to ask another question if I may.

 

I work mainly with Agilent instruments and it seems they all act in a very forgiving way towards user action. I mean, in case the user is trying to set a parameter value which is not appropriate for any reason, the instrument in turn will correct it and re-set this parameter value to the closest available. This is a very nice, flexible and forgiving way to work with the instruments.

 

But what if I want to work in a very strict mode of operation. I mean, in case the user is trying to set a parameter value which is not appropriate for any reason, the instrument will invoke an error message in turn.

 

The trivial way I guess is to write a value to the instrument and read it back right away to verify they are equal. Is there another and better way you can think of maybe ?

 

Thanks alot for your help. y.

0 Kudos
Message 15 of 19
(1,639 Views)

I think to change the way the driver behaves will take some development regardless of how you do it. You could try reading in the value and storing it as a variable, run a test on the variable and then, if all is correct, feed that data into the actual functions. Like I said, changing the behavior of the instrument driver will take some development, so depending on how neatly packaged you want it and how versatile you want it to be, you will have to spend different amounts of time developing. Since the driver already works, I think doing checks on input variables and then feeding them into the instrument commands (external to the driver) may be a good place to start. That way the driver stays intact.

 

Luke W

0 Kudos
Message 16 of 19
(1,619 Views)

Thanks alot for that idea Luke.. I have to do some thinking ..

0 Kudos
Message 17 of 19
(1,613 Views)

You can use a mutex to control access to the specific instrument or the entire bus, whatever you design it to do.

C# has some built-in facilities that may be easier to use than a mutex - a lock mechanism is a built into C# (lock is a C# keyword that uses the Monitor class).  You lock an object and only one thread can own the lock at any one time.  I'm pretty sure Microsoft implemented this as a critical section - it only works on threads within a single process.  Mutex works across multiple processes, but is slower than a lock but you probably don't care.  There's also a Mutex class you can use, and I suspect Microsoft wrapped a Windows API mutex to implement the class.

 

The way to use these is that every time a thread wants to communicate with the instrument of interest, have the thread grab the lock beforehand, and release it afterwards.  A thread won't get ownership of the lock unless it's available (no other thread has it) so once you own the lock you're good to go.  There's several methods / techniques available for checking if a lock is available, waiting for it, etc.

 

There are complexities of course, but if you are using only a single lock you can avoid most of them.  With an interpreted environment like C# (the CLR) locking isn't as straightforward as with pure sequential code - mutual exclusion is a procedural concept, not an OO concept so doing mutex in an OO language is always a bit funky.  Java has the same problem.

 

You might want to get a copy of C# 4.0 In A Nutshell ISBN 978-0-596-80095-6  - it is a very good reference and has a chapter on threads and synchronization that describes the use of locks and mutexes.

 

 

0 Kudos
Message 18 of 19
(1,609 Views)

Thanks alot for your detailed answer Menchar, you and Luke really helped me, I really appreciate that..:)

0 Kudos
Message 19 of 19
(1,599 Views)