RSLogix 5000 - String Byte Swap

Tharon

Member
Join Date
Jan 2007
Location
Other
Posts
1,430
I ran into a situation where some devices I use display String bytes in a swapped order as they are stored in my PLC (CompactLogix). I thought it would be an easy fix, since I've heard about Swap Byte instruction, but never used it. Turns out the Swap Byte instruction wouldn't work nicely (if at all) with Strings.

So I ended up making a short AOI to that I could input any length string and it would output a result with each byte pair swapped. Wasn't hard, and good practice in indexing and AOI usage. But I was just curious if there was already an instruction that existed that would have done that for me?
 
I ran into a situation where some devices I use display String bytes in a swapped order as they are stored in my PLC (CompactLogix). I thought it would be an easy fix, since I've heard about Swap Byte instruction, but never used it. Turns out the Swap Byte instruction wouldn't work nicely (if at all) with Strings.

So I ended up making a short AOI to that I could input any length string and it would output a result with each byte pair swapped. Wasn't hard, and good practice in indexing and AOI usage. But I was just curious if there was already an instruction that existed that would have done that for me?

Are you saying that your STRING tag contains something like...

"eHll ooWlr d"
for
"Hello World "

Swap Byte instruction is used when an INT contains 2 ASCII characters that are stored in reverse order, or for a DINT that contains 4 ASCII characters, and there are options for the re-ordering.

So it won't work with the SINT array where a STRING tag is stored.

You could COP (or CPS) that array to an INT array of half the size, do the SWPB on all the elements of the INT array, then COP it back to the STRING.Data array. Just be mindful of your COP lengths, should be number of elements of the destination data-type.
 
Are you saying that your STRING tag contains something like...

"eHll ooWlr d"
for
"Hello World "

Yes, that's what it does. Every two pair of characters are swapped.


You could COP (or CPS) that array to an INT array of half the size, do the SWPB on all the elements of the INT array, then COP it back to the STRING.Data array. Just be mindful of your COP lengths, should be number of elements of the destination data-type.

In my AOI, I did
MOV ORIGINAL.DATA[INDEX] SWAPPED.DATA[INDEX+1]
MOV ORIGINAL.DATA[INDEX+1] SWAPPED.DATA[INDEX]

Then I incremented the INDEX by 2, until I reached the end of the array.

Only thing I had to do was add 1 to the .LEN if it was an odd length, because of the swapping of the NULL character at the end of an odd number of characters. If I didn't, then odd length string would have the last character truncated.

(Actually moved it to a Temporary string in the AOI and didn't move it out of the AOI until it was completely swapped, since it takes a few scans to actually complete the full swap.)

Good to know that moving an array of SINTs into an array of INTs combines two elements into a single INT. I am not sure if I would have ever tried that.
 
Good to know that moving an array of SINTs into an array of INTs combines two elements into a single INT. I am not sure if I would have ever tried that.

Yes - COP, CPS, and FLL instructions are just fast "byte copiers", and take absolutely no regard for data-type.

I've seen the technique used in various situations - even COPying REALs (Floats) into an existing integer array for MSG to another PLC, the other PLC just COPs the integers back to a REAL array, thus restoring the float values...

As stated the Length parameter is the number of elements of the destination data-type. From that the processor computes the number of bytes to copy.
 

Similar Topics

Hey all, I am trying to figure out how to import some alarm messages without having to type them all in by hand. They are in the format of an...
Replies
4
Views
1,127
Hi everyone, new user so please forgive any breaches of etiquette. But in RSLogix how can you convert a string in the PLC to all lowercase and...
Replies
4
Views
1,505
Hi, I am reading on strings and having a hard time finding an instruction on making a string to display on the HMI. What I want to do is move a...
Replies
5
Views
7,409
I'm working with a CompactLogix system and RSLogix 5000. I used a condition on a rung to copy a string from itself to another string when the...
Replies
8
Views
5,878
Hey Guys, I've got an array of strings in an L75 processor that take the shape of something like this: MyString[01] = '012345.678' I'm needing...
Replies
4
Views
9,976
Back
Top Bottom