RSView32 VBA help - Binary to decimal conversion

SysIntegrator

Member
Join Date
Jul 2012
Location
Pittsburgh, PA
Posts
46
Hey,

I'm looking for a bit of guidance on some vba code in RSView32. For the record I know zero (I know its shameful) VBA code.

What I'm trying to do is take 8 Digital tags and convert it back to an Analog tag. I don't have any means to transmit this data as an analog value.

I've never written any VBA code before so I did some looking around and found this site:

http://www.vb-helper.com/howto_decimal_to_binary.html

But I'm not sure how it all looks when it gets finished. I know I need to go and fetch all my discrete tags, but I'm not really sure where my variables get dropped into that example code. So far I've got this:

Public Function GROB35_ERRORCODE(Binary As String) As Long

Set gTagDb.GetTag("GROBS\35_ERROR_0") = BIT_0
Set gTagDb.GetTag("GROBS\35_ERROR_1") = BIT_1
Set gTagDb.GetTag("GROBS\35_ERROR_2") = BIT_2
Set gTagDb.GetTag("GROBS\35_ERROR_3") = BIT_3
Set gTagDb.GetTag("GROBS\35_ERROR_4") = BIT_4
Set gTagDb.GetTag("GROBS\35_ERROR_5") = BIT_5
Set gTagDb.GetTag("GROBS\35_ERROR_6") = BIT_6
Set gTagDb.GetTag("GROBS\35_ERROR_7") = BIT_7

Dim n As Long
Dim s As Integer

For s = 1 To Len(Binary)
n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
(s - 1)))
Next s
BinaryToDecimal = n

Set Error = gTagDb.GetTag("GROBS\35_ERROR")

End Function

Any guidance is appreciated, thanks.
 
Ok, I changed my approach.

I decided since it was only an 8 byte word instead of doing a loop I'd do it the easy way.

Here's the new code:

Sub GROB35_ERRORCODE()

Dim BIT_0 As Boolean
Dim BIT_1 As Boolean
Dim BIT_2 As Boolean
Dim BIT_3 As Boolean
Dim BIT_4 As Boolean
Dim BIT_5 As Boolean
Dim BIT_6 As Boolean
Dim BIT_7 As Boolean
Dim ERROR As Integer

Set BIT_0 = gTagDb.GetTag("GROBS\35_ERROR_0")
Set BIT_1 = gTagDb.GetTag("GROBS\35_ERROR_1")
Set BIT_2 = gTagDb.GetTag("GROBS\35_ERROR_2")
Set BIT_3 = gTagDb.GetTag("GROBS\35_ERROR_3")
Set BIT_4 = gTagDb.GetTag("GROBS\35_ERROR_4")
Set BIT_5 = gTagDb.GetTag("GROBS\35_ERROR_5")
Set BIT_6 = gTagDb.GetTag("GROBS\35_ERROR_6")
Set BIT_7 = gTagDb.GetTag("GROBS\35_ERROR_7")
Set ERROR = ((BIT_7 * 128) + (BIT_6 * 64) + (BIT_5 * 32) + (BIT_4 * 16) + (BIT_3 * 8) + (BIT_2 * 4) + (BIT_1 * 2) + (BIT_0))
Set ERROR = gTagDb.GetTag("GROBS\35_ERROR")

End Sub

Couple issues with this.

1 - It won't compile, it highlights Set BIT_0 and says "Object Required"

2 - I have this as a subroutine under "modules". Back in old RSView32 if I try to run a macro VBAExec "" it does not give me the option to choose this subroutine. What's missing here?

Also, if anybody knows of any tutorials or basic information on the whole VBA ~ RSView32 interfacing I'd appreciate it.

Thanks.
 
Oops, that last line is wrong, how do I write back a tag value?

Set gTagDb.GetTag("GROBS\35_ERROR") = ERROR

That doesn't work does it? I don't know where people are looking up these commands.
 
OK, I finally found the RSView32 examples in the help.

It looks like there's lots of ways to do this, but this is where the help points me if I follow their Farenheit to Celsius conversion example.

Sub GROB35E()
On Error GoTo GROB35E_Error

Dim BIT0 As Tag
Dim BIT1 As Tag
Dim BIT2 As Tag
Dim BIT3 As Tag
Dim BIT4 As Tag
Dim BIT5 As Tag
Dim BIT6 As Tag
Dim BIT7 As Tag
Dim ERR As Tag

Set ERR = gTagDb.GetTag("GROBS\35_ERROR")
Set BIT0 = gTagDb.GetTag("GROBS\35_ERROR_0")
Set BIT1 = gTagDb.GetTag("GROBS\35_ERROR_1")
Set BIT2 = gTagDb.GetTag("GROBS\35_ERROR_2")
Set BIT3 = gTagDb.GetTag("GROBS\35_ERROR_3")
Set BIT4 = gTagDb.GetTag("GROBS\35_ERROR_4")
Set BIT5 = gTagDb.GetTag("GROBS\35_ERROR_5")
Set BIT6 = gTagDb.GetTag("GROBS\35_ERROR_6")
Set BIT7 = gTagDb.GetTag("GROBS\35_ERROR_7")
ERR.Value = ((BIT7.Value * 128) + (BIT6.Value * 64) + (BIT5.Value * 32) + (BIT4.Value * 16) + (BIT3.Value * 8) + (BIT2.Value * 4) + (BIT1.Value * 2) + (BIT0.Value))
Set ERR = Nothing
Set BIT0 = Nothing
Set BIT1 = Nothing
Set BIT2 = Nothing
Set BIT3 = Nothing
Set BIT4 = Nothing
Set BIT5 = Nothing
Set BIT6 = Nothing
Set BIT7 = Nothing
Exit Sub

