Isolate bit

Share your scripts written for the Script Device in this forum.
Post Reply
Thales
Member
Posts: 46
Joined: Sat Sep 29, 2007 3:52 pm

Isolate bit

Post by Thales »

Please I received this message from Richard Naninck, but now he is out of office.
Some one knows how to do the second part of this ? ISOLATE BIT

___________________________
That seems very binary to me. Switch info is stored in one byte and should therefore have 8 switches. One switch per bit.
I take it you get this like some kind of hex data because 255 wouldn't fit in 01xx so it would be 01FF where all units on address 1 are on.

You need to parse out the receive data.

Get the complete data xxyy and split it into Left(Data, 2) and Right(Data, 2). That way you get the address and the switch data in two variables.

The switch data needs to be looked at through a loop.

For nit = 0 to 8
If data And (2 ^ nit) = 1 Then
switch(nit) = "On"
End If
Next

Above you see a loop that loops through all sixteen switches from the selected address. Then the data gets checked against an And statement and if it returns a 1 then that switch is on and if it is 0 then that switch is off.
Basic binary stuff.

01 and 1 = 1
02 and 1 = 0
03 and 1 = 1
03 and 2 = 1
04 and 1 = 0
04 and 2 = 0
04 and 4 = 4

Isolate each switch (bit) in the data byte and And it so you test each switch for on (1) and off (0)
2 ^ 0 = 1
2 ^ 1 = 2
2 ^ 2 = 4
2 ^ 3 = 8
2 ^ 4 = 16
etc
markd
Advanced Member
Posts: 234
Joined: Fri Jul 21, 2006 4:32 pm

Post by markd »

The usual way of isolating a bit is to mask the other bits out. . . are you familiar with binary?

00000000 = 0
00000001 = 1
00000010 = 2
00000100 = 4

etc

that is what he is talking about in the section just above where you have bolded. I'm not sure what the bold is talking about, other than that it is a list of the bit values that you would need to AND against.

So if you want to know if the rightmost bit is set, you AND your variable with 1. If the result is TRUE, the bit is set, otherwise not.

HTH

Markd
Thales
Member
Posts: 46
Joined: Sat Sep 29, 2007 3:52 pm

Post by Thales »

Markd,

Sorry but I do not understand yet.

I would like to know what output is ON or OFF, but without to use all 255 command lines to check this.
I know that is possible to know like you said
00000001 - output 1 is ON
00000010 - output 2 is ON
etc..

But how to do this on Script file?

thank you
markd
Advanced Member
Posts: 234
Joined: Fri Jul 21, 2006 4:32 pm

Re: Isolate bit

Post by markd »

Richards code above does this-

For nit = 0 to 7 ( not 8 as he has above- that is a typo ;-)
If data And (2 ^ nit) = 1 Then
switch(nit) = "On"
else
switch(nit) = "Off" (I'm adding this to set back to Off if the bit isn't set)
End If
Next

this code checks 8 bits putting the result into the switch array. . . so switch(0) will be "On" if data = XXXXXXX1 where X means don't care- ie switch(0) is not influenced by any of the other bits.
switch(1) will be "On" if data = XXXXXX1X. etc etc etc.

Does that make sense? I think it will work that way, I can't try it just now.

Markd
Thales
Member
Posts: 46
Joined: Sat Sep 29, 2007 3:52 pm

Post by Thales »

Please What am I doing wrong?
Below my script file.
I am using the PLasma Status from Richard, but after to continue my I have many problens.

________________________

Dim Action
Dim ReceivedData
Dim OldReceivedData
Dim HexBytes()
Dim DecBytes()
Dim Data
Dim change(1)
Dim Modulo



'-------------------------------------------------------
'- Main: Checks Received Data and Handles Send Data ----
'-------------------------------------------------------
Do
Sleep 1
ReceivedData = GetPropertyValue("Ler saidas.Received Data")
If OldReceivedData <> ReceivedData Then
OldReceivedData = ReceivedData
Call Handle_ReceivedData(ReceivedData)
End If



Loop


'-------------------------------------------------------
Sub Handle_ReceivedData(Data)
Dim nit
Dim Message
Dim Length
Dim Value
Dim HexData
Dim CommandBytes
Dim n





Message = ""

Length = Len(Data)

ReDim HexBytes(Length)
ReDim DecBytes(Length)

'If Length > 2 Then
For nit = 1 To Length
Value = Asc(Mid(Data, nit, 1)) ': MsgBox " value " & Value

If Value = 255 Then '255 Wordt gereset naar 0 omdat in HouseBot het 0-character wordt gezien als 255
Value = 0
End If

HexData = Hex(Value) ': MsgBox "HexData " & Hexdata
DecBytes(nit) = Value ': MsgBox "value2 " & value

