A Call to Arms for all VBA Experts

krakenfan69

Lifetime Supporting Member
Join Date
Nov 2011
Location
Alberta
Posts
229
More and more the sites I go to seem to have HMI's with VBA code intertwined into the PLC. I have picked up some books but they seem to focus on Excel and not HMI control. I was wondering if anyone here would be interested in taking on the project of creating a how to for VBA. Perhaps create a program and HMI that could be downloaded and play along at home. Each week or two as time allows show another step in how to use VBA to trigger bits in the HMI or create trend screens that selects trends from a list to load into a template. I just thought it could be a great resource on the forum for those of us new to VBA. Just a thought (hope).

Thanks for looking,

Kraken Fan #69
 
Last edited:
Not natively perhaps but it is my understanding that with a DDE or ActiveX driver it can read and write to and from the PLC. I further beloeve that FT ME does not support VBA if I am correct.

Kraken Fan #69
 
I have manipulated data on plc from factory talk se. I even was working on a setup to push configuration data from a SQL database to a controllogix to run a lab skid. Did some other stuff building tends as well. I would be interested in contributing it would all be AB specific from me however

In all honesty I would love to see a plc programming how to cookbook style text be developed. When I learned computer programming those where the bees knees. Showing you real world examples with detailed descriptions of why things where done in certain ways. So far I have seen nothing similar for plcs.
 
Last edited:
I have manipulated data on plc from factory talk se. I even was working on a setup to push configuration data from a SQL database to a controllogix to run a lab skid. Did some other stuff building tends as well. I would be interested in contributing it would all be AB specific from me however

In all honesty I would love to see a plc programming how to cookbook style text be developed. When I learned computer programming those where the bees knees. Showing you real world examples with detailed descriptions of why things where done in certain ways. So far I have seen nothing similar for plcs.


That's kinda what I was going for. I figured if it takes off it could be a good VBA for HMI 101 kinda thing. I would prefer AB myself as it is pretty widely used but hey, beggers and all...

Kraken Fan #69
 
I'd certainly be interrested to read/participate in such a thread. I've done a fair amount of scripting in the past in VBA under RsView32 mostly but also with VBS under WinCC7.0 but only considder my abilities as just above novice.

I moved into project management about 18 months ago and don't officially write software any more but one of my current projects is likely to involve some scripting to generate csv files in Factory Talk ViewSE.

Nick
 
Spend your time learning Ignition and python. There is even an on-line course to teach children python.

http://python4kids.wordpress.com/

Python is good for the future I think, I'm working on learning it but VBA will continue to be relevant so learning it is valuable. Excel + VBA = POWER.

In-terms of VBA within HMI's, well that's usually used when you need advanced features that aren't natively built into the HMI application itself. Usually you'll see the majority of this reading/writing to databases of some sort then sending that information to a PLC so bulk read/writes may be done in VBA as well.

Not all HMI's use VBA though, some have their own scripting languages as eel so what you learn in VBA may or may not be useful somewhere else. The important thing is to learn the structure, how to write code, declare variables the fundamentals that you can translate to any other language.
 
So where would people like to start?

Declaring Variables?

Declare public variable in the declaration section so that they are accesible by all your sub routines and functions. Variable declared inside a subroutine are private and only accessible inside the subroutine in which they have been declared.

The code below is taken from an RSView32 project. The first declaration is a constant followed by Some custom data types and then some arrays made up of the data types.

Code:
Public Const WL_MaxPunch As Integer = 60         
 
Public Type PunchListRec 'Record of data for one move/punch
    Distance As Double 'index distance
    Punch As Integer    'Punch Number
    strPrint As String * 42    'Text to print
    iIndex As Integer 'Index
    iPurlin As Integer 'Purlin Number
End Type
Public Type PurlinRec           'Record structure for Purlin List
  A_OrdNum As String * 10       'Reference Order number
  'B_SaleOrd As String * 10      'Sales Order number
  C_Quant As Integer             '
  D_Length As Double            '
  E_Bundle As Integer
  F_Punch(0 To WL_MaxPunch) As String * 1
  G_Dist(0 To WL_MaxPunch) As Integer
  H_Type As String * 17
  I_Mark As String * 17
  J_Holes As Integer
  K_LineNo As Integer
  L_Item As Integer         'Item number within Quantity
  M_WorkListItem As Integer 'Entry in Worklist
End Type
 
Public Punchlist() As PunchListRec 'Array of punchlist records
Public PurlinList() As PurlinRec    'Array of Purlin records
Public TempPurlin() As PurlinRec
Public PrintQueue() As String * 42     'Queue of Strings to print
Public WLTotRec As Integer    'Work List current Total number of valid data records
Public PLTotRec As Integer    'Punch List current Total number of valid data records

