Convert Decimal Checklist to Binary

JJH

Member
Join Date
Oct 2015
Location
South Carolina
Posts
57
Hey all,

Here's a tricky decimal to binary conversion project I could use some help with:

In my RSLogix 5000 application, I have a "checklist", that a user needs to check off all the necessary boxes on an HMI application. Depending on the app, they might only need to use four checks, or as many as sixteen. This has previously been done by Boolean tags, but it requires a fair amount of maintenance, and especially so when not all 16 checks are required for a certain application.

I'm trying to simply that process by creating a DINT value for the checklist and each "check" represents a Boolean portion of that value. So, the DINT called CheckVal will be 15 when CheckVal.0, .1, .2 and .3 are selected.

What I need to do though is to compare the CheckVal number against the number of checks expected. So, if I say there are supposed to be four checks, as in the above example, when CheckVal is 15, then the user has checked all the boxes. If the number of needed checks is say 6, however, then the checks aren't complete until CheckVal is equal to 63. In this way, a programmer just has to enter in how many checks are needed for the application and the Complete bit will be set once that's done. Also, when clearing bits, a '0' can be moved into CheckVal.

So! How can I create some basic logic to set a binary number from a decimal entry to compare against the CheckVal number? If I enter in a four for the number of checks, it should convert to 15, which I can then compare against the CheckVal number to show it is completed.

Sort of a confusing way to explain it, but I'm sure it's an easy solution. Please respond with your thoughts. I have started a simple logic application I can send to anyone interested for an easier visual review.

Thanks~
 
Two come to mind:

1. Use a CPT instruction. The value you're looking for is (2^x)-1. if x=4 (The number of check, then (2^4)-1 = 15.

2. Attached is a bit count AOI that count the number of 1s in a DINT. The addend is an optional number added to the end. this let's you string more than one DINT to count.

EDIT: Huh. The image came in as an attachment, but it still reflect both use cases. ;)

2017-11-17_9-49-16.png
 

Attachments

  • BitCount.zip
    1.3 KB · Views: 8
In standard ST:

CompVal:= 0;
FOR i:=1 to NumChecks DO
CompVal.0:=1;
CompVal:= SHL(CompVal, 1);
END_FOR

In Rockwell Ladder:

0 MOV CompVal
BSL.Array = CompVal (must be an array of DINT but you just use CompVal[0])
BSL.Source Bit = 1
BSL.Length = NumChecks
 
Hi all,

Thanks for both of your replies. Jordan's was the best solution for me, I just needed a quick way to determine how to compare the number of checks against the expected value.

I appreciate your timely response and input!
 
Sounds like you have a solution, but another method would be to use some bitwise operations.

Code:
CheckValue    = 0010 1101
CheckRequired = 1110 1111

AND Operation
0010 1101 (CheckValue)
1110 1111 (CheckRequired)
------------
[B]0010 1101[/B]

XOR the AND Result against the CheckRequired
0010 1101 (AND Result)
1110 1111 (CheckRequired)
------------
[B]1100 0010[/B]

This result is the checks not yet completed, resulting in non-zero value. If all the checks had been made, the result would be 0. This method is handy if you wanted to identify and notify the operator of what checks have not yet been made.
 
If I understand correctly, Paully's is also the only possible method if the checks are not always sequential...for example:

Application 1 requires 5 checks: 0, 1, 2, 3, 4
Application 2 requires 7 checks: 0, 1, 2, 3, 4, 5, 6

If you only ever need sequentially-numbered checks, and they always start from zero, then Jordan's method works fine.

But...

Application 1 requires 5 checks: 0, 1, 2, 3, 4
Application 2 requires 7 checks: 0, 2, 4, 5, 7, 8, 9

If you don't necessarily need every check in a numerical sequence at any point, then the only usable method is Paully's. In this case, knowing the number of checks no longer means you know the resulting decimal number - you have to know which bits are required, not just how many.

Anyway I guess your application is the former, but perhaps something worth thinking about in case one day your boss goes "oh, by the way, we no longer need check 2 in application ABC. Still need checks 0, 1, 3, and 4 though. And application XYZ still needs all five. Can you just duck in and fix that before lunch?"
 
If I understand correctly, Paully's is also the only possible method if the checks are not always sequential...for example:

Application 1 requires 5 checks: 0, 1, 2, 3, 4
Application 2 requires 7 checks: 0, 1, 2, 3, 4, 5, 6

If you only ever need sequentially-numbered checks, and they always start from zero, then Jordan's method works fine.

But...

Application 1 requires 5 checks: 0, 1, 2, 3, 4
Application 2 requires 7 checks: 0, 2, 4, 5, 7, 8, 9

If you don't necessarily need every check in a numerical sequence at any point, then the only usable method is Paully's. In this case, knowing the number of checks no longer means you know the resulting decimal number - you have to know which bits are required, not just how many.

Anyway I guess your application is the former, but perhaps something worth thinking about in case one day your boss goes "oh, by the way, we no longer need check 2 in application ABC. Still need checks 0, 1, 3, and 4 though. And application XYZ still needs all five. Can you just duck in and fix that before lunch?"

From the OP, sequential was inferred, giving the CPT function I provided. The second one I provided was a bit counter, which would work irrespective of the actual bits being used. ;)
 
From the OP, sequential was inferred, giving the CPT function I provided. The second one I provided was a bit counter, which would work irrespective of the actual bits being used. ;)

Absolutely, yes. Your solutions are definitely valid for the OP's question! I was just raising it as a warning because if there's ever the possibility that the sequential order may change at some point, you can save yourself a significant headache by planning for it up front :site:
 

Similar Topics

Hi all! looking for help converting an 8 bit word SINT tag into decimal. I have an IFM SBN246 flow sensor wired in my controller (L310ER) and it...
Replies
4
Views
2,129
Hey All, i have been fortunate enough to play around with an Applied Motion Products Servo Drive SV200 with 100 watt/ 24vdc congif. I managed...
Replies
2
Views
1,859
Anyone have a good way to convert a decimal number to fractions. Need to convert decimal inches to fractions before sending to HMI
Replies
16
Views
6,377
Hey all, I've reviewed many threads regarding converting one data type to another and I have something I'm working on that I'm stumped on and...
Replies
9
Views
5,865
I'm working with SLC 500 Data File. I have a few numbers like 8.313452e+07 . I forget , how do you convert these numbers . Thanks so much.
Replies
11
Views
2,429
Back
Top Bottom