Jump to content

Problem re-entering an inner while loop


Recommended Posts

I have a While Loop that reads data from the serial port. I then have another While Loop within this that processes the data received from the port one line at a time - an iteration for each line. The inner loop uses the match pattern function and extracts values before passing on the 'after substring' to be processed in the next iteration. The condition of the inner loop is stop when this string length is 0. This works until the first time the string is 0 then I cannot re-enter the inner loop when more data needs processing. I think it must be the loop condition but I am relatively new to labview and am not sure the best way round it! Any suggestions would be great :-) Thanks, Liz

Link to comment

QUOTE(lizimunro @ Mar 6 2008, 01:10 PM)

I have a While Loop that reads data from the serial port. I then have another While Loop within this that processes the data received from the port one line at a time - an iteration for each line. The inner loop uses the match pattern function and extracts values before passing on the 'after substring' to be processed in the next iteration. The condition of the inner loop is stop when this string length is 0. This works until the first time the string is 0 then I cannot re-enter the inner loop when more data needs processing. I think it must be the loop condition but I am relatively new to labview and am not sure the best way round it! Any suggestions would be great :-) Thanks, Liz

Hi,

No offense, but I would suggest that you try to clean up the code a bit, since it will make debugging much easier.

* Try to keep wires straigh and visible (not hidden behind structures etc.)

* don't use sequence structures to determine the data flow, this is in most cases handled by the wires automatically.-

* add error handling, so that you can detect and handle errors, e.g. returned by the VISA read

* runtime in sec, will always retuirn a value close to zero, since there is no way for LabVIEW to know that the second timer should be executed after the outer while loop

Second, I think that the reason your code does not work is due to one of the following;

1. Match string uses \n to find newline character, but this is given in normal notation, right-click on the constant and select \-code display instead (if you do that you'll see a string of "\\n", i.e. a two character search, but maybe this is as it should be?)

2. There is no handling in the case that the match pattern function fails (perhaps due to that we have not read enough characters from the serial port)

3. VISA read returns an error that is left unhandled (I guess this is unlikely though)

Good Luck

/J

Link to comment

QUOTE(lizimunro @ Mar 6 2008, 07:10 AM)

I have a While Loop that reads data from the serial port. I then have another While Loop within this that processes the data received from the port one line at a time - an iteration for each line. The inner loop uses the match pattern function and extracts values before passing on the 'after substring' to be processed in the next iteration. The condition of the inner loop is stop when this string length is 0. This works until the first time the string is 0 then I cannot re-enter the inner loop when more data needs processing. I think it must be the loop condition but I am relatively new to labview and am not sure the best way round it! Any suggestions would be great :-) Thanks, Liz

Aristos is correct - lose the sequence frames. They just make it hard to look at.

My instinct would be to decouple the read operation from the string processing completely. This would definitely be easier to debug.

I would probably create a queue where the Elements are a cluster of an enum and the resultant string data.

A successful VISA Read would Enqueue the Element including the string result. An unsuccesful Read would Enqueue error information for possible use in the Processing loop.

The Processing loop would be a separate While Loop (not nested) where the data would be dequeued and processed.

To build sucessful string processing code you would need some data to simulate the expected results from the Read operation.

When this code is able to perform properly, THEN you put it in a while loop and feed it a series of data blocks and make sure that works.

Then you bring in the Queue and Case statement and process the data as it is produced.

If you are not aware of how queues work this implementation is equivalent to a FIFO buffer (1st In 1st OUT). The Read operation can produce as many bocks of data as it can, as fast or slow as it can, and the processing loop just takes it one step at a time. If it finishes all the queued blocks it just waits until the next one arrives.

In side the Processing loop you have a case statement containing the processing code and at least one other frame fro processing a Stop command which would be generated in the Read loop and Enqueued there.

I would try to show this, but I can't really run your read function without your hardware.

Maybe you could show us some real or simulated strings from the Read operation.

Also, your start test button doesn't do anything. When you run the VI the loop starts running immediately without allowing time for you to choose a different duration and test type.

If you really want to change the Test Type and Duration after the VI is running you should investigate the use of User Event structures. I would advise you do that anyway, they are too important to remain ignorant about, particularly if you are creating GUI Front panels that the operator is expected to interact with during run time.

Mike

Link to comment

Thanks for all the advice, it is very useful as I am new to coding in Labview (as you can tell!) and the housekeeping aspect is not really covered in the books.

I have kind of solved my problem. I have changed a few functions but I put a delay of 5000 ms in as well because it was trying to read the bytes at port too quick. I need it to read a whole line so I can extract the values I want and it was reading 2 or 3 bytes at once.

I have attached the updated vi. I tried to attach the block diagram but it didnt work so I will investigate that..sorry.

Thanks again for all the advice.

Liz :-)

Link to comment

Liz,

Just as an example, I did some more cleanup of your last attachement. I did not made any functional changes whatsoever.

post-121-1204829989.png?width=400

A good house keeping rule is: remove everything that distract from understanding what the code do. Very often this end up meaning keep wire straight, remove unnecessary bend in wire, align primitive when possible etc...

You might find this usefull.

Cheer

PJM

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.