If Value < 16 Then
Message = Message + "0" + HexData
HexBytes(nit) = "0" + HexData ': MsgBox "nit " & nit
Else
Message = Message + HexData
HexBytes(nit) = HexData ': MsgBox " HexData2 " & HexData
End If

If nit < Length Then
Message = Message + " "
End If
Next

from here

CommandBytes = HexBytes(1) & HexBytes(2)

If CommandBytes = 103 Then

Call Modulo_01(State)

End If
'-------------------------------------

Sub Modulo_01(State) Here I received a error message

'Msgbox "CommandBytes" & CommandBytes


Modulo = HexBytes(3)


MsgBox "Modulo" & Modulo


For n = 0 To 7: MsgBox "n" & n
: MsgBox "Modulo" & Modulo
If Modulo And (2 ^ n) = 1 Then
change(n) = "On"
'else
'change(n) = "Off"
End If
Next


End If

Dim Action
Dim ReceivedData
Dim OldReceivedData
Dim HexBytes()
Dim DecBytes()
Dim Data
Dim change(1)
Dim Modulo



'-------------------------------------------------------
'- Main: Checks Received Data and Handles Send Data ----
'-------------------------------------------------------
Do
Sleep 1
ReceivedData = GetPropertyValue("Ler saidas.Received Data")
If OldReceivedData <> ReceivedData Then
OldReceivedData = ReceivedData
Call Handle_ReceivedData(ReceivedData)
End If



Loop


'-------------------------------------------------------
Sub Handle_ReceivedData(Data)
Dim nit
Dim Message
Dim Length
Dim Value
Dim HexData
Dim CommandBytes
Dim n





Message = ""

Length = Len(Data)

ReDim HexBytes(Length)
ReDim DecBytes(Length)

'If Length > 2 Then
For nit = 1 To Length
Value = Asc(Mid(Data, nit, 1)) ': MsgBox " value " & Value

If Value = 255 Then '255 Wordt gereset naar 0 omdat in HouseBot het 0-character wordt gezien als 255
Value = 0
End If

HexData = Hex(Value) ': MsgBox "HexData " & Hexdata
DecBytes(nit) = Value ': MsgBox "value2 " & value

If Value < 16 Then
Message = Message + "0" + HexData
HexBytes(nit) = "0" + HexData ': MsgBox "nit " & nit
Else
Message = Message + HexData
HexBytes(nit) = HexData ': MsgBox " HexData2 " & HexData
End If

If nit < Length Then
Message = Message + " "
End If
Next

CommandBytes = HexBytes(1) & HexBytes(2)

If CommandBytes = 103 Then

Call Modulo_01(State)

End If
'-------------------------------------

Sub Modulo_01(State)

'Msgbox "CommandBytes" & CommandBytes


Modulo = HexBytes(3)


MsgBox "Modulo" & Modulo


For n = 0 To 7: MsgBox "n" & n
: MsgBox "Modulo" & Modulo
If Modulo And (2 ^ n) = 1 Then
change(n) = "On"
'else
'change(n) = "Off"
End If
Next


End If

And Now what Have I to do to use the information to set this property below?


:setPropertyvalue "CP8SR-1/2.Power State","On"

:setPropertyvalue "CP8SR-1/2.Power State","Off"


'End Select

'End Select
'End If
End Sub



'End Select
'End If
End Sub
Thales
Member
Posts: 46
Joined: Sat Sep 29, 2007 3:52 pm

Post by Thales »

Sorry this is the Script.


'Attribute VB_Name = "Module1"
Option Explicit
'On Error Resume Next

Dim Action
Dim ReceivedData
Dim OldReceivedData
Dim HexBytes()
Dim DecBytes()
Dim Data
Dim change(1)
Dim Modulo



'-------------------------------------------------------
'- Main: Checks Received Data and Handles Send Data ----
'-------------------------------------------------------
Do
Sleep 1
ReceivedData = GetPropertyValue("Ler saidas.Received Data")
If OldReceivedData <> ReceivedData Then
OldReceivedData = ReceivedData
Call Handle_ReceivedData(ReceivedData)
End If



Loop


'-------------------------------------------------------
Sub Handle_ReceivedData(Data)
Dim nit
Dim Message
Dim Length
Dim Value
Dim HexData
Dim CommandBytes
Dim n





Message = ""

Length = Len(Data)

ReDim HexBytes(Length)
ReDim DecBytes(Length)

'If Length > 2 Then
For nit = 1 To Length
Value = Asc(Mid(Data, nit, 1)) ': MsgBox " value " & Value

If Value = 255 Then '255 Wordt gereset naar 0 omdat in HouseBot het 0-character wordt gezien als 255
Value = 0
End If

HexData = Hex(Value) ': MsgBox "HexData " & Hexdata
DecBytes(nit) = Value ': MsgBox "value2 " & value

If Value < 16 Then
Message = Message + "0" + HexData
HexBytes(nit) = "0" + HexData ': MsgBox "nit " & nit
Else
Message = Message + HexData
HexBytes(nit) = HexData ': MsgBox " HexData2 " & HexData
End If

