Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

i am trying to control an instrument with visual basic via TCP/IP protocol. So far we are able to send the commands to the instrument but not able to receive the data sent by the instrument. can someone please guide me how to do that? Below is the code we are using:
VB
Imports System.Net.Sockets
Imports System.IO

Public Class Form1
    Private Client As TCPControl

    Private Sub cmdConnect_Click(sender As Object, e As EventArgs) Handles cmdConnect.Click
        Client = New TCPControl("169.254.164.61", 3602)
        If Client.Client.Connected Then cmdConnect.Text = "Connected"
    End Sub

    Private Sub cmdSend_Click(sender As Object, e As EventArgs) Handles cmdSend.Click
        SendMessage()
        txtMessage.Clear()
        txtMessage.Focus()
    End Sub

    Private Sub SendMessage()
        If Client.Client.Connected = True Then Client.Send(txtMessage.Text)
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If Client.Client.Connected = True Then
            Client.Client.Close()
        End If
    End Sub

End Class


What I have tried:

We tried lot of online tutorials but no success so far.
Posted
Updated 31-Jul-17 4:25am
v2

Here are some examples, TcpClient is probably the simplest option:

TcpClient Class (System.Net.Sockets)[^]

Windows TCP Communication in VB.NET sample in VB.NET, Windows Shell Script for Visual Studio 2008[^]

TCP/IP with VB.NET[^]

Also make sure you use the right Encoding, here is an example:
VB.NET TCP Client/Server Socket Commmunications[^]

VB
' Convert String to Byte array.
Dim mystring As String = "my string data"
Dim myarray() As Byte = System.Text.Encoding.ASCII.GetBytes(mystring)
 
Share this answer
 
v3
Comments
Member 8325258 24-Jun-17 5:19am    
Thank you RickZeeland for your reply.But i want a client side code which will send and receive data in string form not in bytes.

VB.NET TCP Client/Server Socket Commmunications[^]
This hasn't helped me much.

I am looking for a code which will receive data, as my client is sending data correctly but i don't know how to receive it. Can you please help me in this.
Here is a very simple (but working) class demonstrating how to use the basic features of the TcpClient class. Received byte stream is converted to an ASCII string, you can adapt it easily if you need a different string encoding (e.g. UTF8).


Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text


Public Class MyClient
	
	'exported methods:
'  public bool Connect(string server, int port)
'      public bool IsConnected()
'    public void Send(string msg)
'    public string Read()
'    public bool DataAvailable()
	

	Public client As TcpClient

    
	Private stream As NetworkStream
	Private data As String

	'constructor
	Public Sub New()

		data = ""
	End Sub


	Public Function Connect(server As String, port As Integer) As Boolean


		Try
			client = New TcpClient(server, port)
			stream = client.GetStream()
			Return True
		Catch
			Return False
		End Try


	End Function

	Public Function IsConnected() As Boolean


		If Not client.Connected Then
			Return False
		Else
			Return (Not (client.Client.Poll(1, SelectMode.SelectRead) AndAlso (client.Available = 0)))
		End If


	End Function

	Public Sub Send(msg As String)
		Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(msg)
		stream.Write(bytes, 0, bytes.Length)

	End Sub


	Public Function Read() As String

		GetData()

		If data = "" Then
			Return ""
		Else
			Dim s As String = data
			data = ""
			Return s
		End If

	End Function

	Public Function DataAvailable() As Boolean
		Return stream.DataAvailable AndAlso IsConnected()

	End Function

	Private Sub GetData()
		If Not (client.Connected AndAlso stream.DataAvailable) Then
			Return
		End If


		' Buffer for reading data
		Dim bytes As Byte() = New [Byte](2047) {}
		' Loop to receive all the data sent by the client.

		Dim i As Integer

		While client.Connected AndAlso stream.DataAvailable


			i = stream.Read(bytes, 0, bytes.Length)

			data += System.Text.Encoding.ASCII.GetString(bytes, 0, i)
		End While
	End Sub



End Class
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900