View Single Post
 
Old 05-06-2014, 11:30 PM
layferd layferd is offline
Newcomer
 
Join Date: May 2014
Posts: 1
Question receive data from arduino via ethernet

Hi all,
I have a problem here,i want to receive data from arduino with net socket in vb,and show the data in textbox,I just code it,but it doesn't work,can anyone help me?
I appreciate any help
[CODE][Imports System.Net.Sockets
Imports System.Text


Public Class Form2
Dim tcpClient As New System.Net.Sockets.TcpClient()
Dim networkStream As NetworkStream
Dim KeyPressed As Integer

Private Function Arduino_Connect(ByVal IP As String, ByVal Port As Integer) As Boolean
tcpClient.Connect(IP, Port)
networkStream = tcpClient.GetStream()
If Not networkStream.CanWrite Or Not networkStream.CanRead Then
tcpClient.Close()
networkStream = Nothing
Return False
End If
Return True
End Function
Private Sub Arduino_Write(ByVal Output As String)
If Not IsNothing(networkStream) Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(Output)
Dim endByte As [Byte]() = {&HFE}
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Write(endByte, 0, 1)
Else
MsgBox("ERROR")
End If
End Sub
Private Sub Arduino_Disconnect()
If Not IsNothing(networkStream) Then
tcpClient.Close()
networkStream = Nothing
End If
End Sub
Private Sub Form2_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Arduino_Connect("192.168.1.8", 12)

End Sub

Public Shared Function SendReceiveTest1(ByVal server As Socket) As Integer
Dim msg As Byte() = Encoding.UTF8.GetBytes("This is a test")
Dim bytes(255) As Byte
Try
' Blocks until send returns.
Dim i As Integer = server.Send(msg)
Console.WriteLine("Sent {0} bytes.", i)

' Get reply from the server.
i = server.Receive(bytes)
Console.WriteLine(Encoding.UTF8.GetString(bytes))
Catch e As SocketException
Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
Return e.ErrorCode
End Try
Return 0

End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Arduino_Write("E")
Dim a As Integer
a = networkStream.ReadByte
TextBox1 = a

End Sub
End Class]
Reply With Quote