If an arithmetic expression uses two operands of different types, the long operand is automatically converted to a double value. The result of the operation is a double value. If the calculation is to provide an integer value (long), the double operand or result must be converted to a long value by the Round function.
There exist various format conversions from/to Simatic specific types. these format conversions are only useful in context of “Block” operations.
Dtostr
Declaration | string DToStr(double x) |
Function | Converts the double value x to a string. The number is displayed either exponential or decimal, depending on the size. |
Cross | StrToL, LToStr, StrToD |
Example | double d d := 125.56 / 37 debug(‘Result : ‘ + dtostr(d)) |
Ltostr
Declaration | string LToStr(long x, long radix) |
Function | Converts the long value x to a string. The radix parameter specifies the basis on which the conversion should take place – possible values are in the range of 2 to 36. If radix is 10 and x is less than zero, LToStr precedes the result with a minus sign. For conversion to Decimal use radix 10 |
Cross | DToStr, StrToL, StrToD |
Example | long l string s l := 1247 s := ltostr(l, 10) debug(‘Decimal : ‘ + s) s := ltostr(l, 16) debug(‘Hexadecimal : ‘ + s) s := ltostr(l, 2) debug(‘Binary : ‘ + s) |
StrLower
Declaration | string StrLower(string source) |
Function | Converts all uppercase letters of the source string to lowercase and returns the result. Source is not changed. The letters A – Z are converted. |
Cross | StrUpper |
Example | string s s := ‘ABcDEfGH’ debug(strlower(s)) |
Strtod
Declaration | double StrToD(string source) |
Function | Converts the source-designated string to a floating-point number and returns the result as a double. |
Cross | Str, StrToL |
Example | double d d := strtod(‘-1.7567E+02’) |
Strtol
Declaration | long StrToL(string source) |
Function | Converts the source-designated string to a long value. |
Cross | DToStr, LToStr, StrToD |
Example | long l l := strtol(‘-1’) |
Strtolex
Declaration | long StrToLEx(string source, long radix) |
Function | Converts a number contained in the source-designated string to a long value (signed). The radix parameter specifies the assumed numerical basis for the number stored in the string. The values 0, as well as 2 to 36, are allowed. If specified as radix 0, the first characters of the string determine the number base to use: 1..9 Decimal Representation 0x Hexadecimal representation 0 Octal representation The conversion ends at the first non-convertible character in the string. Ranges: Dec: -2,147,483,648 to 2,147,483,647 Hex: -80000000 to 7FFFFFFF (values > 7FFFFFFF are converted to 7FFFFFFF!) If a hexadecimal value > 7FFFFFFF is to be converted to a long value without a sign, this is possible as follows: long n1, n2 n1:=substr(“$Global:VarString”,0,4) n2:=substr(“$Global:VarString”,4,4) “$Global:VarLong” := (StrToLEx(n1,16) shl 16) + StrToLEx(n2,16) |
Cross | StrToL, LToStr |
Example | long l, k l := StrToLEx(‘0x1D7F’, 0) k := StrToLEx(‘00011101011111111’, 2) |
StrUpper
Declaration | string StrUpper(string source) |
Function | Converts all lowercase letters of the source string to uppercase and returns the result. Source is not changed. The letters A – Z are converted |
Cross | StrLower |
Example | string s s := ‘ABcDEfGH’ debug(strupper(s)) |
Format conversions for PLC datatypes
these format conversions are only useful in context of “Block” operations.
DToF
Declaration | long DToF (double x) |
Function | Converts the double value x to the floating-point format of the Simatic-S7. The result of this function cannot be further processed in the script, but is only suitable to be entered with SetBlockNum in a data block, which is to be written into an AG of type Simatic S7. |
Cross | SetBlockNum, FToD, DToKG |
Example | block b b := NewBlock(6) SetBlockNum(b, 0, 2, false, Endian(“Internal:InputGanz”, false)) SetBlockNum(b, 2, 4, false, DToF(“Intern:InputFliess”))”S PS:ShouldBlock” := b |
DToKG
Declaration | Long DToKG (double x) |
Function | Converts the double value x to a floating point number with the KG format of the Simatic-S5. |
Cross | KGToD |
Example | double d long l d := sin(pi) l := dtokg(d) “SPS1:WEG” := l |
Endian
Declaration | long Endian(long x, bool all) |
Function | Swaps both high and low bytes of the x argument. If all = true, the high and low words of the x argument are also exchanged. This function facilitates the processing of data that originates from other machines (e.g. Simatic-S7) that have different Endianess. |
Example | long l l := endian(“SPS1:DB100 DD1”, true) |
FToD
Declaration | double FToD(long x) |
Function | Converts a value from the floating-point format of the Simatic-S7 to a double value. Only values extracted from a data block read from a Simatic S7 AG using GetBlockNum are useful for entering this function. |
Cross | etBlockNum, DToF, KGToD |
Example | Long TempData //We get the data from an Simatic Block TempData := GetblockNum(Data, BlockOffset, 4, false) //Since GetBlockNum has already corrected the Endianess of the Integer, we //have to revert what GetBlockNum did TempData := Endian(Endian(TempData, true), false) //Now we can call the conversion return FToD(TempData) |
Additional | The FToD funcion will propagate Nan (Not a number) values into your scripts, where these can cause issues. We reccomend you use the Script funcion “FtoDEx” which allows you to catch and write an replacement value in case of an NaN. You can download the script below. |
KGToD
Declaration | double KGToD(long x) |
Function | Converts a floating point number x with the Simatic-S5 KG format to a double value. |
Cross | DToKG |
Example | long l double d l := “SPS1:DB100 DD1”) d := kgtod(l) |