TechTip: Using StringLength In a Weintek HMI

This TechTip is about using StringLength in a Weintek HMI. An example application for this might be where you want to return a string to generate a batch code where you may gather data from various sources – different devices for example.

Previously on a TechTip, we covered using StringCat, where StringLength is useful is where your strings vary in length. By measuring the length of the string, we can use it to create an offset when we concatenate using StringCat.

Let’s break down what we’re doing here:

Declare variables for the source LW data and declare the OutputString:

char Text1[10] // Text1 from HMI LW500-
char Text2[10] // Text2 from HMI LW510-
char Text3[10] // Text3 from HMI LW520-
char Text4[10] // Text4 from HMI LW530-

char OutputString[40] // Result output string

Declare variables for the offsets :

short length1 // first length variable
short length2 // second length variable

Clear the output buffer and output addresses:

FILL(OutputString[0], 0, 40) // clear outputstring
SetData(OutputString[0], “Local HMI”, LW, 600,40) // Clear LW600

Get the Data from the HMI LW addresses:

GetData(Text1[0], “Local HMI”, LW, 500, 10) // get Text1
GetData(Text2[0], “Local HMI”, LW, 510, 10) // get Text2
GetData(Text3[0], “Local HMI”, LW, 520, 10) // get Text3
GetData(Text4[0], “Local HMI”, LW, 530, 10) // get Text4

Using StringCat, put our first text into OutputString starting at [0] and measure the length of Text1 and put that into length1:

StringCat(Text1[0], OutputString[0]) // concatenate Text into OutputString
length1 = StringLength(Text1[0]) // measure Text1 and put result into “length1”

Concatenate Text2 with OutputString using “length1” as the offset and calculate the next offset using StringLength

StringCat(Text2[0], OutputString[length1]) // offset prodcode into outputstring by length of Text1 (length1)
length2 = StringLength(Text2[0]) + length1 // measure Text2 and add to length1 to give the next offset

Concatenate Text3 with OutputString using “length2” as the offset and calculate the next offset using StringLength

StringCat(Text3[0], OutputString[length2]) // use length2 as the offset
length1 = StringLength(Text3[0]) + length2// measure Text3 and add to length2 to give the next offset

Concatenate Text4 with OutputString using “length1” as the offset and setting that data in LW600-

StringCat(Text4[0], OutputString[length1]) // use length1 as the offset

SetData(OutputString[0], “Local HMI”, LW, 600, 40) // set LW600 with the value of OutputString

There’s a demo project here for you to try out.