Byte Swap in PLC-5

Ken Roach

Lifetime Supporting Member + Moderator
Join Date
Apr 2002
Location
Seattle, WA
Posts
17,482
I'm writing a PLC-5 program that receives a barcode string over DH+ that ends up in an Integer register in the controller.

The challenge is that the data is "byte swapped", so that when I move it into a STRING data type the individual bytes in each word are backwards from human readable.

I have: ABCRDO1E
I want: BARCODE1

Does anyone have a code snippet or subroutine to share that will swap the bytes in an array of PLC-5 integers? In the SLC-500 and ControlLogix I have the convenient SWP instruction, but in the PLC-5 I have to do it by hand.
 
I havent got what you want Ken, but if you want to swap say N7:0.0-N7:0.7 with N7:0.8-N7:0.15 (i speak decimal better than octal) is it possible to write a routine using indexed addressing that would increment on scan to move the bits into another register in the order you need them. Hope this makes sense. Alan Case
 
This is funny

Code:
      AND   Swapped 127 temp0      ; KEEP LOWER 7 BIT OF ASCII CHAR.
      MUL   temp0 256 temp0        ; SHIFT RIGHT 8 OR MAKE LO BYTE HI
      DIV   Swapped 256 temp1      ; SHIFT LEFT 8 OR MAKE HI BYTE LO
      AND   temp1 255 temp1        ; KEEP ONLY THE LOW 8 BITS   
      OR    temp0 temp1 UnSwapped  ; COMBINE

The AND before the MUL is required to avoid overflowing 32767. This assumes you are using ascii characters only and therefore all characters are in the range of 0-127.

The AND after the DIV will not be required if you are only using ascii data because the integer will always be positive.
 
I cant seem to attach 2 jpegs to the same post.

This example will give you 10 integers of converted data starting at N7:20 with the source data being 10 consecutive integers starting at n7:0

byte_swap1.jpg
 
Last edited:
I ended up doing it exactly the way 93LT1 illustrated.

Peter, I was thinking about using DIV and MUL to shift bytes around, but I didn't have it clear in my head (up all night working on this!). I think those instructions might be faster than making the PLC-5 move all those bits around. It probably won't be discernible in scan time, but I'll see about doing it both ways to compare.

Thanks, guys !
 

Similar Topics

I have a C-More HMI that changes my PLC String from "Machine Status" to "aMhcni etStasu" . There is an option with other objects that have string...
Replies
15
Views
3,458
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,567
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,926
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,840
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,159
Back
Top Bottom