If nit < Length Then
Message = Message + " "
End If
Next


Here starts to be differente

CommandBytes = HexBytes(1) & HexBytes(2)

If CommandBytes = 103 Then

Call Modulo_01(State)

End If
'-------------------------------------

Sub Modulo_01(State) Here I received a error message

'Msgbox "CommandBytes" & CommandBytes


Modulo = HexBytes(3)


MsgBox "Modulo" & Modulo


For n = 0 To 7: MsgBox "n" & n
: MsgBox "Modulo" & Modulo
If Modulo And (2 ^ n) = 1 Then
change(n) = "On"
'else
'change(n) = "Off"
End If
Next


End If

What Have I to do here?

:setPropertyvalue "CP8SR-1/2.Power State","On"

:setPropertyvalue "CP8SR-1/2.Power State","Off"
[/b]
markd
Advanced Member
Posts: 234
Joined: Fri Jul 21, 2006 4:32 pm

Post by markd »

I'm not sure what is causing your error messages. . . one thing is that you have

Dim change(1)

Which means an array of one location, then in the sub you try to access 8 locations of it. Try

Dim change(8)


I think you need to either un-comment the

'else
'change(n) = "Off"

or clear change() somewhere else. Otherwise once you see the On command, you will never turn it off.

if (change(x)=On) then

setPropertyvalue "CP8SR-1/2.Power State","On"

else

setPropertyvalue "CP8SR-1/2.Power State","Off"

Where x = the bit position of the bit that indicates power state.

7 6 5 4 3 2 1 0

Markd
Thales
Member
Posts: 46
Joined: Sat Sep 29, 2007 3:52 pm

Post by Thales »

I made same changes, and the Script works.
But I received the message "subscribed out of the interval:'[number:3]'
line 79, it refer to the bold below.

Are there any incompatibility with this?

For nit = 1 To Length
Value = Asc(Mid(Data, nit, 1)) ': MsgBox " value " & Value

If Value = 255 Then '255 Wordt gereset naar 0 omdat in HouseBot het 0-character wordt gezien als 255
Value = 0
End If

HexData = Hex(Value) ': MsgBox "HexData " & Hexdata
DecBytes(nit) = Value ': MsgBox "value2 " & value

If Value < 16 Then
Message = Message + "0" + HexData
HexBytes(nit) = "0" + HexData ': MsgBox "nit " & nit
Else
Message = Message + HexData
HexBytes(nit) = HexData ': MsgBox " HexData2 " & HexData
End If

If nit < Length Then
Message = Message + " "
End If
Next

HexBytes(3)=state

For n 0 to 7
If state And (2^n) = 1 Then
change(n) = "On"
End If
Next

'-------------------------------------

Sub Resultado


Modulo = Hexbytes(3)


For n = 0 to 7 : MsgBox "n" & n
: MsgBox "Modulo" & Modulo
If Modulo And (2 ^ n) = 1 Then
change(n) = "On"
'else
'change(n) = "Off"
End If
Next
markd
Advanced Member
Posts: 234
Joined: Fri Jul 21, 2006 4:32 pm

Post by markd »

Hi Thales-

I'm not exactly sure where you are in the code- I know one of the other scripts you posted shows a ReDim HexBytes(Length). . . If that is in the same code segment, if Length is less than 4, you would get this error.

Markd
Thales
Member
Posts: 46
Joined: Sat Sep 29, 2007 3:52 pm

Post by Thales »

Markd,

The situation is that.

For nit = 1 to Length.

And I need to my Script
Hexbytes(1)
HexBytes(2)
HexBytes(3)
HexBytes(4)

i.e all is less than 4.

Are there any way to fix this?
markd
Advanced Member
Posts: 234
Joined: Fri Jul 21, 2006 4:32 pm

Post by markd »

This is how HexBytes is dimensioned-

Message = ""

Length = Len(Data)

ReDim HexBytes(Length)
ReDim DecBytes(Length)

'If Length > 2 Then

if Length is less than 4, it sounds like your code will have a problem. Is Data guaranteed to be at least 4 bytes? Why don't you put a msgbox in and see what Length is set to. . . .

You can always change it to HexBytes(5), or whatever the maximum size Data you expect. . . remember that the array is going to be zero based, not one based.

hex(4) will yield

hex(0), hex(1), hex(2), hex(3)

for defined variables.

Markd
Thales
Member
Posts: 46
Joined: Sat Sep 29, 2007 3:52 pm

Post by Thales »

Markd

I change
If Length >3 Then

And now is working well, I put the MsgBox and I was receivins lenght 1, 2 and 3.
Now I think it was fix thank you

Thales
markd
Advanced Member
Posts: 234
Joined: Fri Jul 21, 2006 4:32 pm

Post by markd »

Great!
Post Reply