AdvancedHMI Software

General Category => Support Questions => Topic started by: opmal7 on August 15, 2016, 03:36:12 PM

Title: Integer Data Out of Range
Post by: opmal7 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?
Title: Re: Integer Data Out of Range
Post by: opmal7 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!
Title: Re: Integer Data Out of Range
Post by: Archie 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

Title: Re: Integer Data Out of Range
Post by: bachphi 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)
Title: Re: Integer Data Out of Range
Post by: Godra 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)
Title: Re: Integer Data Out of Range
Post by: opmal7 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!