General Category > Support Questions

Pulling a string from a CompactLogix...

(1/3) > >>

JasonT:
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:
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:
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: ---Label1.Text=ExtractString(BasicLabel1.Text)

--- End code ---

- After the End Sub, enter this code:

--- Code: ---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

--- End code ---

Godra:
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.

JasonT:
First Thank you for the help on this issue. Sorry it has taken me so long to get back to this one, I was waiting on the customer. I have used the code provided and I am now getting an error when I try and run the solution. Here is the error...


--- Quote ---System.ArgumentException
  HResult=0x80070057
  Message=Destination array is not long enough to copy all the items in the collection. Check array index and length.
  Source=mscorlib
  StackTrace:
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.BitConverter.ToInt64(Byte[] value, Int32 startIndex)
   at MfgControl.AdvancedHMI.frmManual.ExtractString(String s) in D:\Current Projects\JOBS\WARREN\CMT LASER\HMI\AdvancedHMI\frmManual.vb:line 64
   at MfgControl.AdvancedHMI.frmManual.BasicLabel1_ValueChanged(Object sender, EventArgs e) in D:\Current Projects\JOBS\WARREN\CMT LASER\HMI\AdvancedHMI\frmManual.vb:line 57
   at AdvancedHMIControls.BasicLabel.OnvalueChanged(EventArgs e) in D:\Current Projects\JOBS\WARREN\CMT LASER\HMI\AdvancedHMIControls\Controls\BasicLabel.vb:line 729
   at AdvancedHMIControls.BasicLabel.set_Value(String value) in D:\Current Projects\JOBS\WARREN\CMT LASER\HMI\AdvancedHMIControls\Controls\BasicLabel.vb:line 89

--- End quote ---

Any help would be great!

Thank you in advance


Also as an FYI...the PLC string a SINT with 82 bytes

Navigation

[0] Message Index

[#] Next page

Go to full version