Author Topic: Any way to write a double (in hex) into integer values?  (Read 2023 times)

opmal7

  • Newbie
  • *
  • Posts: 19
    • View Profile
Any way to write a double (in hex) into integer values?
« on: July 25, 2016, 11:35:34 AM »
I have a sensor that uses an RS-422 signal that contains multiple data types in the rs-422 message.  The sensor is connected to my Micrologix 1100, and everything is sent and received in hex format.  I'm using an integer array (set to Hex/BCD Radix) to store the incoming and outgoing messages.  One of the pieces of data is in double format, and I have a question about how to get this to work with the AdvancedHMI screen.

I want to add a text box or label where the user can input a number in double format (lets say, 1234.5678).  In hex format this would be (40934A456D5CFAAD).  Then, I need to write this value to the integer array (say N1:1-N1:4).  So in the end, N1:1 will contain 4093, N1:2 - 4A45, N1:3 - 6D5C, N1:4 - FAAD.

Anyone have any tips on how to accomplish this?  Thanks.

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Any way to write a double (in hex) into integer values?
« Reply #1 on: July 25, 2016, 11:48:31 AM »
maybe these two functions would help :

Code: [Select]
  Private Function Hex2Bytes(ByVal Hex As String) As Byte()  'HexString to Byte Array
        Dim bytes As New List(Of Byte)
        For i As Integer = 0 To Hex.Length - 1 Step 2
            bytes.Add(Byte.Parse(Hex.Substring(i, 2), Globalization.NumberStyles.HexNumber))
        Next
        Return bytes.ToArray
    End Function
    Private Function Bytes2Hex(ByVal Input As Byte()) As String
        Dim Result As New System.Text.StringBuilder(Input.Length * 2)
        Dim Part As String
        For Each b As Byte In Input
            Part = Conversion.Hex(b)
            If Part.Length = 1 Then Part = "0" & Part
            Result.Append(Part)
        Next
        Return Result.ToString()
    End Function
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

opmal7

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Any way to write a double (in hex) into integer values?
« Reply #2 on: July 25, 2016, 02:52:58 PM »
I appreciate the info, but unfortunately this seems a bit over my head.  I have some programming knowledge, but not much in vb.

I don't necessarily need to translate the number to hex before writing it to the PLC.  If I do a .write command, and specify the starting address N1:1, and then the double variable, will it write it to the 4 consecutive integer addresses?  (assuming the .write command works).

For example, in N1:1 I want to write the value of 4A45.  If it is written as an equivalent integer (19013), I assume that would be ok since the PLC sees these two as the same thing, and the Hex/BCB Radix option is only changing the way the data is displayed to me in the data table on the RsLogix program.
« Last Edit: July 25, 2016, 03:39:31 PM by opmal7 »

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Any way to write a double (in hex) into integer values?
« Reply #3 on: July 26, 2016, 10:03:09 AM »
The AB driver doesn't seem to support that feature (unlike Modbus driver which can use prefix F to store float into 2 registers).

To achieve what you want, if you place a textbox on the form then you can use a code similar to this:

Code: [Select]
    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Try
                Dim bytes() As Byte = BitConverter.GetBytes(CDbl(Me.TextBox1.Text))
                Dim strIN(3) As String
                Dim strOUT(3) As String
                For i As Integer = 3 To 0 Step -1
                    For j As Integer = 1 To 0 Step -1 'Swap Bytes
                        strIN(3 - i) &= bytes(j + i * 2).ToString("X")
                    Next
                    strOUT(3 - i) = Convert.ToInt16(strIN(3 - i), 16)
                Next
                Me.SerialDF1forSLCMicroCom1.BeginWrite("N7:1", 4, strOUT)
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try
        End If
    End Sub

Serial driver is used in this example but Ethernet driver should do the same.

Check the attached picture for the output.

It might be possible to do these things in a different way as well.

opmal7

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Any way to write a double (in hex) into integer values?
« Reply #4 on: July 27, 2016, 11:52:41 AM »
Gorda, I really appreciate the info.  I am using the Ethernet driver (not serial), so when I wrote the code I got an error on the "Me.EthernetIPforSLCMicroCom1.BeginWrite...." line. 

"BeginWrite' is not a member of 'AdvancedHMIDrivers.EthernetIPforSLCMicroCom'.

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Any way to write a double (in hex) into integer values?
« Reply #5 on: July 27, 2016, 06:02:05 PM »
I just tried the code in versions 3.99m and 3.99n and it works.

What version of AHMI are you using?