Jump to content
Sign in to follow this  
marius

Saving and loading a file with fix path, by using a sub

Recommended Posts

Hello,

I have the following problem in my project: Because of the size of my code and not only, I have to use subs.

The attached file save-02A.vi uses the sub sub-save-02.vi. This sub has three inputs: two booleans, LOAD and SAVE and an array of clusters.

My problem is with the array of clusters: When I send it in sub to save it is logically ok (however save-02A doesn't work and I don't know where is the error), but when I do a load in sub, how can I get the new array content from the sub? In other words, how can I send the pointer of this array in sub, so that I can use the same memory space for save and for load? How can I pass not a copy of the array in sub, but its address? I know how to do it in other languages, it is easy, but in Labview documentation I did not find this explanation.

Thank you very much.

Marius

Download File:post-5076-1165199023.vi

Download File:post-5076-1165199044.vi

Share this post


Link to post
Share on other sites

Hi Marius,

I looked at the VIs. I am not sure what the task is. May be you can describe what you are trying to achieve, i.e, the functionality desired -- without going into the implementation details. This should help us program it.

Regards,

-Khalid

Share this post


Link to post
Share on other sites
Hi Marius,

I looked at the VIs. I am not sure what the task is. May be you can describe what you are trying to achieve, i.e, the functionality desired -- without going into the implementation details. This should help us program it.

Regards,

-Khalid

Hello Khalid, hello all,

save-02A.vi has two tasks: Saves the array under the default name (see the path - here if you want to make it run, you need to write another path, valid in your computer) and Loads the array by calling the same default path. save-02A.vi calls the subprogram sub-save-02.vi. The problems here are: 1.this subprogram, when called, doesn't work. 2. When you try to use it for saving the logic is ok, you send the array in the subprogram. But when you want to load the array, you need to have the array back from the sub, as a returned value, and I don't know how to do it. In other words, I need to send the pointer (the address of the array) in the subprogram, so I can use the array there for saving, or for modifying it (in case when I load it from the file), so I got it modified back in the main program.

To note that I already tried the same code, by incorporating all into a main program, no subprogram, and it works fine. So the problem is with the passing the parameters to and from the subprogram.

Can you or somebody else help?

I badly need to know how to do this, because I need to use it in other applications right away.

Can you or anybody else help me?

Thank you very much,

Marius

Share this post


Link to post
Share on other sites
... But when you want to load the array, you need to have the array back from the sub, as a returned value, and I don't know how to do it. ...

To get the output from a subVI into its caller VI, the most staright-forward option is to create an output terminal on the subVI, and use that in the caller VI.

Hope this helps.

-Khalid

Share this post


Link to post
Share on other sites

:oops:

To get the output from a subVI into its caller VI, the most staright-forward option is to create an output terminal on the subVI, and use that in the caller VI.

Hope this helps.

-Khalid

Hello Khalid, all,

Thank you for your quick answer.

How I understand the situation until now is:

- if you have a sub and you want to pass a parameter to it, you assign a CONTROL of this sub to a collector of the sub and that collector becomes an INPUT. Later on you can connect a CONTROL from the main program to it.

- if you want to get a parameter from the sub, you assign an INDICATOR of this sub to a collector of the sub and that collector becomes an OUTPUT. Later on you can connect an INDICATOR from the main program to it.

Now, my problem is little different:

I want to pass the pointer (address) of my array to the sub, so that, WHITIN the sub, I will be able to:

1. save the array on the file. In this case I use the pointer above as an INPUT. No array modification. Just get it from the main and save it.

2. Load new values into array, from a file. In this case I use the pointer above as an OUTPUT.

Questions:

How to correct my sub attached in my first "e-mail" here, so that it responds to the above 1,2 ?

How to address the sub from the main, once the sub corrected?

A quick answer will be greatly appreciated,

Thank you very much,

Marius

Share this post


Link to post
Share on other sites
:oops:

Hello Khalid, all,

Thank you for your quick answer.

How I understand the situation until now is:

- if you have a sub and you want to pass a parameter to it, you assign a CONTROL of this sub to a collector of the sub and that collector becomes an INPUT. Later on you can connect a CONTROL from the main program to it.

- if you want to get a parameter from the sub, you assign an INDICATOR of this sub to a collector of the sub and that collector becomes an OUTPUT. Later on you can connect an INDICATOR from the main program to it.

