LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

2 loops accessing serial port

Hi there,

I have a little issue with parallel while loops accessing the same hardware on a serial port.  My two loops both open a connection to my datataker and interogate channels.  One loop interogates some channels every 10s and the other loop tries to access the datataker every 60sec.  The problem is I think they conflict when they try and execute at the same time.

Is there a way I can get them to attempt to execute only if the other loop is finished?  Essentially queing them only if the other is executing. 

 

I did think about a boolean flag when a loops executing and a case structure in both loops which wont let it execute until the flag is false (for instance).  I figure there is a better way than this.

 

Let me know what you think.  My code is attached.

 

 

 

 

0 Kudos
Message 1 of 9
(3,597 Views)

If I was doing this, I'd make it so there was only one loop doing all the serical communication, and it would distribute data to the other loops, possibly using queues.  A 60 second loop wait in your second loop will feel like it takes forever to stop the program.  (probably almost 2 minutes)

 

I would probably just put everything into 1 loop running at a 1 second interval, and only execute the code is the appopriate time has elapsed based on the 2 sampling rates.

 

Without making major changes to your code, you could also use semaphores before and after each serial subVI so that only one or the other can execute at any instant.

 

By the way, the index Array node is expandable.  (You have already expanded all of them and not used the 2nd output).  By expanding them, you can use one Index Array function to get all 7 of your elements.

0 Kudos
Message 2 of 9
(3,590 Views)

Reglar readers of my post are saying (Ben you sound like a broken record).... but here we go again.

 

An Action Engine (see this link)  will help out.

 

Create an AE with method like

 

Init - Opens serial port and saves visa ref in shift register

Query1 - It use the VISA ref from the SR to send the query and wait for the reply.

Query2 -It does your other query and waits for the repsonse.

 

If you read that Nugget on Action Engines you will see that an Action Engine uses a mutex in the back ground to enusre only one caller is active at a time. That Nugget exaplains that behind the scenes a mutex is used. So your thought process was going the right way. AEs are the way to execute your idea.

 

 

Ben

Message Edited by Ben on 03-13-2009 09:06 AM
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 3 of 9
(3,585 Views)
Edit: They have covered everything I wrote.

 

Message Edited by Adnan Z on 03-13-2009 07:32 PM
Adnan Zafar
Certified LabVIEW Architect
Coleman Technologies
0 Kudos
Message 4 of 9
(3,584 Views)

Ben wrote:

Reglar readers of my post are saying (Ben you sound like a broken record).... but here we go again.

 

An Action Engine (see this link)  will help out.

 


Ben, I think you should be in line for an award for most linked to nugget.Smiley Very Happy

Message 5 of 9
(3,573 Views)
I agree with ben Action Engins or functional globals are a great way to make a lock around critical code section and prevent race conditions in concurrent programs.
Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 6 of 9
(3,567 Views)

Ravens Fan wrote:
...

Ben, I think you should be in line for an award for most linked to nugget.Smiley Very Happy


Thanks!

 

That is exactly why I wrote it. I think it was worth the time.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 7 of 9
(3,562 Views)

Ben wrote:

Thanks!

 

That is exactly why I wrote it. I think it was worth the time.

 

Ben


Let me personally thank you for writing it.  I had just started using functional global variables and your nugget inspired me to make better use of them.

 

 

 

 

Back to the original question.  If I remember, both loops in the OP's program used the same subVI to encapsulate the communication with the serial device.  The subVI wasn't attached so I couldn't look into it.  Was is set to be reentrant?

 

I would say if that subVI was not re-entrant, then the communication in the two loops should not be interefering with each other.

0 Kudos
Message 8 of 9
(3,558 Views)

Hi there,

Wow you guys are quick on the reply, especially for a saturday.  Evidently I have exposed my limited knowledge of Labview and it is bringing me unstuck left right and centre.

I'll work through a few of the ideas mentioned on monday when I have my datataker next to me.  

 

I see someone mentioned I didn't add the sub VI's.  How do I save all of the necessary files to the one file so anyone could run that program?  

 

Cheers


Wes

0 Kudos
Message 9 of 9
(3,531 Views)