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

JasonT

  • Newbie
  • *
  • Posts: 6
    • 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: 5322
    • 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: 5322
    • 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: 1447
    • 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 »

JasonT

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Pulling a string from a CompactLogix...
« Reply #4 on: January 23, 2018, 08:58:17 AM »
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

Any help would be great!

Thank you in advance


Also as an FYI...the PLC string a SINT with 82 bytes
« Last Edit: January 23, 2018, 09:01:41 AM by JasonT »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Pulling a string from a CompactLogix...
« Reply #5 on: January 23, 2018, 10:15:44 AM »
Add this line of code at the first line of the ExtractString function, then see what is in the Visual Studio Output Window:

Console.WriteLine("Value as Hex string=" & s)

JasonT

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Pulling a string from a CompactLogix...
« Reply #6 on: January 23, 2018, 10:45:31 AM »
Getting an error that says...

Quote
Severity   Code   Description   Project   File   Line   Suppression State
Error   BC30389   's' is not accessible in this context because it is 'Friend'.      58   Active

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Pulling a string from a CompactLogix...
« Reply #7 on: January 23, 2018, 11:20:13 AM »
Did you put the code like this:
Code: [Select]
Private Function ExtractString(ByVal s As String) As String
        Console.WriteLine("Value as Hex string=" & s)
        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

JasonT

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Pulling a string from a CompactLogix...
« Reply #8 on: January 23, 2018, 11:36:52 AM »
I was a bonehead...lol...I didn't put it in the right place.

As for the Output it is saying the same thing as before that the destination array is not long enough

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Pulling a string from a CompactLogix...
« Reply #9 on: January 23, 2018, 11:41:18 AM »
In the output Window you should also get

Value as Shex String=?????

I'm wanting to see what the value in hex format is,

JasonT

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Pulling a string from a CompactLogix...
« Reply #10 on: January 23, 2018, 11:48:46 AM »
Here is the entire output log...


Quote
'WARREN 5805.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WARREN 5805.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'D:\Current Projects\JOBS\WARREN\CMT LASER\HMI\AdvancedHMI\bin\Debug\WARREN 5805.exe'. Symbols loaded.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualBasic\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Remoting\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'D:\...\HMI\AdvancedHMI\bin\Debug\AdvancedHMIDrivers.dll'. Symbols loaded.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'D:\...\HMI\AdvancedHMI\bin\Debug\MfgControl.AdvancedHMI.Drivers.dll'. Module was built without symbols.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'D:\...\HMI\AdvancedHMI\bin\Debug\AdvancedHMIControls.dll'. Symbols loaded.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'D:\...\HMI\AdvancedHMI\bin\Debug\MfgControl.AdvancedHMI.Controls.dll'. Module was built without symbols.
'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll'. Cannot find or open the PDB file.
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
Value as Hex string=65
Exception thrown: 'System.ArgumentException' in mscorlib.dll
An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code
Destination array is not long enough to copy all the items in the collection. Check array index and length.

'WARREN 5805.exe' (CLR v4.0.30319: WARREN 5805.exe): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Pulling a string from a CompactLogix...
« Reply #11 on: January 23, 2018, 12:07:53 PM »
The Value is only a 65, so it can't be reading a custom length string. Any custom length string will return at least 8 characters such as 01000000, which represents the length in hex format, then there will be the number of hex values representing the characters.

Are you completely sure the tag you are reading is a custom length string?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Pulling a string from a CompactLogix...
« Reply #12 on: January 23, 2018, 12:10:53 PM »
I think I may be leading you down the wrong path because I may be misunderstanding the goal.

If you look at the string in RSLogix, do you see '65' ?

JasonT

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Pulling a string from a CompactLogix...
« Reply #13 on: January 23, 2018, 12:25:21 PM »
The tag I am reading is only part of the string. The string is WX,OK,A,W5G18023$r$1D14198085 and the .DATA[6] is the tag that I am reading which is the character "A" in the string.
Since that is the only information I need from that string, that is the tag I used

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Pulling a string from a CompactLogix...
« Reply #14 on: January 23, 2018, 12:47:37 PM »
Ok. So I completely misunderstood and went down the totally wrong path. This will be much simpler.

Where you had this code:

Label1.Text=ExtractString(BasicLabel1.Text)

Change it to this:

Label1.Text=chr(BasicLabel1.Text)