LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Managing/Preventing Simultaneous Access to Same VISA port

Solved!
Go to solution

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!

0 Kudos
Message 1 of 14
(2,720 Views)

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.

 

http://forums.ni.com/t5/LabVIEW/User-specified-DAQ-interruptions-instrument-control-through/m-p/2604...

 

-AK2DM

~~~~~~~~~~~~~~~~~~~~~~~~~~
"It’s the questions that drive us.”
~~~~~~~~~~~~~~~~~~~~~~~~~~
Message 2 of 14
(2,714 Views)

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…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 3 of 14
(2,708 Views)
Solution
Accepted by topic author _Ben_

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:

RM FBN.png


"Should be" isn't "Is" -Jay
Message 4 of 14
(2,684 Views)

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

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
Message 5 of 14
(2,669 Views)

@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.Smiley Wink  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.


"Should be" isn't "Is" -Jay
0 Kudos
Message 6 of 14
(2,664 Views)


I am having trouble with your snippet but decide to start a new thread

 

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 7 of 14
(2,657 Views)

@RTSLVU wrote:

What am I doign wrong here? I can not seem to figure out how to use a snippet.

 



Jing here

A "Jing on draging to your desktop then draging to your new BD.  (Sorry for the large screen- you may need to scroll to get the full picture):smileywink:


"Should be" isn't "Is" -Jay
0 Kudos
Message 8 of 14
(2,646 Views)

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!

0 Kudos
Message 9 of 14
(2,632 Views)

@_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.


"Should be" isn't "Is" -Jay
0 Kudos
Message 10 of 14
(2,623 Views)