Bicky22 Posted January 13, 2014 Share Posted January 13, 2014 Hi, I had a simple question. I just was looking for a way to simplify my code. I want the to read the channel where the number defined by a variable... In the simplest form I am trying to make: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if (Var.ActNum==1) read(check1) // channel 1 endif if (Var.ActNum==2) read(check2) // channel 2 endif _______________ into something like: _______________ Var.number=Var.ActNum read(check(Var.number)) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I also hope this works for variables: something like: ________________________________________ Var.number=Var.ActNum Var.on(Var.number) = 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I hope this is clear enough. Thank you. Link to comment Share on other sites More sharing options...
AzeoTech Posted January 13, 2014 Share Posted January 13, 2014 You want to use the execute() function for this: execute("read(check" + var.number + ")") Execute takes a string and executes it as if it was a line of script. You can assemble the string using constant strings and variables like I did. Link to comment Share on other sites More sharing options...
Bicky22 Posted January 13, 2014 Author Share Posted January 13, 2014 Awesome. I knew there would be a simple way. Thank you, I appreciate the quick response. Link to comment Share on other sites More sharing options...
Bicky22 Posted January 21, 2014 Author Share Posted January 21, 2014 I am still having a trouble with the syntax I was hoping you could help. How do I correctly script: Var.number=1 // defines unit number Var.strname= ”Unit” // defines unit name Var.strAction=”Action” // defines action name execute("Var.strPos + var.number =99") // set unit1 = 99 if (("Var.strPos + var.number”== 99) // I want to set this so Unit# can vary. // do x execute("Var.strAction + var.number = “Did x") // I want to set this so Action# can vary Else // do y execute("Var.strAction + var.number = “Did y") Endif Link to comment Share on other sites More sharing options...
AzeoTech Posted January 21, 2014 Share Posted January 21, 2014 A couple problems: 1) you don't want your variable names inside quotes, otherwise it just executes it as is. So: execute("Var.strPos + var.number =99") will simply run the command: Var.strPos + var.number =99 which does nothing. You need to build up the string using the contents of those variables: execute(Var.strPos + doubleToStr(var.number) + " =99") Likewise with the if(). It should look the same as the execute, except replace execute() with evaluate() and the = with == Finally, I don't know what you are doing with the Did x and Did y. For one thing you are using upsidedown quotes which is invalid in DAQFactory because its not in standard ASCII. Its fine inside the string, but not as part of the execute. If you want to embed quotes inside of a string, consider using either ', or concating: execute(var.strAction + doubleToStr(var.number) + " = 'Did x'") or: execute(var.strAction + doubleToStr(var.number) + " = " + '"Did x"') Remember ' and " can be used interchangably, but they need to match up. You can't do: myString = 'abc" Link to comment Share on other sites More sharing options...
Bicky22 Posted January 28, 2014 Author Share Posted January 28, 2014 I am still having a some issues.. Like you explained before I can use execute("read(check" + var.number + ")") to read specific channels. I am having a problem assigning the result to a variable. such as: Var.Result= execute("read(check" + var.number + ")") Thank you Link to comment Share on other sites More sharing options...
AzeoTech Posted January 28, 2014 Share Posted January 28, 2014 Two things: 1) read() doesn't return anything. It just triggers the read. After you do read(), you can access the channel by its name. 2) execute() doesn't return anything either. It is designed to run statements. If you want to determine the result of a function, use evaluate(). So, you want: execute("read(check" + var.number + ")") var.result = evaluate("check" + var.number + "[0]") Also, note that using var. notation for variables is quite deprecated. Use variable declaration instead: global result private number execute("read(check" + number + ")") result = evaluate("check" + number + "[0]") Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.