[RsLogix] Reverse characters in a string ?

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.
 
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.
 
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...
14
Replies
8,065
Views
Bit of a weird one here maybe. One thing that may be relevant(?) Doing this on a non-activated copy of RSLogix while waiting for the activation...
8
Replies
297
Views
Really pulling my hair out here, yet again. I have RSLogix 500 on a Windows 11 laptop. I am trying to get it running on a different Windows 11...
2
Replies
254
Views
Hi, I have recently started learning PLC ladder logic using the free Rockwell RSLogix Micro Starter software to develop practice projects and...
6
Replies
305
Views
Can someone give me an idea as to how much the software and lifetime license currently costs? (the Starter Edition is really all I need) Thanks.
2
Replies
576
Views
Top