[Studio5000] Insert a char before every occurence of space chars from a string

Nico38

Member
Join Date
Sep 2021
Location
France
Posts
8
Hello guys,

I am working with a CompactLogix, which communicates over Ethernet/IP with a pin marker.
Basically, The PLC sends the texts to be marked to the marking UC, and then it triggers the marking.

The marking UC doesn't accept space characters into the texts string to be marked. To get rid of this situation, every space characters must be escaped with '\' character.

Example :
Text to mark : 'This is the content to print'
String to send to marking UC : 'This\ is\ the\ content\ to\ print'

What would be the cleanest way to achieve such a goal ?
Could you give me some tips, to help me start with that task, because I am not an expert with Allen-Bradley PLC programming.

Thanks in advance for your help
 
Hey Nico38,

Welcome to the forum!

Do you always send the same content?
Does the size of string to print vary? Do the # of words vary as well?

I'd likely use a "FIND" and "INSERT" instruction set.
Look for the first space character using the "FIND" - you should return the position of the space in the string.
Check for the next by starting at the located character +1.

Use the insert instruction by taking the RESULT from the FIND and dropping in your "" character.

Likely need a bit of finessing, but would get the job done.
Good luck!
 
...use a "FIND" and "INSERT" instruction set. ...

MOV 1 inext OTE index.0

LBL loop XIC Do_escape LEQ inext thestring.LEN FIND String_tag Space inext inext GRT inext 0 INSERT String_tag Backslash inext String_tag ADD inext 2 inext JMP loop

OTU Do_escape



  • String_tag is the STRING tag to be modified
  • Space is a STRING tag of .LENgth 1 containing a space character (ASCII 32; $20)
  • Backslash is a STRING of .LENgth 1 containing a backslash character (ASCII 92; $5C)
  • Do_escape is a one-shot BOOLEAN tag that is OTLed to 1 whenever a new string arrives in String_tag, and has not yet had its spaces escaped
Caveats


  • There are multiple ways, other than OTL/OTU and Do_escape, to ensure the spaces are not repeatedly escaped, once per scan.
  • The should probably be some logic to protect against the string becoming too long (82 chars, typically?).
 
Something that we have done, is to create a string array (in this case named StringArray datatype STRING[7]) to build our final data. You can fill the array with all of your variables, and any constants needed. For example:

Code:
StringArray[0] = "This"
StringArray[1] = "is"
StringArray[2] = "the"
StringArray[3] = "content"
StringArray[4] = "to"
StringArray[5] = "print"
StringArray[6] = "/ "

Then we use a series of CONCAT instructions to fill a second string tag (In this case named FinalString datatype STRING) to build the part number, or data to send off wherever it needs to go.
Code:
CONCAT FinalString StringArray[0] FinalString CONCAT FinalString StringArray[6] FinalString CONCAT FinalString StringArray[1] FinalString CONCAT FinalString StringArray[6] FinalString CONCAT FinalString StringArray[2] FinalString CONCAT FinalString StringArray[6] FinalString CONCAT FinalString StringArray[3] FinalString CONCAT FinalString StringArray[6] FinalString CONCAT FinalString StringArray[4] FinalString CONCAT FinalString StringArray[6] FinalString CONCAT FinalString StringArray[5] FinalString

You can see in the image attached that FinalString contains "This/ is/ the/ content/ to/ print" (well, at least "This/ is/ the/ conte", but hopefully you get the idea). You are able to change any of the StringArray variables as you see fit before turning on the XIC BuildData to concatenate the final string. It isn't the prettiest to look at, especially for really long final strings, but I think it is easier to understand. Hope this helps some.

CONCAT Rung.jpg
 
Thank you guys for you fast and valuables replies !

Because the source string may have different numbers of characters, I've chosen the solution using a Loop with FIND and INSERT, and it works just as expected.

Screenshot.png Screenshot2.png
 
I have seen this done using FSC instructions as well. But i think a couple of the other methods mentioned previously are what i would end up using.
 

Similar Topics

I have an array of 55 REAL values. Is there a way to multiply based on the array location ? I have 55 transfer belts that are equally spaced...
Replies
3
Views
154
Hi Hope you all are doing well. Iam working on a project with some AOI. I also hate no online edits... lol. My problem occurs when I use a UDT...
Replies
2
Views
161
I am not sure if this is possible but if there is a way, you guys would be the ones to know. I am currently working on a project where we are...
Replies
7
Views
222
Hi all. I'm having issues adding an ethernet module to my project in Studio500 v34. The device is a Fredericks Televac EthernetIP MX gateway which...
Replies
8
Views
365
The day of week program started changing day of week 2 hours early. It changes at 10 P.M. instead of 12A.M. Just started this year.
Replies
22
Views
2,676
Back
Top Bottom