Dividing a string to Array or several variables in ST
Hello, I have been messing around with ST for a few hours now, trying to solve a problem that is simple to solve in other languages (python, vba etc), but i cant seem to find any documentation for the ST language.
in our program we recive a string, that we want to devide into several variables, and each "variable" in the string is seperated by a semicolon.
for example:
string: This;is;the;string;i;want;to;convert
and we want to devide this string into variables:
var1: This
var2: is
var3: the
and so on..
in other languages i whould use a for-loop and Len(string) to get each letter to the variables i want. But since i cant find any source for the ST language used in freelance it´s like writing a letter to someone in a forign language without a dictionary.
so i whould really appreciate if someone whould tell me how to do what i want, or point me in a direction where i can find a "dictionary" for this language :)
Thank you in advance.
in our program we recive a string, that we want to devide into several variables, and each "variable" in the string is seperated by a semicolon.
for example:
string: This;is;the;string;i;want;to;convert
and we want to devide this string into variables:
var1: This
var2: is
var3: the
and so on..
in other languages i whould use a for-loop and Len(string) to get each letter to the variables i want. But since i cant find any source for the ST language used in freelance it´s like writing a letter to someone in a forign language without a dictionary.
so i whould really appreciate if someone whould tell me how to do what i want, or point me in a direction where i can find a "dictionary" for this language :)
Thank you in advance.
Voted best answer
The elements of ST language are explained in the engineering manual IEC 61131-3 Programming manual in section 8, ST Language.
As tr10091969 wrote you need to use the string function in the Freelance Standard function block library. One would use the S_FIND function to look for the separator character ";" and use the S_LEFT function to retrieve the first sub-string. S_DEL would be used to adjust the string in order to get the next word as the most left one. S_LEN can be used to determine when all the sub-strings in a string have been retrieved.
While all of this is straight forward, the tricky part is how to store the sub-strings into Variables. The easiest way would be to store them in an array of strings using the loop variable as an index. Problem is, arrays only exist in the ST world and cannot be used outside of an ST program. If you need to do that, you would need to use a CASE statement in a storage loop after the retrieving loop to store the sub-strings into Var1, Var2,...VarK and clear the VarK+1...VarN
As for the concerns of endless loops: Besides making sure that your loop always terminates, to prevent an endless loop from "Killing" the controller you can give the task with the loop an worse priority than the other tasks. This way, other tasks can interrupt the loop task in case of an endless loop. Your controller would still go into the 100% CPU load, but other tasks are still computing. You should also get a task alarm for the loop task (if you configure it) to point you to that situation.
As tr10091969 wrote you need to use the string function in the Freelance Standard function block library. One would use the S_FIND function to look for the separator character ";" and use the S_LEFT function to retrieve the first sub-string. S_DEL would be used to adjust the string in order to get the next word as the most left one. S_LEN can be used to determine when all the sub-strings in a string have been retrieved.
While all of this is straight forward, the tricky part is how to store the sub-strings into Variables. The easiest way would be to store them in an array of strings using the loop variable as an index. Problem is, arrays only exist in the ST world and cannot be used outside of an ST program. If you need to do that, you would need to use a CASE statement in a storage loop after the retrieving loop to store the sub-strings into Var1, Var2,...VarK and clear the VarK+1...VarN
As for the concerns of endless loops: Besides making sure that your loop always terminates, to prevent an endless loop from "Killing" the controller you can give the task with the loop an worse priority than the other tasks. This way, other tasks can interrupt the loop task in case of an endless loop. Your controller would still go into the 100% CPU load, but other tasks are still computing. You should also get a task alarm for the loop task (if you configure it) to point you to that situation.
Source: SK
Answers
structured text uses PASCAL as language scheme.
Including loops and conditions.
Most is borrowed from PASCAL, IIRC because it's a "teaching language" and easy to learn.
The syntax scheme is a little bit weird for a pure C/C++ programmer like me.
I miss va_args and good pointer arithmetic.
Also I miss a preprocessor, which PASCAL programmers don't understand ;-)
Including loops and conditions.
Most is borrowed from PASCAL, IIRC because it's a "teaching language" and easy to learn.
The syntax scheme is a little bit weird for a pure C/C++ programmer like me.
I miss va_args and good pointer arithmetic.
Also I miss a preprocessor, which PASCAL programmers don't understand ;-)
AFAIK there is no reference manual for ST language.
Try searching in Control Builder online help, search for String functions.
I cannot give you exact answer now. I would solve it in while loop until next semicolon exists in the string.
Another start point could be browsing in System / Basic lib functions in CBM libraries tree.
Try searching in Control Builder online help, search for String functions.
I cannot give you exact answer now. I would solve it in while loop until next semicolon exists in the string.
Another start point could be browsing in System / Basic lib functions in CBM libraries tree.
https://forum-controlsystems.abb.com/...
General Comments ....
There are some pretty good reasons for not using FOR/NEXT and WHILE/UNTIL Loops in a PLC - The main one being that endless execution of a loop like this is a fantastic way to kill your controller when your program overruns. So before you launch into for/next loops ensure that you know your loop will actually end properly every time.
String Handling functions tend to be pretty CPU intensive in any type of computer. In a PC with 8GB of memory and a 2.2MHz CPU who cares. But when you are dealing with an expensive PLC, with limited memory and an absolute requirement that your program MUST finish within a few milliseconds, its a big deal. Which is why processing strings in a PLC isn't really a good idea and why nobody really much wants to do it. Hence, the ST language specifications really don't have extensive string or array handling.
General Comments ....
There are some pretty good reasons for not using FOR/NEXT and WHILE/UNTIL Loops in a PLC - The main one being that endless execution of a loop like this is a fantastic way to kill your controller when your program overruns. So before you launch into for/next loops ensure that you know your loop will actually end properly every time.
String Handling functions tend to be pretty CPU intensive in any type of computer. In a PC with 8GB of memory and a 2.2MHz CPU who cares. But when you are dealing with an expensive PLC, with limited memory and an absolute requirement that your program MUST finish within a few milliseconds, its a big deal. Which is why processing strings in a PLC isn't really a good idea and why nobody really much wants to do it. Hence, the ST language specifications really don't have extensive string or array handling.
Add new comment