Author Topic: Read String Data  (Read 5805 times)

David Bartel

  • Newbie
  • *
  • Posts: 6
    • View Profile
Read String Data
« on: October 01, 2013, 07:49:28 PM »
I have just started using Advanced HMI and was wondering if there is a way to read a string tag from a Compact Logix PLC

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5288
    • View Profile
    • AdvancedHMI
Re: Read String Data
« Reply #1 on: October 01, 2013, 08:20:43 PM »
Do you want to show it on a form or read it using code? To show on a form just add a BasicLabel and set the PLCAddressValue to the Tag name.

If you are using a Program Scope tag, you have to precede it with "PROGRAM:", following by the program name, then a period and finally the tag name. For example:

PROGRAM:MainProgram.MyTag

David Bartel

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Read String Data
« Reply #2 on: October 02, 2013, 10:54:46 AM »
All my tags are scoped as Controller (makes it easy for me). When I put the address in (for example) STR2.data, All I get is the ASCII equivalent value for the first character.

If I try it simply as STR2 I get "Arithmetic operation resulted in an overflow" error message.

What I want to do is read the string from the PLC and show it (as a string) on the form. It's looks to me like I might need to write a little function to do something like this, but maybe I'm missing something?

David Bartel

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Read String Data
« Reply #3 on: October 02, 2013, 11:10:22 AM »
Archie -

I just tried a little experiment. Rather than use a string tag that has a custom length I just used a generic string (I think the default is 82 characters) and it worked exactly like you said it would!

Rather than try to figure out how to determine the "custom" string length definition, I think I can alter my PLC program to just use the default string length.

Thank You for your help.

David

dmroeder

  • Global Moderator
  • Full Member
  • *****
  • Posts: 210
    • View Profile
Re: Read String Data
« Reply #4 on: October 02, 2013, 12:34:59 PM »
David, I ran into the same thing that you did a while back.  I made a routine that did the following:

1) Read the length element of the string
2) Read the data array of the string (based on what the length was)
3) Assemble the data back into a string and return it

Here is the code I used:

Code: [Select]
    Public Function ReadSillyString(ByVal TagName As String) As String

        PLCCom.IPAddress = txtIPAddress.Text 'Set driver IP Address

        'Read the length of the string
        Dim intLength As Integer = PLCCom.ReadAny(TagName & ".LEN")
        'Read the data array of the string.  This will get the ASCII value of each character

        If intLength > 0 Then
            Dim strDataInTag() As String = PLCCom.ReadAny(TagName & ".DATA", intLength)

            'Byte array that will be used to convert our string array to a byte array
            'This is necessary to make it easy to convert back to a single string
            Dim bytASCIIArray(0 To intLength) As Byte
            For i = 0 To intLength - 1  'Convert each ASCII character to a byte
                bytASCIIArray(i) = Convert.ToByte(strDataInTag(i))
            Next

            'Converts our byte array back to a string and returns it
            Return System.Text.Encoding.ASCII.GetString(bytASCIIArray)
        Else
            Return ""
        End If

    End Function

David Bartel

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Read String Data
« Reply #5 on: October 03, 2013, 09:44:59 AM »
Thank you for the code. I will give it a try a little later. I have kind of a Band-Aid fix working at the moment by making sure the string types in the PLC are all default length definitions.