Byte Swap a String in Studio5000

Does the PLC have a roll left or roll right instruction? If you roll and word 8 bits in either direction it does the same as a swap.
/gotta burn this into ROM

That would require a 16-bit INT shift, and Logix uses 32-bit DINTs for bit shifts.

With BSL/BSR, the roll could possibly be done by specifying the .UL bit as the source bit. That said, BSL/BSR only shift one bit at a time, so there would have to be eight executions of the instruction for each byte pair. COP instructions could be used to move the string .DATA[...] SINTs into and out of INTs.

So in the end, @dmroeder's or my approach would be quicker. Note that my use of the string MID instruction is essentially a shift of eight bits, but needs another LENGTH/2 passes to complete the roll.

I think it could also be done in four instructions:

  • CPT halflen ((string.len+1) AND -2)/2;
  • COP/CPS string.DATA[0] int_array[0] halflen
  • FAL ctl halflen 0 ALL int_array[ctl.POS] (((int_array[ctl.POS] AND -256)/256) AND 255) + (((int_array[ctl.POS] AND 255)*256) AND -256)
  • COP/CPS int_array[0] string.DATA[0] string.len
There is a risk of overflow in that CPT if the data are not all 7-bit ASCII, that would require some extra steps in the expression.
 

Similar Topics

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...
Replies
3
Views
3,853
I am writing software for a system(*) that has the following data flow 4-20mA Sensor => Siemens AI 8xU/I/RTD/TC ST card Siemens AI 8xU/I/RTD/TC...
Replies
16
Views
6,624
Good Morning, I'm using RS Logix 5000 to program an Allen-Bradley 1769-QBFC1B Version 20.12 and want to use the Byte Swap Instruction (SWPB)...
Replies
7
Views
4,964
Hi guys I'm having some weird data conversion issue over modbus happening. And I found a post here before that is similar to what I'm seeing but...
Replies
6
Views
7,191
Hello everybody, I have a communication to a Linux-PC via sockets. So I created a quite large UDT with all the content. Unfortunately we have to...
Replies
1
Views
2,153
Back
Top Bottom