TCP client

Here is a code sample of a simple TCP client that reads data from the IP voltmeter. It sends the command "volt_1" to the TCP server and the TCP server sends the measured value on the first channel. This way it is possible to read data on all channels.

The transfer involves only a few bytes, making it a highly efficient and speedy communication method.

Code


using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        string server = "192.168.0.60";
        int port = 9760;

        try
        {
            TcpClient client = new TcpClient();
            client.Connect(server, port);
            Console.WriteLine("Connected to the server.");
            NetworkStream stream = client.GetStream();

            while (true)
            {
                try
                {
                    string message = "volt_1";
                    byte[] data = Encoding.ASCII.GetBytes(message);
                    stream.Write(data, 0, data.Length);

                    byte[] buffer = new byte[256];
                    int bytesRead = stream.Read(buffer, 0, buffer.Length);
                    string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                    Console.WriteLine("Voltage: " + response + "V");

                    Thread.Sleep(1000);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error during communication: " + e.Message);
                    break;
                }
            }

            stream.Close();
            client.Close();
            Console.WriteLine("Connection closed.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
    }
}
                        

Commentary

This C# program establishes a connection to a server at IP address 192.168.0.60 on port 9760. Here's a detailed commentary on its functionality:

This code is a simple example and can be further modified and expanded. It serves to create your own application for measurement and automation.