vb.netplcfins

Sending FINS Commands from VB.net to PLC


I have a simple vb.net program that i'm trying to send a FINS Command and receive the response using UDP. I've been using the following question as a reference point FINS Commands C#, but believe I've got something wrong with my Packet, or perhaps I just don't have something quite right.

My current program has 4 text boxes (TBIP, TBPORT, TBSEND, TBReceive). When I run the program, I type in the IP, Port, Packet info to send, and click a button. What i'd like to have happen is the received information sesnt to TBReceive.

My Form Load code looks like this:

Dim publisher As New Sockets.UdpClient(0)
Dim Subsriber As New Sockets.UdpClient(9600)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Subsriber.Client.ReceiveTimeout = 5000
        Subsriber.Client.Blocking = False


    End Sub

My Button Click:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        publisher.Connect(TBIP.Text, TBPort.Text)
        Dim Sendbytes() As Byte = ASCII.GetBytes(TBSend.Text)
        publisher.Send(Sendbytes, Sendbytes.Length)

        Subsriber.Client.Blocking = True


        Timer1.Start()

    End Sub

My Timer:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
            Dim rcvbytes As [Byte]() = Subsriber.Receive(ep)
            Dim returndata As String = ASCII.GetString(rcvbytes)

            TBReceive.Text = returndata.ToString

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

On the Omron PLC, I'm trying to read CIO 100. My ip address is 10.0.1.91 so my string is below. (5B is the last octet):

80 00 02 01 02 00 01 5B 00 12 01 01 00 00 12 34

I have added spaces above between the HEX bits for easy reading.

Could the first problem be i'm converting to or trying to send the info as ASCII? Any help would be appreciated.


Solution

  • Hex is for humans, your PLC is going to want binary data. You'll need to convert between the "800002010200015B0012010100001234" hex string you want to enter in the TextBox and the binary bytes that the PLC likes. Sample code you can use:

    Module HexConversions
        Public Function HexToBinary(hex As String) As Byte()
            hex = hex.Replace(" ", "")
            If hex.Length mod 2 <> 0 then Throw New FormatException 
            Dim bytes = hex.Length \ 2 - 1
            Dim bin(bytes) As Byte
            For ix As Integer = 0 to bytes
                bin(ix) = Byte.Parse(hex.Substring(ix * 2, 2), Globalization.NumberStyles.HexNumber)
            Next
            Return bin
        End Function
    
        Public Function BinaryToHex(bytes() As Byte, Optional usespace As Boolean = False) As String
            Dim hex = BitConverter.ToString(bytes)
            Return hex.Replace("-", IIf(usespace, " ", ""))
        End Function
    End Module
    

    Which you'd use in your existing code like:

        Try         
            Dim Sendbytes() As Byte = HexConverter.HexToBinary(TBSend.Text)
            publisher.Send(Sendbytes, Sendbytes.Length)
        Catch ex As FormatException
            MessageBox.Show("Please enter a valid hex string")
            TBSend.Focus()
            TBSend.SelectAll()
        End Try
    

    and

        Dim rcvbytes() As Byte = Subsriber.Receive(ep)
        TBReceive.Text = HexConverter.BinaryToHex(rcvbytes, True)