txturbo Posted October 10, 2017 Share Posted October 10, 2017 I have an application that is used on more than one system. Each system has slightly different hardware but the user interface is the same. How can I use a configuration file to select the proper hardware depending on the system at startup? Quote Link to comment Share on other sites More sharing options...
AzeoTech Posted October 10, 2017 Share Posted October 10, 2017 The way I do it is using a class that contains all the settings, then I instantiate that class once into a global variable, called, say "gSys". Then I can access the settings using gSys.mySetting. The class has a load and save method that saves all the local variables to a json formatted text file. class CSystem local debug = local string rootDirectory = "c:\daqfactory" local mySetting = 5 local AnotherSetting = 6 local string AStringSetting = "Some string" function saveSettings() try private string out = toJson() private handle = file.Open(rootDirectory + "\Settings.json",,1,,1) file.Write(handle, out) file.Close(handle) catch() ? strLastError endcatch endfunction function loadSettings() try private string in private handle = file.Open(rootDirectory + "\Settings.json",1,,,1) in = file.Read(handle) file.Close(handle) fromJson(in) catch("F0002") ? "Settings not found, creating new file" saveSettings() endcatch catch() ? strLastError endcatch endfunction endclass I then instantiate the class in my start up sequence, and call loadSettings on it: global gSys = new(CSystem) gSys.loadSettings() And whenever I change a setting from within the program, I call gSys.saveSettings(). The nice part is that if you add a new setting to your application and then run the application on a system that has an older settings file, the system will still load those settings, and then fill in any new settings with the defaults. Note that I store the root folder as part of the settings. This is a little backwards because until the file is loaded, it doesn't know the root folder, but instead will use the default. Quote Link to comment Share on other sites More sharing options...
txturbo Posted January 19, 2019 Author Share Posted January 19, 2019 I have been trying to get this to work but I get an error. I changed the attributes on file open to replace the blanks from the example above with "0" but that did not seem to help. I tried to set up global variables to match the locals from the class as well. I created a test file. I set up a sequence named createClasses with this: class CSystem local debug = local string rootDirectory = "c:\daqfactory" local mySetting = 5 local AnotherSetting = 6 local string AStringSetting = "Some string" function saveSettings() try private string out = toJson() private handle = file.Open(rootDirectory + "\Settings.json",,1,,1) file.Write(handle, out) file.Close(handle) catch() ? strLastError endcatch endfunction function loadSettings() try private string in private handle = file.Open(rootDirectory + "\Settings.json",1,,,1) in = file.Read(handle) file.Close(handle) fromJson(in) catch("F0002") ? "Settings not found, creating new file" saveSettings() endcatch catch() ? strLastError endcatch endfunction endclass Then I run a start up sequence with this: createClasses() global gSys = new(CSystem) gSys.loadSettings() I get this error: C1000 Channel or function not found: startup Line 3 - Uncaught error in sequence startup sysloadtest.ctl Quote Link to comment Share on other sites More sharing options...
AzeoTech Posted January 19, 2019 Share Posted January 19, 2019 The problem is the second line in your class declaration: local debug = That is invalid. You either need to give it an initial value or don't include the = sign. Quote Link to comment Share on other sites More sharing options...
txturbo Posted January 20, 2019 Author Share Posted January 20, 2019 Thanks, it's working now. 1. I got rid of the local debug = 2. I changed the file open from this private handle = file.Open(rootDirectory + "\Settings.json",1,,,1) private handle = file.Open(rootDirectory + "\Settings.json",,,1,,1) to this private handle = file.Open(rootDirectory + "\Settings.json",1,0,0,1) private handle = file.Open(rootDirectory + "\Settings.json",0,1,0,1) Quote Link to comment Share on other sites More sharing options...
AzeoTech Posted January 20, 2019 Share Posted January 20, 2019 You had #2 done in the document you posted, so all I changed was #1. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.