You can read this post on LinkedIn as well.
Introduction
The support on string datatype in EBPro Macro falls short. I complained about it in my last article GetData()/SetData() and Error C45 in EasyBuilder Pro (EBPro). When I contacted Maple Systems' technical support, they couldn't answer my question and played dead in hope that I would walk away. And I did, disappointedly...
Luckily, the solution dawned on me last night. Now let's learn!
Section 1: Limitations of String Datatype in EBPro Macro
If you create an ASCII input object in EBPro, you can choose from Unicode or UTF-8 encoding. For Unicode, each word reads 2 bytes. In contrast, each word reads 1 byte for UTF-8.
Section 2: The Problem with Extra Spacing in ASCII Objects
If you limit your usage to very basic functions on HMI, you have nothing to worry about. However, if you write a Macro to play with these ASCII objects or read/write them to/from your PLC controller. You may run into a problem like this.
Section 3: Solution: Removing Extra Spacing in Macro
So how do we remove the extra spacing?
Here is how I do in Macro. (Of course, you can solve this in PLC as well. Just Concatenate the non-empty elements in the PLC string into the other PLC string. Then ta-da!)
macro_command main() // There is no string datatype in Macro. We use a character array instead char hmi_char_array[20] = "" int len_hmi_char_array = 0 // Get input from HMI. I type in HELLO WORLD here. // The starting point is 10 and there are 20 elements GetData(hmi_char_array[0], "Local HMI", LW, 10, 20) // Copy the length of hmi_char_array len_hmi_char_array = StringLength(hmi_char_array[0]) SetData(len_hmi_char_array, "CompactLogix", "PLC_String.LEN", 1) // Move chars in the hmi_char_array to a string in PLC SetData(hmi_char_array[0], "CompactLogix", "PLC_String.DATA[0]", 1) SetData(hmi_char_array[2], "CompactLogix", "PLC_String.DATA[1]", 1) SetData(hmi_char_array[4], "CompactLogix", "PLC_String.DATA[2]", 1) . . . SetData(hmi_char_array[16], "CompactLogix", "PLC_String.DATA[8]", 1) SetData(hmi_char_array[18], "CompactLogix", "PLC_String.DATA[9]", 1) SetData(hmi_char_array[20], "CompactLogix", "PLC_String.DATA[10]", 1) end macro_command()
You would probably wonder why I don't use an index and a for loop to finish the tedious SetData() job. Unfortunately, it's because I can't. It simply doesn't support this format: hmi_char_array[index] in Macro.
Now we remove a snag in our way. Time to have a great day, my engineering friends!
Alternative Solution: Removing Extra Spacing in PLC
You can also read/write a PLC tag rather than an HMI tag when you create an ASCII input object. Here is how you do it in Macro. (Remember to activate your Macro as you see fit. Either by a function key, a notification bit, a window, or a time period.)
You may notice that in the first example I created a character array in the char data type but one in the short data type in the second example. That's an intriguing ambiguity. I assume it works as well if you use the char datatype in the second example. After all, the "if-else" logic is simply checking if there's something in the element.
macro_command main( // There is no string datatype in Macro. We use a character array instead short hmi_char_array[20] = "" int count = 0, element = 0 // Get the string from PLC. GetData(hmi_char_array[0], "CompactLogix", "PLC_String.DATA[0]", 1) for count = 0 t 19 element = hmi_char_array[count] if element then // check if it is a character count = count + 1 else break end if next count // Use count as the length of hmi_char_array and copy to PLC SetData(count, "CompactLogix", "PLC_String.LEN", 1) end macro_command())
Comments
Post a Comment