GROB35E_Error:
Select Case ERR.Number
Case roErrorTagValueCommError, roErrorTagEvent
'Communication error, log it and exit
gActivity.Log ERR.Description, roActivityError, _
roActivityCommunication, , "Grob 35 Errors"
Case roErrorTagValueStale
'This error will only happen when using roNoWait
'with OnScan or ReadFromSource, ignore
Resume Next
Case roErrorTagValueUninitialized
'This error will only happen when using roNoWait
'with OnScan or ReadFromSource, ignore
Resume Next
Case Else
'Any other error, log it and exit
gActivity.Log ERR.Description, roActivityError, _
roActivityCommunication, , "Grob 35 Errors"
End Select
Set ERR = Nothing
Set BIT0 = Nothing
Set BIT1 = Nothing
Set BIT2 = Nothing
Set BIT3 = Nothing
Set BIT4 = Nothing
Set BIT5 = Nothing
Set BIT6 = Nothing
Set BIT7 = Nothing
Exit Sub
End Sub

Also, I figured out I needed the subroutine under "This project" (userform) and not a module. Now I can call it up in a Macro, but it says "An error occurred in the Subroutine GROB35E".

So I don't know what the hang up is there, but what I'm struggling with in general is when I need to define my variables as tags vs integers/booleans/whatever. Could I have just defined my variables as booleans, not used the set functions, and placed .Value at the end of each?
 
Could I have just defined my variables as booleans, not used the set functions, and placed .Value at the end of each?

I'm not any help to this application, but I don't think your last idea would work, unless what you wanted was a string of 0 and 1s (as that is all that a boolean bit would return).
 
I have done some VBA code in RSView32, but I really do not understand your explanation for what you are trying to do:

What I'm trying to do is take 8 Digital tags and convert it back to an Analog tag. I don't have any means to transmit this data as an analog value.

The reason I do not understand this is that in RSView, if you import your "digital" tags in one "word" value, then that word already has all the bits with the correct 1 or 0 state, so there is no need to go through any elaborate conversion process.

In fact, when I have run out of RSView tags, I have used that method to import many digital On/Off bits as a single-word tag. RSView has a method to address single bits of any 16-bit tag, called "Derived tags". Put all your individual bits into a PLC word tag, then create an RSView Derived Tag for each bit in the word, as shown in the attached picture. This is for a 16-bit tag named "INPUTS11" (which is nothing more than all the Inputs on a SLC 5/03 Slot 11 16-channel Input Module). Notice that Bit 15 (the left-most bit) is the On/Off input for Proximity Switch 10. To get RSView to look at only Bit 15, do a Boolean AND (&) with the bit-position decimal value (32768 in this case).

I suppose that if you cannot modify your PLC program to put all the bits into word tags, then you reverse the procedure and create RSView Derived Tags to ADD all the Error-code bit tags into derived 16-bit tags (same as in your "ERR.Value computation), if you really must have those for some reason.

RSView32 Derived Tags.jpg
 
Last edited:
I actually ended up getting the above logic to work.

Derived tags would have been a lot less painful way to go about it, but I'm using VBA for some other stuff and am probably going to create some variables to swap out the tag names because I'm doing this with 40 machines.

Addressing all the IO as one word wasn't an option, these values came from a Modbus OPC server that was addressing some DI on a remote IO module. So the syntax of the address really wouldn't let you do that.

Anyways, thanks for the help!
 
Addressing all the IO as one word wasn't an option, these values came from a Modbus OPC server that was addressing some DI on a remote IO module.
Well, yes, that is why I said to take all these bits inside the RSView program and using a Derived Tag with arithmetic expression, convert them to a one-word tag that contains all the data! For me, that is much easier than screwing around with dozens of lines of VBA code. Save the VBA when there is no other way to do it.
 
Last edited:

Similar Topics

Hello all, New to the forums here and glad to find this oasis of RSView/FactoryTalk info! I have a basic VB question I hope can be answered...
Replies
4
Views
2,308
Hi, I'm pretty new to the RSVIEW32 & have encounted a problem whilst coding visual basic. The error is:- Compile Error: Sub or Function not...
Replies
6
Views
2,165
Good friends, I'm new to the forum, I'm working with RSview32, someone can help me how to make a macro in VBA, to save some data in Excel. to...
Replies
6
Views
1,954
We were given RSView32 project files, for quoting, that will be converted to FTView SE. Unfortunately the project has VBA references that were...
Replies
9
Views
4,319
Hi, I got a RSview32 project from site with VBA scripts and trying to run on my local machine to make some graphical changes. VB scripts are used...
Replies
6
Views
3,495
Back
Top Bottom