LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Error 1 at Write to Text File within For Loop

Solved!
Go to solution

RavensFan -- you are (not surprisingly) correct about the Feedback Node and that Option setting.  So where is the Cycle?  I don't see it, but then, there's a Stacked Frame Sequence, where wires go in and out "chaotically" (instead of neatly through Shift Registers or Tunnels, and can be arranged to encourage straight parallel wires, making inputs and outputs "obvious", another reason that NI "banned" the Stacked Sequence Frame to discourage such chaos).

 

Almost all of the Frames (all but Frame 5) appear to do almost nothing, setting or getting a few (often Local) Variables, or Property Nodes, most with no "wired" (from outside) inputs.  Frame 5 has massive code, many inputs, but because they are in a Frame structure and not straight, it is extremely difficult to say what the wires coming into the Frame are originating outside the encompassing Case Structure.

 

Assuming that this code is supposed to do something reasonably important, and will need to be maintained and possibly modified, I would urge you to do the following:

  • Isolate Frame 5.  Starting from the center and working outward, start encapsulating code elements into sub-VIs to begin to bring Order out of Chaos.
  • Try to identify a "function" or a "reason" for each bit of code that you make into a sub-VI.  There is something called the Single Responsibility Principle that says (I'm paraphrasing) "A Sub-VI should do One Thing and Do It Well".  Give your sub-VIs Icons (a simple Box with 3 short text lines is fine, "Find Max" or "Fit Sine" or "Get Final Value", so you'll be able to "see what is happening" by reading the Icons of the sub-VIs lined up sequentially on the Error Line.
  • Use the default 4-2-2-4 Connector Pane on all of your sub-VIs, with Error In and Error Out at the lower corners.  Use the Error Line.
  • Keep your wires as straight and horizontal as possible.
  • Your goal is to have many fewer wires (can you say "Cluster"?) and code (sub-VIs and wires) that fit in a Laptop Screen.
  • Rather than using Local Variables as though they were, say Matlab or C variables, and causing some confusion by reading and writing them in multiple Frames, why not use a wire (strung between Shift Register terminals) as the Variable?  Wires (in LabVIEW) are for Data, and Loop + Shift Register = Memory.

Note that this "advice" can be summed up in two words (which I have said to some of my LabVIEW colleagues, most of who are still speaking to me) -- Start Over!  The steps above are one way to "replace from the inside .out", but it is often easier and more effective to design from the "outside in".  But do keep the Single Responsibility Principle in mind -- at every level, you want to say "What does this Code (or sub-VI) do", and "bury" inside it the Messy Details of How it does it.  As you start coding the How, see if that can't be broken down into simpler "One-Thing" Steps.  Simple Code is Simple to Test, Simple to Understand, and Simple to Debug (but it might be so intuitively obvious that it needs no debugging).  Building the next level from known Simple Code That Works ultimately leads to Top Level Code that Works.

 

Bob "Philosopher" Schor

 

 

Message 21 of 27
(877 Views)

Caveat about starting over:

Sometimes badly written code depends on bad things happening to make it work right.  If/when the attempt is made to refactor this code, be very careful that the refactored code functions the way it used to.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 22 of 27
(874 Views)

I will only be working on this project for 8 weeks or so, so I probably won't get around to rewriting much of the code. I am pretty much tasked with fixing some inherent issues with the code and integrating the code for use within another system. Only about 50% of my LabVIEW time goes to working on this code directly, and usually it is with a specific task in mind; however, it definitely could use some overhauling in general.  

0 Kudos
Message 23 of 27
(860 Views)
Solution
Accepted by topic author etvg

For anyone curious, the issue had nothing to do with the I/O code, as Bob showed it should be working correctly. The issue actually was that one wires going into the "Format into String" function was coming from inside the for loop, so it was giving the "is a member of a cycle" error. Creating a value property node and wiring that to the "Format into String" which creates the file name solved the issue. 

 

Thanks everyone!

0 Kudos
Message 24 of 27
(843 Views)

😞 An object lesson in why it's so important to write well organized, modular code.  It's difficult to find even a simple issue like that if you also have to concentrate on wading through disorganized code.  I'm sorry you were made to suffer someone else's foolishness.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 25 of 27
(831 Views)

I think tomorrow I'm going to take all of the separate areas of code, most of which are in sequence structures, and actually make them subVIs which they should have been in the first place. I believe doing this will make the code a lot more manageable to work with, as it will be 1/4 if not less of it's current size due to code in stacked sequence 5. 

Message 26 of 27
(828 Views)

When you do that, be sure to use the 4-2-2-4 Connector Pattern, put Error In and Error Out on the lowest L/R terminals, and connect the VIs in sequence via the Error Lines.  If you can do something similar with the code in Sequence 5 (maybe with a few simple Loop/Case structures and more sub-VIs), then you can be rid of the Sequence and have a chance at getting something where you can "see at a glance" what (should be) happening.

 

I think (and sincerely hope) you will be pleased with this effort!

 

Bob Schor

0 Kudos
Message 27 of 27
(814 Views)