02-05-2014 09:47 AM
Hello,
I currently have a VI with three parallel loops: a loop for the user interface with an event structure; a loop for data collection; and a loop for data plotting.
The data collection loop writes to the serial port to ask the device to send an updated sensor reading. The device sends the updated sensor reading, and the data collection loop reads the devices reply.
I have a situation where the user interface loop also needs to 1. write to the same device (via the same port), 2. read the reply, and then 3. write again using data from the reply and user input. My problem is the user interface loop is reading the devices reply to the data collection loop rather than the reply to the user interface loop.
So the problem looks like:
data collection loop: How hot is it?
device: 30 Celsius
data collection loop: How hot is it?
device: 30 Celsius
data collection loop: How hot is it?
user interface loop: What color is the sky?
device: 30 Celsius
user interface loop: Ok! the color of the sky is 30 Celsius.
device: blue
I am tempted to pursue a solution where the user interface loop somehow pauses the other two loops while it is writing and reading to the VISA port, but I doubt this is the proper solution. My code is rather large, but I can try to post it if necessary. I remember seeing posts from people with similar problems, but I seem unable to find them today. Thanks in advance for the help!
Solved! Go to Solution.
02-05-2014 09:55 AM
I have used Semaphors in the past, see my attachment here:
Upper loop polls for data at regular intervals.
Lower loop sends configuration commands as needed.
This is just sandbox code I was playing with to prototype a larger app.
-AK2DM
02-05-2014 10:00 AM
Hi Ben,
I suggest to put all VISA Port communication in it's own loop and sending commands and data via queues/notifier to your UI/DAQ loop.
This has the advantage of creating a single loop handling all the device communication, and so being able to do all things in correct order! Sharing a single resource (like your VISA port) to several threads/loops for random access will always be tricky…
02-05-2014 11:05 AM
Ben's famous Action Engine Nugget is always a good read. http://forums.ni.com/t5/LabVIEW/Community-Nugget-4-08-2007-Action-Engines/m-p/503801
Now that you went through that let's expand on a specific style of AE that I call a Resource Module. A RM exposes only those functions that a given application needs to perform using an external resource while protecting "Critical" code sections. In your example even though the VISA operations block multiple callers from writing simultaineously, you failed to protect the critical "Query" operation so reads got responses to the wrong caller. The "Query" is critical and needs to block access to other operations until the query is complete.
Take look at this "Very Basic" RM implementation that does protect the critical query operation:
02-05-2014 11:28 AM
Jeff, I like that! But just one small question - I'm sometimes not very good at acronyms - what's the FBN in your diagram?
Thanks,
Cameron
02-05-2014 11:32 AM
@camerond wrote:
Jeff, I like that! But just one small question - I'm sometimes not very good at acronyms - what's the FBN in your diagram?
Thanks,
Cameron
Feedback Node. You could also use a Shift Register and a while loop but, the FBN has some slight advantages that are out of scope for this thread.
02-05-2014 11:41 AM - edited 02-05-2014 11:46 AM
I am having trouble with your snippet but decide to start a new thread
02-05-2014 11:47 AM
02-05-2014 12:00 PM
Jeff, thank you for your reply. You very eloquently described my problem, and I'm excited to implement the solution you suggested. I am actually working on a different project at the moment, but hopefully I will be able to get to this tomorrow. I have a few follow up questions, but I will wait to ask them until after I've read and fully digested Ben's Action Engine post.
I appreciate everyone's help!
02-05-2014 12:16 PM
@_Ben_ wrote:
Jeff, thank you for your reply. You very eloquently described my problem, and I'm excited to implement the solution you suggested. I am actually working on a different project at the moment, but hopefully I will be able to get to this tomorrow. I have a few follow up questions, but I will wait to ask them until after I've read and fully digested Ben's Action Engine post.
I appreciate everyone's help!
There is also a very good explaination of "LV2 style globals" and race conditions and "Critical" code (Didn't Darren N Write it?) Somewhere (in the community?). I can't find it So
Kudos to anyone with the link.