Author Topic: Reading Strings from Micro850  (Read 1564 times)

bkane

  • Newbie
  • *
  • Posts: 11
    • View Profile
Reading Strings from Micro850
« on: February 07, 2018, 05:14:30 PM »
Hello,

I am trying to read a value from my CCW program that is a time. (T#1s) Is there a way to output this entire string using a basic label or another type of label? I've tried and gotten the output "e8030000". I understand this is a problem probably stemming from trying to read/write different data types but was hoping there was a way around it.

Thanks

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile

bkane

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Reading Strings from Micro850
« Reply #2 on: February 08, 2018, 01:03:43 PM »
I think this is definitely on the right track but unfortunately it did not work. But it may not work because maybe the data I am reading isn't a string? In CCW it's designated as a "time." For example, whenever I edit this time and set it to 1000 through the hmi I get an output of e803000 which definitely isn't 1000 in hex. Is there a way to read a time value from CCW? Since it seems it isn't a string.
Thanks

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Reading Strings from Micro850
« Reply #3 on: February 08, 2018, 02:46:40 PM »
The driver is returning a hex encoded byte stream because it doesn't recognize the data type being reported by the PLC. You will need to parse the data and convert it to the type you want. The code would be similar to this:
Code: [Select]
Private Function ExtractINT(ByVal s As String) As integer
        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 Result As Integer = BitConverter.ToInt32(bytes)

        Return Result
End Function

I typed that code from memory and did not test it, so it may not be 100% correct.

bkane

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Reading Strings from Micro850
« Reply #4 on: February 08, 2018, 03:14:29 PM »
I had to include the index value in the ToInt32 function. In case someone ever has a similar issue this was the correct code. But Archie was 99% correct.
Code: [Select]
   
Private Function ExtractINT(ByVal s As String) As Integer
        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 Result As Integer = BitConverter.ToInt32(bytes, 0)

        Return Result
    End Function