DASYLab

cancel
Showing results for 
Search instead for 
Did you mean: 

Negative binary input

Solved!
Go to solution

Forgive me isf this has been answered elsewhere, but my colleague and I have been struggling to find an easy way to import a binary number from a PLC when it goes negative. It uses the 2's compliment method (ie +/- 2^15 with the  first digit of the 16bit number representing the parity).

 

I have managed a work around using five modules and two variables to convert the input into the correct number:

 

What we want is a range from -32767 to 32768, but without conversion it is displayed as 0 to 65535 ( ie 2^16)

 

what is currently displaying as 65535, should be -1, 65534 should be -2, etc

 

To convert this I used an arithmetic module to generate the opposite number: c-65536, so we now have two numbers: 0 to 65535 and -65536 to -1

 We just need to pick the correct one depending on the input value:

 

For numbers 0 to 32768, we want that number (positive)

For number 32769 to 65535 we want the opposite number, ie -1 to -32767 (negative) so...

 

what I did was write the two numbers generated to two variables.

 

I then setup an action module to set one variable at a time to zero (so if the input is 0-32768 or positive, the action module sets the negative variable to 0, if the input is for a negative number or 32769-65535, then the positive variable is set to 0)

 

When these two variables are then read back in and added together, you get the correct number as one of the results is always 0, the other is either the positive value or the negative value.

 

My point is that this works, but it is awkward and I was wondering if there is a simpler way to do it? Does Dasylab have a module that can sort out a 2's complimented number?

 

Thanks

 

0 Kudos
Message 1 of 9
(8,297 Views)
If I understand correctly, you have a 16 bit signal where the bit 0 is the sign, correct?
Tom Rizzo
InSyS Corp.
www.insyscorp.com
Your DASYLab integrator
0 Kudos
Message 2 of 9
(8,294 Views)

Sorry for the confusion.

 

you said that 65535 is supposed to be -1, so 1 what is supposed to be?

 

Also, can you append your worksheet?

Tom Rizzo
InSyS Corp.
www.insyscorp.com
Your DASYLab integrator
0 Kudos
Message 3 of 9
(8,291 Views)

How are you importing the number? What module? For example, the RS232 module understands signed and unsigned integer binary values and will do the appropriate conversion.

 

You can set up the Formula module to do the test and the math at the same time.

 

((IN(0) > 2^15) * (2^15-  IN(0) )) + ((IN(0) <= 2^15)* IN(0) )

 

 

 

 

Measurement Computing (MCC) has free technical support. Visit www.mccdaq.com and click on the "Support" tab for all support options, including DASYLab.
0 Kudos
Message 4 of 9
(8,286 Views)

Just to fully explain, I have attached a demo of the modules working using a slider rather than the DDE input. (rename the extension to .dsb)

 

There is also a picture attached

 

Message Edited by wideboydave on 11-24-2009 09:51 AM
Download All
0 Kudos
Message 5 of 9
(8,280 Views)
Solution
Accepted by topic author wideboydave

signed integer.jpg

Try my Formula math instead. It's only one module.
I used a switch and the bit-mask module to create the full range of integers.

 

 

Message Edited by CJ Butler -- DASYLab Pro on 11-24-2009 11:00 AM
Measurement Computing (MCC) has free technical support. Visit www.mccdaq.com and click on the "Support" tab for all support options, including DASYLab.
Message 6 of 9
(8,274 Views)

Hmmm, your formula wasn't quite right, but the idea was sound. If you change the formula to: ((IN(0) > 2^15) * (IN(0) - 2^16 )) + ((IN(0) <= 2^15)* IN(0) ) it works as it should.

 

Thanks, I didn't know you could use formulas in this way.

0 Kudos
Message 7 of 9
(8,270 Views)
Great. Glad it works. Formulas are much more powerful than many people realize. The syntax is pretty awful, but, once you figure it out, they work well.
Measurement Computing (MCC) has free technical support. Visit www.mccdaq.com and click on the "Support" tab for all support options, including DASYLab.
Message 8 of 9
(8,268 Views)

Yeah, coming from a visual basic and Excel background.. that is fairly unfamiliar syntax-wise, but understandable none-the-less.

 

I am still surprised that there isn't a specific module in Dasylab for importing 2's compliment numbers from PLCs as it is a global standard for PLC outputs to cope with negative values.

 

We happen to be measuring vacuum and this example is looking at the exponent (ie the value we want is something like 3.2x10^-3mbar, or 0.0032mbar) but our output is expressed as a mantissa with 3 significant figures (320 in this case) which is always positve and an exponent (-3 in this case). My colleague was confused when he got an error with a number of 3.2x10^65,533! He is most pleased that we've now got it down to one module!

 

Thanks again.

0 Kudos
Message 9 of 9
(8,263 Views)