reversing bit order

ganutenator

Lifetime Supporting Member
Join Date
May 2002
Location
kansas
Posts
1,440
I am reading data via modbus from a third party device. All the bits are reversed, and I am wondering if there is a quick and dirty mathematical or logical way to reverse them.

For example. The bit pattern in my PLC for a 16 bit word is 1 through 16 from left to right with 1 being the most significant bit. The device that I am communicating with is 1 through 16 right to left with 16 being the most significant bit.

my PLC:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

3rd party device:
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
 
Could you run it through a LIFO, last in first out. That way the first bit that comes from your third party device will be the last bit the PLC reads and the last bit from the TPD with be the first bit the PLC reads. I've never actually done this, just a thought.
 
A case of dyslexia

What is this third party device? Are you telling us that if the device sends you the value of 5, it comes in as 1010000000000000???

It's not uncommon to find bytes or words reversed, but I have never encountered a mirror image of the entire word. Before trying to work out a scheme to reverse the order of the bits, I think you should get a couple of specific examples and post them here.
 
The problem is Modicon's format inverts the order of bits when moving the contents of a holding register (using a BLKM instruction) to an array of bits or vice-versa.

This is a common problem for newbies who haven't read the instruction manually very carefully (I got burned by it when I first used it). Unfortunately, there isn't a simple fix.

If you haven't gotten too far into your ladder, your best bet is to manually edit your code. The alternative is a brute force method where you write to a new set of coils with a N.O. contact from the old 16th bit coil turning on the new 1st bit coil, the old 15th bit controls the new 2nd bit, etc.

Steve: No, the bit pattern for numbers is not reversed. If you send a '5' you will get a 0000000000000101. And you can block move one register to another just fine with no inversion problems.

But if you BLKM that register to an array of 16 coils expecting the 1st and third coils to be on, they won't be. The 14th and 16th coils will be on instead.
 
Last edited:
Yes, it is probably safe to say that if I read a dec 5 from the third pary device (A Murphy Engine Management Controller) via modbus that it will appear as a decimal 5 in the integer register, and I will confirm this.

The Murphy controller maps all of their discrete bits to integer words. My modicon PLC reads these integers via modbus. The mistake happened because I was originally only given a piece of paper from Murphy that defined which bit was which tag.

The problem is that I have 105 of these bits and would have to use a SENSE instruction for each bit. I of course, took the lazy way out and did a block move to map these bits into coils. Regardless of whether I did this block move or whether I used a SENSE instruction, bit 1 on the piece of paper from Murphy would still be bit 16 in the modicon, regardless of how I referenced it in the Modicon.

I fixed the problem by moving the address comments around in the Modicon and by changing the tag useage around in the Quick Panel OIT.

I have seen some neat tricks done by computer programmers with math to extract bytes of a word etc. and was just wondering, for future reference, if anyone knew a mathematical or logical way to solve this problem.

- Note: You did get me thinking though.... For future reference, although I don't immediately see a need for doing this, What would happen if you moved an integer into coils and then into a different integer. Would the second integer be a different value than the first integer, or does the block move instruction reverse the copy order when moving from coils to an integer.
 
How about the brute force approach, but in a subroutine.

Example:

Ladder File 2

MOV N7:0 to N7:10 (N7:10 contains word to be sorted)
JSR 3
MOV N7:11 to N7:1 (N7:11 contains sorted word)

MOV N7:2 to N7:10 (N7:10 contains word to be sorted)
JSR 3
MOV N7:11 to N7:3 (N7:11 contains sorted word)

Ladder File 3
N7:10 contains word to be sorted
N7:11 will contain sorted word

Sort N7:10 bit by bit to N7:11
i.e. N7:10/0 wil energize N7:11/15
N7:10/1 wil energize N7:11/14
etc.

This way you could sort as may words as you want and reuse the sorting logic.
 
Here is a Modicon 984 ladder logic one scan, bit swapping logic network. Normally this would be done in a subroutine so that it only occurs when needed. The example is swapping from register 400600 to 40611, the logic is contained in subroutine #1. The BROT, bit rotate, shifts the bit pattern of reg 400610. The MBIT, modify bit, changes the value of the bit from 1 to 0 or 0 to 1 in reg 400610 and puts the result into reg 400611



--]P[----|-------|---|-------|---|-------|-
000100 |400600 | |#00001 | |#00001 |
| | | |---|JSR |-
|400610 | |#00000 | |#00001 |
|BLKM | |SUB |- |-------|
|#00001 | |400609 |
|-------| |-------|

{Subroutine Segement}

-|LAB---|--
|#00001| |--]/[------------------|------|-
|------| | 000102 |#0001 |
------|-------|---|-------|-|-|-------|-- |JSR |
|400610 | |400609 | |#00016 | |#0001 |
| | | | | | |------|
|400610 |---|400611 |---|400609 |---( )--
| | | | | | 000102
|BROT | |-|MBIT |---|SUB |--
|#00001 | | |#00001 | |403000 |
|-------| | |-------| |-------|
----------------|

 
Thanks Peter, however I am probably guilty of plagarism for not giving proper credit.
I got this logic from the Modicon Modfax support line several years ago.
 

Similar Topics

Does anybody know how to take a setpoint number and reverse it based on 100. In other words If I set my DB2.DBD10 to 75 (Floating Point)then have...
Replies
3
Views
2,553
Hello! I'm searching the manual for the Cutler-Hammer non-reversing starter model W200M1CFC. I've searched Eaton's website, and only found a...
Replies
0
Views
1,167
Morning, We were having some trouble with a air handler over the past week and we found out that the source of our problems was the motor running...
Replies
11
Views
3,510
Anyone have a slick reversing relay they have used and recommend? I am trying to simplify wiring but not looking to take up more space than the...
Replies
9
Views
3,422
I will try to keep this simple. I have a machine that is running an emerson vfd running in closed loop vector mode. The direction say to to wire...
Replies
10
Views
2,596
Back
Top Bottom