05-12-2010 12:21 PM
Continued from old thread.
Here is my latest version of the WORM Global. For those who are not familiar with this project, a WORM Global is a way to store variables where you can write to them once. WORM stands for Write Once Read Many. This vi holds an array of variants.
You can add any variable at any time. When adding, you write the value to the variable. After adding, you cannot write to it again, there is no mechanism to do so. But you can read the variable as many times as you wish. This prevents race conditions from happening.
It is useful to store constants (variables that are defined and then don't change). An example of its use would be to store GPIB addresses of instruments, datalog file names, config file names, constants flags, and others.
Upon first call, the array of variants is emptied to get rid of leftovers from previous use. It is best to call this vi inside a loop, with two arrays as inputs to the loop: an array of global names and an array of corresponding values. Wire in the Add enum to the Action input. Now the globals are set and can be used anywhere in the program. To use, call the vi again with Read as the action. Wire in the name of the global you want to read. The output is a variant, but can be converted to its proper type using Variant to Data function.
Another action is to Delete. You can delete globals that are no longer needed at any time.
If trying to add a global with a blank name, you will get an error. If trying to read a global with a blank name, you will get an error. If trying to add a global whose name already exists, you will get an error. If trying to read a global whose name does not exist, you will get an error. If you try to delete a global that does not exist, or one in which the name is blank, you will get an error.
Please review the vi and give me some feedback. If you see problems, bugs, or have a better idead, I would love to hear about them.
Solved! Go to Solution.
05-12-2010 12:45 PM - edited 05-12-2010 12:48 PM
I'm wondering if a WORM should prevent deleting a variable and then adding a new one with the same name but a different value. Or, am I worrying too much?
05-12-2010 03:13 PM
There could be valid reasons why someone would want to do this. Maybe some condition could necessitate a change of value. I think I'll leave it as it is. Leave the programmer enough rope to hang himself if he wants to.
05-12-2010 03:21 PM
here's a Variant based on your code.
Ton
05-12-2010 04:16 PM
You give 5 people the same problem and you will get 5 different codes. Array of clusters is what popped up in my head when I started this. Your other points are really trivial and a matter of personal preference. Feel free to use whatever version you wish. I did this for fun, and will probably not use it myself since I am happy with the standard Globals vi. However, it may be of some use to others who are more concerned with race conditions.
05-13-2010 12:23 AM
Yes you are correct 4 of the 5 points were somewhat trivial.
Your code is pretty nice though
An array of clusters is a good method, and I am not sure at what point the performance of the variant attributes will really matter.
Ton
05-13-2010 07:14 AM
jcarmody wrote:I'm wondering if a WORM should prevent deleting a variable and then adding a new one with the same name but a different value. Or, am I worrying too much?
Message Edited by jcarmody on 05-12-2010 01:48 PM
Salut le cousinDuSud,
Great post. I like the idea of WORMs.
If it is Write Once Read Many, why would you allow deleting entries and allowing either a replacement of adding a new one?
Otherwise, you have a mechanism to update the WORM by deleting an entry and re-entering it or to give it a slightly different name.
I would simply allow one shot at entering everything. If it's the wrong value, the programmer needs to fix it at the source, but shouldn't be able to re-write to it; otherwise, it should not be considered Write Once. I'm thinking in the lines of a ROM, not an EEPROM. You could create two types, a WORM and a MWORM (equivalent to an EEPROM).
RayR
05-13-2010 11:02 AM
Hmmm.... You make a valid point. I think I will delete the delete function. Having it only encourages bad programming practice. Also, I will look into making a couple of changes as suggested by Ton. Skip the search for name if name is blank, and remove the terminal for the array output so as not to expose the globals to the outside world. These are all valid points. I'll get to it sometime today.
05-13-2010 11:51 AM
Now your talking... 😄
You're gonna be make a better WORM. 🙂
05-13-2010 01:25 PM
OK here is the long awaited (about 2 hours), much anticipated, new WORM_Globals.vi. Thanx to Ton and Ray and others who have contributed. Now there are only two enum items, Add and Read. No Delete and no Delete All. Since Delete All was only used at first call, I just perform the action in a case structure on first call. Name is checked for blank before any action takes place. If blank, only an error is given, no action performed. The globals array is not passed onto the world, I've deleted the terminal.
Any other suggestions are welcome.