-1

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.

Community
  • 1
  • 1
Shmewnix
  • 1,553
  • 10
  • 32
  • 66
  • There is no possible scenario where ASCII.GetBytes(TBSend.Text) can produce those byte values. Very unclear what you are doing. – Hans Passant Jun 16 '14 at 14:31
  • I'm trying to send the above hex values as a string using UDP to a PLC. So basically I need to send `800002010200015B0012010100001234` as a HEX string as a UDP Packet, then receive the hex string on the response. I only know how to send using ASCII, which I assume does nothing for me since I need to send it as a HEX string.. – Shmewnix Jun 16 '14 at 14:40

1 Answers1

3

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)
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • It throws an InvalidOperationException: `An error occurred creating the form. See Exception.InnerException for details. The error is: Only one usage of each socket address (protocol/network address/port) is normally permitted` – Shmewnix Jun 16 '14 at 18:42
  • That of course doesn't have anything to do with the code I posted, it doesn't use a socket. Ask another question about it. After you looked at Google hits first, it is a very common mistake. – Hans Passant Jun 16 '14 at 18:46
  • This isn't quite right still. The command sends, but i'm guessing the PLC doesn't understand it because there is no response, not even an attempted response... – Shmewnix Jun 18 '14 at 13:51