From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Elapsed time in while loop

Solved!
Go to solution

Hi all

   In my code I want to add elapsed time in while loop,how to show time smoothly as sequence. Here is snapshot and comment,for you refer,thanks!

mmexport1604135429413.jpg

0 Kudos
Message 1 of 15
(4,385 Views)

You are only updating your elapsed time indicator when you state changes which is why it jumps so many seconds.

 

You need to put your elapsed time logic in a separate loop which is triggered to stop when your main processing loop stops.

 

I have attached a possibility.

 

As an aside, Rather than using strings for your states, you are much better off creating a enum type def which contains all of your possible states. This removes any possibility of typos, and if you change the elements in your enum your case structure will automatically update or give you an error at compile time rather than a runtime error.

0 Kudos
Message 2 of 15
(4,348 Views)
  • You don't need your inner loop (besides, the number of iterations is known before the loop starts, so a FOR loop would have been more appropriate)
  •  Why do you think you need so many instances if "get time&date (I would use high resolution relative seconds, no need to convert to DBL)
  • Your local variable seems unnecessary.
  • Autoindexing on a while loop is unusual. It is usually done on FOR loops.
0 Kudos
Message 3 of 15
(4,326 Views)

See if this can give you some ideas.

 

altenbach_0-1604179923252.png

 

 

Note: No inner loops! No local variables

 

Second Note: You could just connect your elapsed time to the iteration terminal. Same difference. Above code is more flexible, because you might want to change timing in the future.

0 Kudos
Message 4 of 15
(4,321 Views)
It is very kind of your comments,I need insert serial acquired data while loop in each case instruct,how to set up show elapsed time smoothly as 1s,2s,3s..... ,thanks!
0 Kudos
Message 5 of 15
(4,288 Views)

I have just realised that I didn't attach an altered version of your code in my previous response. See an alternative method here (Based on your new code)

 

Because each state in your main loop takes >1000ms at a minimum (Because of the wait in some of the cases) I don't see a way of doing this other than having a separate loop. You have got to tell this loop when to stop which I have done using a local variable...There are better ways of doing this but this is the simplest as long as you remember to set its value to false before your loops start.

 

A couple of more comments regarding your serial comms:

  • You are opening and closing the session with each read/write combo. You are much better off opening the session at the beginning, leaving it open and then closing it right at the end of your code 
  • Try to avoid using the bytes at port property value. I am guessing that is where the 1500ms wait has come from in order to make sure that your device has written all of its data to the port. If you have a look at the devices serial communication protocol it is likely to do one of the following things
    • Character termination, each message is terminated by a specific character probably a line feed or carriage return or sometimes an ASCII character like a $
      If this is the case use the character termination features of VISA, look in the initial case of the code attached. Here you can set what character you are expecting to be at the end of the message. Not you have to give this as a U8 integer which can be confusing when you are starting out (Google ASCII characters, hexidecimal). By default it is set to terminate with a line feed. When you use the read function if you don't give it a bytes to read it will continue reading until a the termination character is read or a timeout has occurred.
    • Each message is a fixed length. In which case don't use character termination, and just wire the number of characters you are expecting to your read function. This way it will return when it has read all the expected characters or a timeout has occurred.
  • I am not sure why you have got nested case structures here. You could put all of your states in one case structure which will make your code much more readable
  • I am going to reiterate using type deffed enums rather than strings. Removes any risk of typos and makes changing your states much easier (You have spelt initial wrong for example, that incorrect spelling would have to be consistent in both your array of states and case structure and anywhere else it is used) Personally my spelling is so bad that even my incorrect spellings are inconsistent.
  • You are clearing all of your errors. I try and avoid this because you have no idea if your code has worked or not, and if not why not. Have a think about what you want your code to do if it doesn't work
    • Show a message to the user
    • retry the current state
    • end the program
0 Kudos
Message 6 of 15
(4,280 Views)

Well, if some of your iterations take longer than 1 second, you need to place the timing into a seperate loop as has already been said. Also, if you only want to see whole seconds, don't use a format with fractional seconds for the indicator (I round to nearest instead). Do you really need to wait 1500ms for a response??? What kind of connection is this?

 

Why do you think you need so much duplicate code? It is probably sufficient to initialize the serial port before the outer loop (or in the init state). You don't need any of the inner loops. Your item1,2,3 cases differ only by two text constants each, so why do you think you need to duplicate everything!!? Isn't there a termination character? Polling for "bytes at port" is wrong in almost all cases.

 

Here is a very (very!) quick draft that I am sure needs much more work but will give you some ideas. I never use serial, so it would be worth consulting with an expert.

0 Kudos
Message 7 of 15
(4,276 Views)

 

Yours comment are very useful, I have changed queue instruct show tick time.Any thing else to change,thanks for us help me for the issue.

 

20201103082531.png

0 Kudos
Message 8 of 15
(4,229 Views)
Solution
Accepted by topic author Sam.Huang

Wow, now you are way overcomplicating things again with inner loops, local variables, and queues.

All you probably need is half the code!

0 Kudos
Message 9 of 15
(4,223 Views)

Here are some ideas for simplifications.

 

0 Kudos
Message 10 of 15
(4,219 Views)