0

I am trying to communicate with an Arduino using an ethernet wired connection. I did that using python and C but now m getting difficulty doing the same with VB.net. Simply I am trying to send a UDP packet to Arduino using its IP address and port number and want the response to get back and show it in the textbox.

Imports System.Net
Imports System.Environment, System.Text.Encoding


Public Class udpWindow
 Dim localIP As String = Dns.GetHostByName(Dns.GetHostName()).AddressList(0).ToString
 Dim localPort As Integer = 5000

 Dim publisher As New Sockets.UdpClient(0)
 Dim subscriber As New Sockets.UdpClient(localPort)

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    LocalIP_txt.Text = localIP
    localPort_txt.Text = localPort

 End Sub


 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
    publisher.Connect(ServerIP.Text, ServerPort.Text)
    Dim sendbytes() As Byte = ASCII.GetBytes("0")
    publisher.Send(sendbytes, sendbytes.Length)

 End Sub

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    subscriber.Client.ReceiveTimeout = 100
    subscriber.Client.Blocking = False
 End Sub


 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Try

        Dim ep As IPEndPoint = New IPEndPoint(0, localPort)
        Dim recvbytes() As Byte = subscriber.Receive(ep)
        'MsgBox("msg recieved")
        rcvMsgs.Text = ASCII.GetString(recvbytes)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

 End Sub
End Class

Can anyone help me with a better alternative or with the correction? Thanx in advance as this is my first query over here so pardon for any improper mistake.

3
  • You are putting subscriber's socket into non-blocking mode. The Receive() method does not work in non-blocking mode. The error you are getting is basically telling you that the socket returned a WSAEWOULDBLOCK error when there is no data available to read. Use the BeginReceive() or ReceiveAsync() method instead for non-blocking reads, and let the socket notify you when data is available, don't poll for it Commented Apr 19, 2021 at 17:30
  • Otherwise, if you want to keep polling, then just get rid of the non-blocking mode, and check subscriber.Available > 0 before calling subscriber.Receive(). Or, since you are using a timeout anyway, just have your try..catch ignore any SocketException whose ErrorCode is WSAETIMEDOUT (10060). Or, get rid of the timer and use a separate thread that calls Receive() in a loop without a timeout. Commented Apr 19, 2021 at 17:41
  • @RemyLebeau really wants to thank you for ur support. This was my first query on StackOverflow and got that sorted within a day m happy with my experience. Commented Apr 20, 2021 at 3:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.