The code fraction below contains an example of declaring variable inside a subroutine with the Dim instruction.

Code:
Sub AddManPurlin()
' Add Manualy entered purlin data to Worklist.
' The Manually added data is dircted to worklist record 0 and
' then bumbed up the list to record 1.
Dim TagsColl As Tags, TagPath, Tag, strTemp As String, i As Integer, Result As Variant
On Error GoTo ErrHandler
  
   Result = MsgBox("Add this Job to Work List?", vbYesNo)
    If Result = vbNo Then
        Exit Sub
    End If
  
  TagPath = "Display\ManPurlin\"

Nick
 
Great Stuff. I'm Learning already! So these declared variables are used only for the vba correct? As in distance as double then it could read a PLC value into the distance variable and use that info to know it's location or travel? Am I on the right track here or still out in left field?

Thanks for wading in and leading the charge to help us understand VBA!!

Kraken Fan #69
 
Python is good for the future I think, I'm working on learning it but VBA will continue to be relevant so learning it is valuable. Excel + VBA = POWER.

In-terms of VBA within HMI's, well that's usually used when you need advanced features that aren't natively built into the HMI application itself. Usually you'll see the majority of this reading/writing to databases of some sort then sending that information to a PLC so bulk read/writes may be done in VBA as well.

Not all HMI's use VBA though, some have their own scripting languages as eel so what you learn in VBA may or may not be useful somewhere else. The important thing is to learn the structure, how to write code, declare variables the fundamentals that you can translate to any other language.

Thanks Paully,

I do most of my work on Allen Bradley systems so VBA will be my best bet for now I think. Especially with it's use in Excel. Often as you say I need to get those values out of the PLC and into Excel to do reporting for operation in Oil and Gas as you mention.
Mainly reading and writing values to/from the PLC and some other things like creating a dropdown of all available trends in a FTView SE project and displaying the selected trend.
It's still pretty exciting for me when I get some little thing to work with VBA as I'm so new to it. Like getting the light to come on as an apprentice electrician, "I did it!" moments.

Thanks for adding to thread!

Kraken Fan #69
 
In RSView there is no direct link between variables declared in VBA and tags. There are mechanisms by which you can read and write tags and you can also integrate VB forms in to an RSView application.

The application that the previously posted samples are taken from controls a punch table for making steel purlins; it indexes a strip of steel and then operates one of 8 different punches. It reads production requirment files generated from a SAP system, organises them in to a job queue then generates a index list list from the job queue (index distance + operate punch X).

The punch application is very data manipulation intensive and not well suited to normal PLC/SCADA functions so RSView was used with the HMI functions native to RSView while the data requirments were handled in VBA.

Nick
 
Read and Write Tags

Getting VBA to interract with RSView requires reading and writing tags so here goes:

Read or Write a single tag:

Code:
Dim MyTag As Tag
Dim MyVar As Variant
'Read a Tag
  Set MyTag = gTagDb.GetTag(TagName)
  myVar = MyTag.Value
  Set MyTag = Nothing
 
'Write a Tag
Set MyTag = gTagDb.GetTag(TagName)
  MyTag.Value = myVar
  Set MyTag = Nothing

Write multiple tags:

Code:
Dim TagsColl As Tags, i As Integer, Num As Integer, TagPath As String, LineNum As String
On Error GoTo ErrHandler
  
TagPath = "Display\Purlin\"
  Set TagsColl = gTagDb.QueryForTags(TagPath + "*")
  
If CheckPurlinlist() Then 'Test that Purlinlist is valid
    TagsColl.Item(TagPath + "Order_Num").PendingValue = PurlinList(0).A_OrdNum
    TagsColl.Item(TagPath + "LineNo").PendingValue = CStr(PurlinList(0).K_LineNo)
    TagsColl.Item(TagPath + "Type").PendingValue = PurlinList(0).H_Type
    TagsColl.Item(TagPath + "Mark").PendingValue = PurlinList(0).I_Mark
    TagsColl.Item(TagPath + "Bundle").PendingValue = PurlinList(0).E_Bundle
    TagsColl.Item(TagPath + "Quant").PendingValue = PurlinList(0).C_Quant
    TagsColl.Item(TagPath + "Item").PendingValue = PurlinList(0).L_Item
    TagsColl.Item(TagPath + "Holes").PendingValue = PurlinList(0).J_Holes
    TagsColl.Item(TagPath + "Lenght").PendingValue = PurlinList(0).D_Length
    