Now, my problem is little different:

I want to pass the pointer (address) of my array to the sub, so that, WHITIN the sub, I will be able to:

1. save the array on the file. In this case I use the pointer above as an INPUT. No array modification. Just get it from the main and save it.

2. Load new values into array, from a file. In this case I use the pointer above as an OUTPUT.

Questions:

How to correct my sub attached in my first "e-mail" here, so that it responds to the above 1,2 ?

How to address the sub from the main, once the sub corrected?

A quick answer will be greatly appreciated,

Thank you very much,

Marius

Basically, from your example code and questions, you need to learn more about data flow programming. I'd recommend reading LabVIEW for Everyone (in the interest of full disclosure, one of the authors is my boss ;) ). It is a good book and will get you beyond your initial LabVIEW hang ups.

First, you have to understand one of the major points of data flow programming... data goes in, data goes out. First, let me explain the problems you're having in (sub_save_02.vi)... You arent able to read the data in your subVI because you are writing the value to a control (Initialized Array) but LV does not by default read values from a control. Instead, you should create an indicator and write data to the indicator (call it: Data Out or something so you can differentiate it from the data you write (Data In) and wire it to the connector pane of the subVI. Also, you probably want the File Path to be an Input to your SubVI so that you don't have to overwrite the same file every time... right now its a constant. I would also change the inputs to the SubVIs from booleans to a single Enum value (numeric palette) and have a case for 'Read', 'Write'. The do nothing case should be managed from your main VI (save_02A).

Second, your Main VI (save_02A.vi) is a bit of a wreck as well (no offense). In the interest of writing a long spiel about how to clean it up, or fixing it on your behalf, I would refer you to the LabVIEW Examples section - take a look at the Read/Write Binary Data examples. Also, look at state machine examples. If you don't find them in 7.1, you can definitely find them on LAVA and NI Websites. It will help you to understand why, for example, your loop doesn't run more than one iteration.

Lastly, as a data flow language, LabVIEW doesn't support 'pointers' directly. If you want to make LabVIEW simulate using pointers, you need to do other things and I wouldn't recommend any of them based on your example code. Instead, just relax and remember that data is passed along the wires. It goes into the subVI, is modified (or not) and comes out of the subVI and back into your 'main' application. LabVIEW uses explicit declaration of inputs and outputs based on the wiring of your front panel controls (inputs) and terminals (outputs) to the connector pane. Unlike other programming languages where a function argument can be both an input and an output, in LabVIEW this is not the case (there are ways to make it behave like this but they are not something you should do normally).

Good luck!

Share this post


Link to post
Share on other sites
My problem is with the array of clusters: When I send it in sub to save it is logically ok (however save-02A doesn't work and I don't know where is the error), but when I do a load in sub, how can I get the new array content from the sub? In other words, how can I send the pointer of this array in sub, so that I can use the same memory space for save and for load? How can I pass not a copy of the array in sub, but its address?

You don't. There is no way to specify the address of anything in LabVIEW. Just wire it through. If the compiler detects that the data that flows in eventually flows out then the compiler will take care of making it use the same memory space. If the wire forks at some point, the compiler will decide whether a new copy is needed or whether a single allocation can fulfill both branches of the wire (if, for one simple example, both branches are only reading the array, not modifying it).

If a copy is made, then it is because a copy is needed to do the code that you've written. If you don't need it, the compiler shouldn't allocate it.

You'll find that the bulk of LabVIEW users will stare at you blankly when you start talking about addresses and pointers in memory. That's because there's no reason for them to pay attention to such stuff when working in LabVIEW in all but the most extreme situations. The nature of dataflow is pretty cool from a computer science perspective: Memory is allocated where the wire starts, and deallocated where it ends. Along the way, new copies spawn as needed for the code written.

There are caveats to the above and moments when really advanced programmers trying to squeeze every drop of performance will turn on various advanced tools to tune the memory allocations, but even then they are just tuning when copies get made, not explicitly handling memory addresses. But in the vast majority of cases, LV will do a better job scheduling memory than a person could in such a highly parallel environment.

Automatic memory management. Parallel code execution. Optimal thread scheduling.

Just some of the benefits of a dataflow language. :wub:

Share this post


Link to post
Share on other sites

Hello Omar, Aristos, hello all,

