Phillip Smilski

Phillip Smilski

  • 1.5k
  • 229
  • 976

How do I keep a socket open and keep writing to it?

Mar 23 2024 11:10 PM

Here is the example from Keysight, what I am after is starting the socket and leaving it open for 5 IP addresses. The issue I am having is I open and close the socket ever time I write a line and the instrument does not like this. I have slightly overcome this by writing classes that have multiple lines that I can pass vaiables through, but there are still issues. I modified a project years ago that had this built in, but for the life of me I do not know what is is called and my searches are leading me down the wrong path. I should be able to make a class that starts the socket, then write lines to the socket.

////////////////////////////////////////////////////////////////////////////////
// © Keysight Technologies 2016
//
// You have a royalty-free right to use, modify, reproduce and distribute
// the Sample Application Files (and/or any modified version) in any way
// you find useful, provided that you agree that Keysight Technologies has no
// warranty, obligations or liability for any Sample Application Files.
//
////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;

using Ivi.Visa;
using Ivi.Visa.FormattedIO;

namespace Socket_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Change this variable to the address of your instrument
            string VISA_ADDRESS = "Your instrument's VISA address goes here!";

            // Create a connection (session) to the TCP/IP socket on the instrument. 
            // Change VISA_ADDRESS to a SOCKET address, e.g. "TCPIP::169.254.104.59::5025::SOCKET"
            IMessageBasedSession session = GlobalResourceManager.Open(VISA_ADDRESS) as IMessageBasedSession;

            // The first thing you should do with a SOCKET connection is enable the Termination Character. Otherwise all of your read's will fail
            session.TerminationCharacterEnabled = true;

            // We can find out details of the connection
            ITcpipSocketSession socket = session as ITcpipSocketSession;
            Console.WriteLine("IP: {0}\r\nHostname: {1}\r\nPort: {2}\r\n",
                socket.Address,
                socket.HostName,
                socket.Port);

            // Send the *IDN? and read the response as strings
            MessageBasedFormattedIO formattedIO = new MessageBasedFormattedIO(session);
            formattedIO.WriteLine("*IDN?");
            string idnResponse = formattedIO.ReadLine();

            Console.WriteLine("*IDN? returned: {0}", idnResponse);

            // Close the connection to the instrument
            session.Dispose();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}
 


Answers (6)