Sign in to follow this  
Followers 0
rajsiyer

Citect.. How to use string substitutions in Cicode functions

7 posts in this topic

Hi Guys, I wish to know if there is a method by which one can use string substitutions in Cicode functions.. It is a very useful thing, when used with structured tag names. In my current project, there are 200 Energy meters, each having 8 tags, hence 1600 tags. Each group of 8 tags has the same pattern ie <prefix> being the identity of a given meter. <prefix>_Vavg, <prefix>_Iavg, <prefix>_MaxkW, <prefix>_kWh, <prefix>_kVAr, <prefix>_kW, <prefix>_RunHrs I wish to log this data onto a SQL database table named "METER_LOG", carrying out the logging actions for each meter in a record, that has these fields(Columns) |Meter_Id |----|Vavg|---|Iavg|----|MaxkW|----|kWh|----|kVAr|----|kW|----|RunHrs| My goal now to do this in a loop, so that all the logging actions are done with a few lines of code. There is a table-"METER_IDs" that gives the list of all Meter_IDs or <prefix> And the code I wish to implement is a cicode function that would look like this.- It is not correct, I've used genie substitution syntax and that won't work here. FUNCTION LogAllMeters() int hSQL; string %Cur_ID%;// --This is the string that will be substituted in the loop hSQL = SQLConnect("DSN=<DSN_name>; UID =xxxx; PWD=yyyy"); IF hSQL <> -1 THEN Status = SQLExec(hSQL, "SELECT Meter_ID FROM METER_IDs ORDER BY Meter_ID"); IF Status = 0 THEN WHILE SQLNext(hSQL) = 0 DO // --Scan till the end of the list for all meters %Cur_ID% = SQLGetField(hSQL, "Meter_Id"); INSERT Into METER_LOG VALUES ( %Cur_ID%, %Cur_ID%_Vavg, %Cur_ID%_Iavg, %Cur_ID%_MaxkW, %Cur_ID%_kWh, %Cur_ID%-kVAr, %Cur_ID%_kW, %Cur_ID%_RunHrs); END SQLEnd(hSQL); SQLDisconnect(hSQL); END ELSE SQLDisconnect(hSQL); Message("Error", SQLErrMsg(), 48);END//----outer IF End// of FunctionI would greatly appreciate it if someone could guide me with a way to substitute strings so that the structured tags are correctly referenced inside the loop. Thanks a million for looking in.

Share this post


Link to post
Share on other sites
Try this, FUNCTION LogAllMeters() int hSQL; string sCur_ID //Declare a string for the substitution. The Tag suffix is then concatenated with the prefix by using the + operator eg sCur_ID+"Vavg" hSQL = SQLConnect("DSN=<DSN_name>; UID =xxxx; PWD=yyyy"); IF hSQL <> -1 THEN Status = SQLExec(hSQL, "SELECT Meter_ID FROM METER_IDs ORDER BY Meter_ID"); IF Status = 0 THEN WHILE SQLNext(hSQL) = 0 DO // --Scan till the end of the list for all meters %Cur_ID% = SQLGetField(hSQL, "Meter_Id"); INSERT Into METER_LOG VALUES ( sCur_ID, sCur_ID+"_Vavg", sCur_ID+"_Iavg", sCur_ID+"_MaxkW", sCur_ID+"_kWh", sCur_ID+"-kVAr", sCur_ID+"_kW", sCur_ID+"_RunHrs"); END SQLEnd(hSQL); SQLDisconnect(hSQL); END ELSE SQLDisconnect(hSQL); Message("Error", SQLErrMsg(), 48);END//----outer IF End// of Function

Share this post


Link to post
Share on other sites
Thanks a million, Bud. Beer on me if we ever meet.

Share this post


Link to post
Share on other sites
Chelton, One more question begs... what if the string concatenation is needed to create tags for assignment? Let's say we want to assign sCur_ID+"_MaxkW" = CalcAvg() // an expression that calculates a value and writes it to the concatenated tag. In that case how do we substitute the prefix? Thanks a lot again Sir,

Share this post


Link to post
Share on other sites
If i understand the question correctly, this should do it. TagWrite(sCur_ID+"MaxKW",CalcAvg());
1 person likes this

Share this post


Link to post
Share on other sites
Chelton thanks again! Didn't know that the TagWriteValue() could do this.. Having come this far, is there any function that builds up a tag for referencing the tag whose name is the result of string concatenation? Regards, all the best. Our countries most probably will be competing in the finals of the world cup! .

Share this post


Link to post
Share on other sites
Chelton, one more question for you, see the forums under a new topic

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0