Author Topic: ReadUDT() problem with array of UDT  (Read 716 times)

sramirez

  • Newbie
  • *
  • Posts: 18
    • View Profile
ReadUDT() problem with array of UDT
« on: December 16, 2022, 11:30:19 AM »
I am attempting to use the ReadUDT() function with a UDT that has a an array(1).
The array type is a second UDT which has a tag that is a defined length string.

I get an "Index was out of range." error when making the call   plc.ReadUDT(Of sr_ParentUDT)("srTestTag")

The tags are in the attached image.

If I change srTestTag.TestList[0].Name from a defined length string of 20 to just a string it works fine.

If I use ReadUDT() on the array element directly the defined length string works as expected.
plc.ReadUDT(Of sr_ChildUDT)("srTestTag.TestList[0]")


Hopefully this makes sense.  It is hard to describe in words.

Thanks for any help.
Shawn R
« Last Edit: December 16, 2022, 11:43:14 AM by sramirez »

bachphi

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: ReadUDT() problem with array of UDT
« Reply #1 on: December 16, 2022, 02:19:01 PM »
UDT within a UDT or nested UDT.  Here is an example that works:

Code: [Select]
Public Class U01_Limits
        Public LowLimit As Single
        Public Hi_Limit As Single
    End Class

    Public Class UDT_Test
        Public Limits(1) As U01_Limits
    End Class

...

        Dim myTestUDT As UDT_Test = PLC.ReadUDT(Of UDT_Test)("myUDT")


For further example, see here:

https://www.advancedhmi.com/forum/index.php?topic=2670.msg16897#msg16897
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

sramirez

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: ReadUDT() problem with array of UDT
« Reply #2 on: December 16, 2022, 03:01:37 PM »
Thank you for the reply, but the problem comes to the surface when a property is a string with a max size defined.

So in your example, if Hi_Limit was a string in the vb code but in the PLC was defined as a string data type with a max character limit.  (see image)

bachphi

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: ReadUDT() problem with array of UDT
« Reply #3 on: December 16, 2022, 04:22:14 PM »
In the other post from the link I gave, I stated that custom length of string does work.

And why do like to repeat your mistake as seeing in your previous post. Check  'Property'
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

sramirez

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: ReadUDT() problem with array of UDT
« Reply #4 on: December 19, 2022, 07:43:39 AM »
Thank you again for your help.

1.  I saw your statement that custom length strings work.  In my working sample app I made 1 change and that was to make 1 tag have a max length.  This change caused the app to no longer work.  I don't know what I am doing wrong (because custom string lengths should work according to your post) so that is why I am asking for help.
2. I normally use Public Property so that I can define public access to variables.  The ReadUDT() appears to like Public over Public Property when using arrays so I switched to Public in those cases.
3. I changed my sample app's code from using "Public Property" to just "Public" and continue to error when using a custom length string.

Thanks,
Shawn R


bachphi

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: ReadUDT() problem with array of UDT
« Reply #5 on: December 19, 2022, 08:08:05 AM »
It worked for me:

Code: [Select]
    Public Class UDT_Test
        Public Limits(1) As U01_Limits
        Public S20 As String
    End Class
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

sramirez

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: ReadUDT() problem with array of UDT
« Reply #6 on: December 19, 2022, 04:11:00 PM »
What version are you using? 
I am running 3.9.9.2533.

Thanks again for your help.
Shawn R

bachphi

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: ReadUDT() problem with array of UDT
« Reply #7 on: December 19, 2022, 08:43:21 PM »
I am using 3.9.9.2538 which is the latest version of AHMIv399yBeta38.
But I was just lucky both times apparently.

Here is what I meant, below is an example that works:
Code: [Select]
    Public Class U02_Station
        Public Limits(1) As U01_Limit
        Public Name20 As String
    End Class

But this wont work:
Code: [Select]
    Public Class U02_Station
        Public Name20 As String
        Public Limits(1) As U01_Limit
    End Class

Since custom string is treated as a UDT. you will need to define it first:
Code: [Select]
    Public Class String20
        Public Length As Integer
        Public Characters() As System.SByte
        Public Sub New()
            MyBase.New
            Me.Characters = New System.SByte((20) - 1) {}
        End Sub
        Public Sub New(ByVal value As String)
            MyBase.New
            Me.Length = Math.Min(20, value.Length)
            If (value.Length > 20) Then
                value = value.Substring(0, 20)
            End If

            Dim TmpArray() As Byte = New Byte((20) - 1) {}
            System.Text.ASCIIEncoding.ASCII.GetBytes(value).CopyTo(TmpArray, 0)
            Me.Characters = CType(CType(TmpArray, Array), System.SByte())
        End Sub
        Public Overrides Function ToString() As String
            Return System.Text.Encoding.ASCII.GetString(Array.ConvertAll(Characters, Function(a) CByte(a)))
        End Function
    End Class

then

Code: [Select]
    Public Class U02_Station
        Public Name20 As New String20
        Public Limits(1) As U01_Limit
    End Class
« Last Edit: December 19, 2022, 08:45:03 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

sramirez

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: ReadUDT() problem with array of UDT
« Reply #8 on: December 20, 2022, 08:02:58 AM »
Thank you so much, I have it working now.  I hadn't considered that the custom string is a UDT.

Thank you for the example code.  It saved me a lot of time.

Shawn R