Class from XML

jweaver

New member
Joined
Mar 26, 2008
Messages
2
Programming Experience
1-3
Below is an example of the XML file I need to read.

In my program I need to get the IGroup and IOffset information for each "Symbol". How can I write a program that would automatically make a class reading data from the XML file? I could then add this class to my project and use it like this:

Main.SlideTimeout.IGroup

Main.SlideTimeout.Ioffset

Right now I use XPath to search for Symbol by name and load the IGroup and Ioffset info into a structure. This is time consuming when there are alot of Symbols and I have to manually type in each Symbol name. I could load all the symbols into an arraylist of structures but then it is hard to keep them straight by index. Any help would be greatly appreciated.

VB.NET:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--This file is created by the TCatPlcCtrl automatically. Manually changes will be overwritten!-->
<PlcProjectInfo xmlns:p="http://www.beckhoff.com/2002/01/TcPlcProjectDesc">
	<Symbols>
		<Symbol>
			<Name TaskPrio="1">MAIN.SlideTimout</Name>
			<Type>TIME</Type>
			<IGroup>16448</IGroup>
			<IOffset>24</IOffset>
			<BitSize>32</BitSize>
		</Symbol>
		<Symbol>
			<Name TaskPrio="1">MAIN.WriteCycle</Name>
			<Type>BOOL</Type>
			<Comment>
				<![CDATA[Write PB pressed]]>
			</Comment>
			<IGroup>16448</IGroup>
			<IOffset>28</IOffset>
			<BitSize>8</BitSize>
		</Symbol>
	</Symbols>
</PlcProjectInfo>
 
The stuff inside the <> may represent classes and properties. "Main.SlideTimeout" is a value of Name property, values are not class/property candidates.
What you have there is basically this structure:
VB.NET:
Public Structure Symbol
    Public Name As SymbolName, Type, Comment As String, IGroup, IOffset, BitSize As Integer
    Public Structure SymbolName
        Public TaskPrio As Integer, Value As String
    End Structure
End Structure
Not exactly sure what you mean by 'type in name', a computer can easily get all symbol names so you can choose it a combobox or something.
 
Probably not quite the answer you were expecting, but ...

If you dont need all the variables, dont load from the XML file. Query the PLC directly which will automatically return the IndexGroup, IndexOffset and variable type. If you change your project and forget to reload the XML, this way will always return the correct information from the running PLC program. Besides, I've never written a PLC/HMI program yet where I need to view ALL the PLC variables so just access the ones you need.

A section of my code :-

VB.NET:
Imports Twincat.Ads

......

Public Withevents tcADS as new tcAdsClient

....

Public Class cADSVariable

    Private _intHandle As Integer = 0
    Private _strName As String = ""
    Private _lngIndexGroup As Long = 0
    Private _lngIndexOffset As Long = 0
    Private _strType As String = ""
    Private _adsDatatype As AdsDatatypeId
    Private _intSize As Integer
    Private _strComment As String

    Public Property intHandle() As Integer
        Get
            Return _intHandle
        End Get
        Set(ByVal value As Integer)
            Me._intHandle = value
        End Set
    End Property
    Public ReadOnly Property strName() As String
        Get
            Return _strName
        End Get
    End Property
    Public ReadOnly Property lngIndexGroup() As Long
        Get
            Return _lngIndexGroup
        End Get
    End Property
    Public ReadOnly Property lngIndexOffset() As Long
        Get
            Return _lngIndexOffset
        End Get
    End Property
    Public ReadOnly Property strType() As String
        Get
            Return _strType
        End Get
    End Property
    Public ReadOnly Property adsDatatype() As AdsDatatypeId
        Get
            Return _adsDatatype
        End Get
    End Property
    Public ReadOnly Property intSize() As Integer
        Get
            Return _intSize
        End Get
    End Property
    Public ReadOnly Property strComment() As String
        Get
            Return _strComment
        End Get
    End Property

    Public Function GetVariableHandle(ByVal whatVariableName As String) As Int32
        If _intHandle > 0 Then
            'already defined
            Return 0
        End If
        Try
            Dim vIndexValues As ITcAdsSymbol = tcADS.ReadSymbolInfo(whatVariableName)
            With vIndexValues
                _strName = whatVariableName
                _lngIndexGroup = .IndexGroup
                _lngIndexOffset = .IndexOffset
                _strType = .Type
                _adsDatatype = .Datatype
                _intSize = .Size
                _strComment = .Comment
            End With
            If (_lngIndexGroup > 0) OrElse (_lngIndexOffset > 0) Then
                'OK: Index group and/or offset obtained
                Return 0
            Else
                'ERROR: Could not index group and offset
                Return 2
            End If
        Catch e As TwinCAT.Ads.AdsErrorException
            Return Convert.ToInt32(e.ErrorCode)
        Catch e As NullReferenceException
            'ERROR: vIndexValues is null
            Return 3
        End Try
    End Function

End Class
 
Last edited:
Back
Top