Author Topic: Pulling a string from a CompactLogix...  (Read 87 times)

JasonT

  • Newbie
  • *
  • Posts: 1
    • View Profile
Pulling a string from a CompactLogix...
« on: January 12, 2018, 03:31:50 PM »
Hello all,

I am trying to pull a string from a CompactLogix and display the response in a basic label. I used the string tag in the label PLCValue and I see a response, However it displays as the ASCII code. Is there a way to display it as a letter instead of the ASCII for the letter?

Thanks for any help in this matter.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 3653
    • View Profile
    • AdvancedHMI
Re: Pulling a string from a CompactLogix...
« Reply #1 on: January 12, 2018, 04:20:43 PM »
I will guess it is a custom length string. In that case, it cannot be displayed directly with a BasicLabel. It requires reading with a special function in code.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 3653
    • View Profile
    • AdvancedHMI
Re: Pulling a string from a CompactLogix...
« Reply #2 on: January 12, 2018, 07:48:05 PM »
Here is one way to do it:

- From the Toolbox under the All Windows Forms group, add a Label to your form
- Click once on the BasicLabel you have reading the custom length string
- In the Properties Window, click the lightening bolt to see the selection of event
- Double in the ValueChanged to get back to code
- Enter this code:
Code: [Select]
Label1.Text=ExtractString(BasicLabel1.Text)

- After the End Sub, enter this code:
Code: [Select]
Private Function ExtractString(ByVal s As String) As String
        Dim bytes((s.Length / 2) - 1) As Byte
        For i = 0 To (s.Length / 2) - 1
            bytes(i) = Byte.Parse(s.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber)
        Next
        Dim StringLength As Integer = BitConverter.ToInt32(bytes, 0)
        Dim StringResult As String = System.Text.Encoding.Default.GetString(bytes, 4, bytes.Length - 4)

        Return StringResult
    End Function

Godra

  • Hero Member
  • *****
  • Posts: 624
    • View Profile
Re: Pulling a string from a CompactLogix...
« Reply #3 on: January 12, 2018, 09:24:48 PM »
You can also try using the attached modified BasicLabel, which has Archie's code included.

Make sure to set the new InterpretValueAsCLXString property to True (this should be used for reading custom length strings only).

It also allows to write custom length string by using the Keypad/AlphaKeyboard pop up, which requires adding the tag to the PLCAddressKeypadCollection.
These writes are set to work only when the previously mentioned InterpretValueAsCLXString property is set to True.
« Last Edit: January 15, 2018, 11:53:33 AM by Godra »