Your comments are helpful to me and I think that now I understand more. Probably you need to know that most of my experience is in text code (C, QB), under dos and VB under windows.

Now, I need to complete this project in LabVIEW and I have fully read the user manual, but I find it not very hepful.

I will attach here the following files, making a very simple example that doesn't work and I don't know why.

- save-03A.vi

- sub-save-02.vi

The first vi calls the second one, as a sub.

What save-03A.vi supposes to do (but it doesn't) is to allow to the user to fill an array of two clusters, to specify a path and when it presses on "save", the sub has to save the array.

Unfortunatelly it doesn't save and the program stops after one iteration, with no error shown.

Can you please explain me where I should repair and why doesn't work?

I am very obset with it, since in other languages would take minutes to accomplish such a simple job, but I have to use this LabVIEW which I like it for its capability to build GUIs.

A quick and clear answer will be highly appreciated.

Thank you very much,

Marius :wacko:

Download File:post-5076-1165343915.vi

Download File:post-5076-1165343938.vi

Share this post


Link to post
Share on other sites

Download File:post-5076-1165432636.viDownload File:post-5076-1165432666.viDownload File:post-5076-1165432678.viHello all,

Finally I got it. Take a look to load-save-array-01.vi: Allows you to specify a path, change values in array, then save the array. Also you can load the array configuration from the file. If you look at its diagram, you wont see any subVI

Share this post


Link to post
Share on other sites

Marius,

You should look at the Event Structure in LabVIEW, and use that instead of the While Loop. With the current implementation you are polling the Save and Load buttons all the time in the While loop. With Event Structure you would get an event whenever any of these button's values change (after you set it up that way). Plus, with your Vb background, you will like Events :)

As for passing the array reference into the subVI and then using a Property Node, I guess it can be considered similar to using a pointer. But, you don't want to do that in LabVIEW unless you have a good reason. Because, Property Nodes execute in the UI thread (which is other than the diagram thread), and this will cause a thread-switching which is expensive. Here's a comparison of Terminal versus Property Node versus Local variable:

Local variable = 15 times slower than Terminal

Property node = 653 (!!!) times slower than Terminal

The above is from the following post: http://forums.ni.com/ni/board/message?boar...essage.id=90261

Depending on how long you will be working with LabVIEW, you may want to stop thinking in terms of C and VB, and start thinking in terms of data-flow programming.

Regards,

-Khalid

Share this post


Link to post
Share on other sites
Local variable = 15 times slower than Terminal

Property node = 653 (!!!) times slower than Terminal

The above is from the following post: http://forums.ni.com/ni/board/message?boar...essage.id=90261

Depending on how long you will be working with LabVIEW, you may want to stop thinking in terms of C and VB, and start thinking in terms of data-flow programming.

Regards,

-Khalid

Khalid,

I thank you for your pertinent remarks and for your time spent for me. I really appreciate it.

Even before, starting with LabVIEW I've felt that something is wrong with my approach and mostly that I need to change the way of thinking in this environment.

I am an old dog, frozen in text code and dos approach where I worked for years. I don't have enough Windows programming experience.

I will try to upgrade my thinking. Can you recommend a good manual for this issue?

Thanks,

Marius

Share this post


Link to post
Share on other sites
Can you recommend a good manual for this issue?

Hi Marius,

I must commend your willingness to learn. Here are some basic resources for learning LabVIEW:

http://www.ni.com/academic/lv_training/how_learn_lv.htm

http://forums.lavag.org/knowledgebase.html&showarticle=3

After this, you will want to pick up a good book, like Jim Kring's LabVIEW for Everyone. Of course, monitoring these forums and participating in the discussions is invaluable as well.

Good luck!

-Khalid

Share this post


Link to post
Share on other sites
Hi Marius,

I must commend your willingness to learn. Here are some basic resources for learning LabVIEW:

http://www.ni.com/academic/lv_training/how_learn_lv.htm

http://forums.lavag.org/knowledgebase.html&showarticle=3

After this, you will want to pick up a good book, like Jim Kring's LabVIEW for Everyone. Of course, monitoring these forums and participating in the discussions is invaluable as well.

Good luck!

-Khalid

Hello Khalid and thank you very much for your comments and info. Very appreciated.

Marius

Share this post


Link to post
Share on other sites

Join the conversation

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

Guest
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.

Sign in to follow this  

×
×
  • Create New...

Important Information

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