LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Continuously reading serial data in a loop

Hi all, 

 

Forgive me I'm a noob, trying to make my way here.

 

I have a sound level meter that I am interrogating over serial to make up a little measurement system.

 

I have a series of serial commands I want to make, and so far I've been using a flat sequence to do so, one after the other:

 

*RST = reset the meter

INIT START = Start measurement cycle

MEAS:START = start the measurment required

MEASure:TIMEr? = get the amount of time ran for, when enough, move on

INIT STOP = stop the measurment. 

 

The issue I am having is the cblock where I need to continuously interrogate for the time to stop it when it gets to the correct level. At the moment I have it in a for loop, and it is measuring until it hits a level specified by a constant on a greater than. This is supposed to trigger a boolean to stop the larger loop.

 

The problem I am having is that while my for loop is iterating, it doesnt' seem to be continuously reading in the data for time. I need help making it continuopusly read...

 

Any help would be greatly appreciated, VI attached.

 

Thanks!

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

What you describe is a State Machine, code that runs and does different things depending on what "State" (RST, INIT START, etc.) it is in.  If you go to LabVIEW Help and look for State Machine, you'll see that there is a Sample Project called Simple State Machine.  This should give you some very helpful ideas to help accomplish your task. 

 

BS

Message 2 of 9
(3,932 Views)

Thanks Bob, I'm looking into state machines as a concept. They look fairly complex compared to what  I have now potentially. Will they allow me to loop a read on serial?

 

 

Thanks for the help so far!

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

If I understand you, you will probably need two loops.  Depending on when exactly you want to acquire data, you may need to communicate between the loops.  Probably a local/shared/global variable, queue, etc.

 

[State Machine Loop] --> Issues commands listed in your OP

                       ^

                       |

          Communication

                       |

                      V

[Data Acquisition Loop] --> Continuously reads data
 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If someone helped you out, please select their post as the solution and/or give them Kudos!
0 Kudos
Message 4 of 9
(3,906 Views)

I don't see the point of the FOR loop.  The WHILE loop that it is in should be good enough.  As is now, you have to request and read the time 9,999,999 times.  That will be a VERY long time.

 

A state machine is not complicated.  It is actually a lot more powerful and flexible.  With the sequence structure, you are stuck having to go step by step.  With a state machine, you can skip around states and even repeat easily.  So let's say you get an error somewhere.  With the sequence structure, you still have to go through each case.  With a state machine, you can go straight to the error handling routine and/or abort.  You should definately go this route.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 9
(3,898 Views)

The problem I'm getting however is that it doesn't read it more than once. It gets into that loop, and the loop iterates, but it doesn't read more than once, I can't work out why...

 

I take your point about the state machine structure, but going step by step isn't a problem for this project. Even if this code is within a state machine, I can't see how I can get it to continuously read over and over...

0 Kudos
Message 6 of 9
(3,894 Views)

Here is my simple state machine example. Take a look at it and see if it gives you a better understanding of a simple state machine. 

 

This one was made to contimually record data to a file and create a new file every day. 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 7 of 9
(3,891 Views)

Read the manual for your device.  Check to see if it can only accept so many messages at a given rate.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 8 of 9
(3,880 Views)

Leeps,

 

The code looks like it should do what youre asking.  A state machine would give you a lot more flexibility, but that is up to you. It may not be working properly because youre getting unexpected values.


Suggestion for debugging would be to turn on highlight execution and probe the response read, since im sure thats the read youre refering to as only happening once.  There is a possibility that the device isnt even sending any data even you are utilizing it properly.



-Matt
0 Kudos
Message 9 of 9
(3,554 Views)