Else
    TagsColl.Item(TagPath + "Order_Num").PendingValue = "None"
    TagsColl.Item(TagPath + "LineNo").PendingValue = "None"
    TagsColl.Item(TagPath + "Type").PendingValue = "None"
    TagsColl.Item(TagPath + "Mark").PendingValue = "None"
    TagsColl.Item(TagPath + "Bundle").PendingValue = 0
    TagsColl.Item(TagPath + "Quant").PendingValue = 0
    TagsColl.Item(TagPath + "Item").PendingValue = 0
    TagsColl.Item(TagPath + "Holes").PendingValue = 0
    TagsColl.Item(TagPath + "Lenght").PendingValue = 0
    
End If
TagsColl.WritePendingValues roWait
Set TagsColl = Nothing

Read Multiple tags:

Code:
Dim fName As String, PathName As String, sLine As String
Dim Result
Dim TagsColl As Tags, TagPath As String
On Error GoTo ErrHandler
  
  TagPath = "PLC\Calibration\"
  Set TagsColl = gTagDb.QueryForTags(TagPath + "*") 'Point to tags
  ' Update Tag Database
  TagsColl.ScanOn roWait
  OffA = TagsColl.Item(TagPath + "PunchOffsetA").Value
  OffB = TagsColl.Item(TagPath + "PunchOffsetB").Value
  OffC = TagsColl.Item(TagPath + "PunchOffsetC").Value
  OffD = TagsColl.Item(TagPath + "PunchOffsetD").Value
  OffE = TagsColl.Item(TagPath + "PunchOffsetE").Value
  OffF = TagsColl.Item(TagPath + "PunchOffsetF").Value
  OffG = TagsColl.Item(TagPath + "PunchOffsetG").Value
  OffL = TagsColl.Item(TagPath + "PunchOffsetL").Value
  OffM = TagsColl.Item(TagPath + "PunchOffsetM").Value
  OffCut = TagsColl.Item(TagPath + "ShearOffset").Value
  Goset = TagsColl.Item(TagPath + "Goset").Value
  BlankLength = TagsColl.Item(TagPath + "BlankLength").Value
  OffCrop = TagsColl.Item(TagPath + "CropOffset").Value
  TagsColl.ScanOff
  Set TagsColl = Nothing

Note that whenever an object is created using the "Set XXX = object" command, after it is finished with there should always be a "set XXX = Nothing" instruction to prevent potential memory leaks.

Nick
 
In RSView there is no direct link between variables declared in VBA and tags. There are mechanisms by which you can read and write tags and you can also integrate VB forms in to an RSView application.

The application that the previously posted samples are taken from controls a punch table for making steel purlins; it indexes a strip of steel and then operates one of 8 different punches. It reads production requirment files generated from a SAP system, organises them in to a job queue then generates a index list list from the job queue (index distance + operate punch X).

The punch application is very data manipulation intensive and not well suited to normal PLC/SCADA functions so RSView was used with the HMI functions native to RSView while the data requirments were handled in VBA.

Nick


Yes, that is my understanding. The mechanism would be an DDE or OPC Topic would it not?
In the last example of reading and writing Tags is the path TagPath = "Display\Purlin\" your path to your VBA Tags on the PC, a PLC project, or what exactly? I'm trying to get the link between the VBA tags and the actual PLC file right in my head before we go too far.
I'm setting up a ML1400 with a trainer board so I can try to start playing with some reads/writes.

Thanks,

Kraken Fan #69
 
DDE and OPC are for external tag sources that involve data sharing between applications.

The "Tagpath" refers to the structure of the tag database. See the attached picture.

All the tags in the "\Display\Purlin\" folder are memory tags rather than device tags and they only exist so that the machine operator can interract with them through the screen.

Keeping tags in logical groups is a good habit to get into and will make your code more efficient in the long run.

Nick

TagDB.jpg
 

Similar Topics

:) Traffic lights get an F in efficiency Can we help them?
Replies
12
Views
3,233
DaMan
D
Hello everyone. I am working on designing a call light/alarm system I have roughly 20 stations that will each have their own call switch. Then...
Replies
20
Views
401
I've gotten to the learning curve where I can program a call for pump to come on at set point but I'm not sure how to turn the same pump off when...
Replies
1
Views
124
Hi Siemens Experts, I am hoping someone can shed some light on my issue. I have uploaded the code from a S7-300 (317-2PN/DP) using Step 7...
Replies
9
Views
664
I created this FC and it compiles, but when I use it in my MAIN OB1, it appears to have two outputs. See pictures. What am I doing wrong here...
Replies
9
Views
1,469
Back
Top Bottom