AdvancedHMI Software
General Category => Support Questions => Topic started by: sramirez 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
-
UDT within a UDT or nested UDT. Here is an example that works:
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
-
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)
-
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'
-
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
-
It worked for me:
Public Class UDT_Test
Public Limits(1) As U01_Limits
Public S20 As String
End Class
-
What version are you using?
I am running 3.9.9.2533.
Thanks again for your help.
Shawn R
-
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:
Public Class U02_Station
Public Limits(1) As U01_Limit
Public Name20 As String
End Class
But this wont work:
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:
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
Public Class U02_Station
Public Name20 As New String20
Public Limits(1) As U01_Limit
End Class
-
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