login about faq

I have to communicate to a stepper motor controller and I'm using VB 2008 Express. I had thought the interface was going to be USB but the one that showed up has an ethernet interface. I have done some research and found that Winsock is not really used any more for this, with the TCPClient class in System.Net.Sockets being preferred. The example from the stepper OEM uses an outdated Winsock type of dll. Most of my experience is in VB6 and VBA, so can anyone give advice or sample code that would point me in the right direction on how to use TCPClient. Is it better to create a separate class or use it in a module? The functionality is to send short ascii commands to an IP address/port and receive back short ascii responses. I have a working program otherwise that also does serial comms and just need to get this going. Thanks.

asked Apr 09 '10 at 15:27

Mike%20M's gravatar image

Mike M
312


This is a good example that doesn't use any 3rd party libraries.

link

answered Apr 14 '10 at 00:43

Scott%20Whitlock's gravatar image

Scott Whitlock ♦♦
635115

VB6 and VBA are quite different from VB.Net as far as organization goes. There are no 'modules' per say, so you'll need to use a separate class.

The example that Scott provided is a good start, but there are some optimizations that can be made:

  • Use this as part of your own Class Library.

  • TcpClient and NetworkStream objects are encapsulated in Using statements so they are properly closed and disposed when they leave the program scope.

  • IDisposible is implemented and garbage collection is properly taken care of.

note: this is a quick adaptation from my own code base, so this is in C# and may contain a few typos


VB Usage:

Dim TcpComm as New TcpClientCommClass("127.0.0.1", 80)
Dim tx as string
Dim rx as string

tx = "my message"
rx = TcpComm.Communicate(tx)

C# Usage:

TcpClientCommClass TcpComm = new TcpClientCommClass("127.0.0.1", 80);
string tx = "my message";
string rx = TcpComm.Communicate(tx);

using System;
using System.Net.Sockets;   // for the TcpClient objects
using System.Text;          // for ASCII <-> Byte functions
using System.Diagnostics;   // for debug methods (print, assert)

namespace TcpClientWorkbench
{
    class TcpClientCommClass : IDisposable
    {
        TcpClient client;
        NetworkStream netStream;

        public string Host { get; set; }
        public int Port { get; set; }

        // Instantiate the class using the specified host and port.
        public TcpClientCommClass(string host, int port)
        {
            this.Host = host;
            this.Port = port;
        }

        // Instantiate the class defaulting to the localhost.
        public TcpClientCommClass()
        {
            this.Host = "127.0.0.1";
            this.Port = 80;
        }

        public string Communicate(string message)
        {
            try
            {
                using (client = new TcpClient(Host, Port))
                {
                    using (netStream = client.GetStream())
                    {
                        if ((netStream.CanRead == true) && (netStream.CanWrite))
                        {
                            // Simple transmit example
                            byte[] transmitBuffer = Encoding.ASCII.GetBytes(message);
                            netStream.Write(transmitBuffer, 0, transmitBuffer.Length);

                            // Simple receive example
                            byte[] receiveBuffer = new byte[client.ReceiveBufferSize];
                            netStream.Read(receiveBuffer, 0, client.ReceiveBufferSize);

                            Debug.Print(Encoding.ASCII.GetString(receiveBuffer));
                            return Encoding.ASCII.GetString(receiveBuffer);
                        }
                        else
                        {
                            if (netStream.CanRead == false)
                            {
                                Debug.Print("Can not read from stream");
                            }

                            if (netStream.CanWrite == false)
                            {
                                Debug.Print("Can not write to stream");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.ToString());
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~TcpClientCommClass() 
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing) 
            {


                if (netStream != null)
                {
                    netStream.Close();
                    netStream.Dispose();
                }

                if (client != null)
                {
                    client.Close();
                    client = null;
                }
            }            
        }
    }
}

If your looking for input on Visual Basic, C#, or pretty much any other programming language outside the scope of ladder logic and function blocks, I'd try my luck on Stackoverflow.com first.

link

answered Jun 18 '10 at 16:22

Greg%20Buehler's gravatar image

Greg Buehler
1066

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×7
×2
×1
×1

Asked: Apr 09 '10 at 15:27

Seen: 1,439 times

Last updated: Jun 18 '10 at 16:22