Author Topic: Integer Data Out of Range  (Read 1672 times)

opmal7

  • Newbie
  • *
  • Posts: 19
    • View Profile
Integer Data Out of Range
« on: August 15, 2016, 03:36:12 PM »
I'm trying to write a value as an integer to a Micrologix 1100 PLC, using the EthernetIPforSLCMicroCom1 driver.  I'm trying to write the value 35578, and I'm getting an error "unhandled exception" saying that the data is out of range (-32768 to 32767).  Is there any way to do this?

opmal7

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Integer Data Out of Range
« Reply #1 on: August 15, 2016, 04:46:33 PM »
I'm trying to write a value as an integer to a Micrologix 1100 PLC, using the EthernetIPforSLCMicroCom1 driver.  I'm trying to write the value 35578, and I'm getting an error "unhandled exception" saying that the data is out of range (-32768 to 32767).  Is there any way to do this?

Just to clarify a little bit, I'm starting with the hex string '8AFA' which is what I want to write to the PLC.  I'm writing to an integer array, so I did a convert.toint32 before using the EthernetIPforSLCMicroCom1.write command.  I want to write an integer with the hex equivalent of '8AFA' to the PLC.  Thanks!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5323
    • View Profile
    • AdvancedHMI
Re: Integer Data Out of Range
« Reply #2 on: August 15, 2016, 05:05:19 PM »
The driver only works with signed 16 bit integers, so you will need to convert you hex value to the equivalent signed integer. I found this function on the internet that seems to work well:
Code: [Select]
   Function ConvertHex(ByVal hexVal As String) As Integer
        If hexVal(0) >= "8"c Then 'We have a negative value in the 2's complement
            Dim sb As New System.Text.StringBuilder(hexVal.Length)
            For i As Integer = 0 To hexVal.Length - 1
                'Convert one hex digit into an Integer
                Dim hexDigit As Integer = Convert.ToInt32(hexVal(i), 16)

                'Invert and append it as hex digit
                sb.Append((15 - hexDigit).ToString("X"))
            Next i

            'Get the inverted hex number as Integer again
            Dim inverted As Integer = Convert.ToInt32(sb.ToString(), 16)

            'Add 1 to the inverted number in order to get the 2's complement
            Return -(inverted + 1)
        Else
            Return Convert.ToInt32(hexVal, 16)
        End If
    End Function


bachphi

  • Hero Member
  • *****
  • Posts: 674
    • View Profile
Re: Integer Data Out of Range
« Reply #3 on: August 15, 2016, 05:59:01 PM »
or
Code: [Select]
        Dim hexString As String = "8AFA"  'to 16 bit signed integer
        Dim num As Integer = Int16.Parse(hexString, System.Globalization.NumberStyles.HexNumber)
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Godra

  • Hero Member
  • *****
  • Posts: 1449
    • View Profile
Re: Integer Data Out of Range
« Reply #4 on: August 15, 2016, 11:39:11 PM »
Or bachphi's code slightly different:

Code: [Select]
        Dim hexString As String = "8AFA"  'to 16 bit signed integer
        Dim num As Short = Convert.ToInt16(hexString, 16)

opmal7

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Integer Data Out of Range
« Reply #5 on: August 16, 2016, 11:39:31 AM »
or
Code: [Select]
        Dim hexString As String = "8AFA"  'to 16 bit signed integer
        Dim num As Integer = Int16.Parse(hexString, System.Globalization.NumberStyles.HexNumber)

I used this one, and it worked.  Thanks!