DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

12 bit signed number

I'm having a problem dealing with a 12 bit signed binary number. I have a eI16 channel, and need to mask off the 4 MSB's. These 4 MSB bits represent an address location that we don't care about. The actual sign bit is the 12th bit. So, this is what I have:


Set Channel(32) = Block.Channels.Add("Electrical Angle", eI16)
Channel(32).Formatter.Bitmask = 4095 'xxxx 1111 1111 1111. i want this channel to give me values from +/-2048

Any ideas??
0 Kudos
Message 1 of 5
(7,565 Views)
Hi Matt,

Use the bit manipulation functions to make your own procedure. First you must extract the value of your MSB (bit 11). Then clear that bit to zero. Check the value of your sign bit and set your data to negative if it is 1.

channel_val = 4095
sign_bit = GetB(channel_val, 11) '0b0000 1000 0000 0000
channel_val = ClrB(channel_val, 11)
if sign_bit = 1 then channel_val = -channel_val

After running this code, channel_val will be -2047.

Good Luck!

Tyler T.
0 Kudos
Message 2 of 5
(7,549 Views)
Hey Tyler,
That looks like it'll work great, but I keep getting an error when I try to run the script. it says:

Variable undefined: "GetB"

and then it says to check my settings. I think i'm missing something. Everytime I try to preform any function that turns red colored in my script window, i always get an error message. For example, the ClrB function or the FormulaCalc function always give me errors. I'm thinking my settings are missing something.
0 Kudos
Message 3 of 5
(7,546 Views)
Hi,

The GetB function is a DIAdem specific function. DataPlugins are kind of running a level below this in an environment called USI (Universal Storage Interface). GetB is not available there.
But even if it would be available, this would not completely solve your problem. Negative Integer numbers are stored as 2's complement. This means that if you have a 16 bit integer which has a value of 2 (01000000 00000000, starting with the MSB from the left), then -2 is not 01000000 00000001. In fact its 01111111 11111111). If you would take the lower bits from your negative number and multiply them by two you would not get what you expect. To calculate -2 from 2 you invert the bits from "2" and add 1. The resulting bitsequence is the valid representation for -2.

Probably you don't want to know all those details but rather have something which works. I don't have an example for a signal decoded as 12 bit signed integer. If you could send me one of your files, I could try it out. Do you have an example file where you know how the signal looks like (e.g. a sine curve) ?

Here is what should work (without having it verified) :

' assuming that "channel_val" is the variable which already contains
' the lower 12 bit from the 16-bit integer read from file

If ( 0 <> (channel_val and 4095) ) Then
' invert the bits and add one
channel_val = (Not channel_val) + 1
' mask out the bits 13-16. The result should be the number we are looking for, but
' positive instead of negative
channel_val = (channel_val and (4095-1))
' make it negative :
channel_val = channel_val * (-1)
Else
' no further step required, its a positive number
End If


As I said, if you could send me an example file and the current state of your plugin I may be in a better position to help

Andreas
0 Kudos
Message 4 of 5
(7,539 Views)

Hi Matt,

What's missing there in the DataPlugin API is bit extension for this case.  Unfortunately in DIAdem 9.1 you'll have to use the workaround that Andreas described here.  For future reference though:

The BitMask property can accept a string as well as an integer.  This makes your code easier to read and edit.  So you can input the BitMask like this:

  Channel(32).Formatter.BitMask = "0x0fff"

Thank you for pointing out this problem,

Myrle

******************
For tips and tricks on creating VBScript DataPlugins go to http://dataplugins.blogspot.com.
0 Kudos
Message 5 of 5
(7,497 Views)