[RsLogix] Reverse characters in a string ?

passwordg

Member
Join Date
Aug 2011
Location
South Carolina
Posts
224
Hi guys,

Using a CompactLogix PLC.

Really struggling with this. Is there an easy way to flip the letters in a STRING data type ?
Example: a tag holding the value "ABCDE" is copied to a string tag called "EDCBA"

Any ideas or suggestions would be really helpful please.
Thank you.
 
Here's the brute force option. There's probably a slightly neater way with an FAL instruction

Screen Shot 2022-10-18 at 9.02.03 am.png
 
My next question, of course, is "why?"
I wasn't going to ask why as I assumed he has a good reason but my first thought was, PLCs are not made for this and whatever required him to do that is poor design. I know some devices (barcose scanners come to mind) require a song and dance in data conversion and that really ****es me off, but his is just way out there.
 
Isn't SINT .DATA[] zero-based, so the character (SINT) at .DATA[.LEN-(1+.POS)] goes to .DATA[.POS], where .POS runs from 0 to .LEN-1?


Also, if the goal is to swapping the characters in-place, then .POS only needs to run from 0 to ((.LEN AND -2)/2), and no extra memory is required.
 
You're right about the zero based addressing indexing, my code would need to be updated with a few +1's and -1's.

But I'm not sure about your second sentence. The PLC won't "swap" them, it'll just copy one to the other. So you have to copy to a new string, or else youll start with ABCDEF, and then halfway through have FEDDEF, and then if you continue, you'll still end up with FEDDEF.
 
PLCs are not made for this

Why? Ladder logic for this, I can understand, but when you have a processor, arrays and a text language, there's no reason why they shouldn't be able to do this.

The issue is how much of an impact does this have on the important stuff.. but that's down to the implementer.
 
Last edited:
I new that trick but never used it because it required too much loading and storing from memory. When I was programing 80186s in assembly language I would load both values and use an xchg instruction to swap the values then store them. This took only 4 memory accesses as opposed to the 'tricks' 9 memory accesses.
 
The 80186 instruction set is not exactly relevant in the domain of PLC programming. That said, some PLCs might have a swap-bytes command that could work here, although it would still need temporary space.

My main point was that, if needed, we can reverse the string in-place with half the loop steps or less, and how each swap occurs is not important; the no-temp-space trick is just that, a trick (or an answer to a question at an interview)

AND s.LEN -2 i DIV i 2 i

LBL loop GRT i 0 XOR s.DATA[i-1] s.DATA[s.LEN-i] s.DATA[i-1] XOR s.DATA[i-1] s.DATA[s.LEN-i] s.DATA[s.LEN-i] XOR s.DATA[i-1] s.DATA[s.LEN-i] s.DATA[i-1] SUB i 1 i JMP loop

Still as many instructions or more than any other suggestion so far.
 
Why? Ladder logic for this, I can understand, but when you have a processor, arrays and a text language, there's no reason why they shouldn't be able to do this.

The issue is how much of an impact does this have on the important stuff.. but that's down to the implementer.
;) I wrote a post and I lost with my mouse gesture ;)


Another one bites the dust.
 
When I need something like this I usally turn to good old Quick Basic and convert one of those to structured text. Here is the QB version if you want to try and convert it:
' Reverses the order of all characters in a$.
'
' EXAMPLE OF USE: b$ = Reverse$(a$)
' PARAMETERS: a$ String to be processed
' VARIABLES: n% Length of the string
' r$ Working string space
' i% Index into the string
'
FUNCTION Reverse$ (a$) STATIC
n% = LEN(a$)
r$ = a$
FOR i% = 1 TO n%
MID$(r$, i%, 1) = MID$(a$, n% - i% + 1, 1)
NEXT i%
Reverse$ = r$
r$ = ""
END FUNCTION
 

Similar Topics

Hi I did not find the instruction. I am a very part time user of Rockwell soft. I am looking for the same "NOT" function that you can use in...
Replies
14
Views
5,350
Hello all, I have a question in regards to RSlogix 5000. I am having issues with the program force closing when I try to make online edits. We...
Replies
0
Views
95
I am having trouble with getting no control of my analog output signal. I am using the SCL function block to control my analog output. The logic...
Replies
11
Views
211
Hello, Haven't been on in a while. I need to generate a bit level pdf of the I/O for RSLogix 500. I can generate a report but it just shows the...
Replies
1
Views
117
Greetings ... someone sent me a request for some student handsouts that I developed ... turns out that I had this hosted on my business website...
Replies
0
Views
111
Back
Top Bottom