The following C# code opens a VISA session to a measurement instrument:
// Connect
var vi = GlobalResourceManager.Open("TCPIP::10.0.0.2::hislip0::INSTR") as IMessageBasedSession;
// Set TCP Keep Alive
if (vi.ResourceName.StartsWith("TCPIP"))
{
((INativeVisaSession)vi).SetAttributeBoolean(NativeVisaAttribute.TcpKeepAlive, true);
}
// IDN query
vi.RawIO.Write("*IDN?");
Console.WriteLine(vi.RawIO.ReadString());
// IDN query using SRQ
vi.EnableEvent(EventType.ServiceRequest);
vi.DiscardEvents(EventType.ServiceRequest);
vi.RawIO.Write("*CLS;*ESE 1;*SRE 32");
vi.RawIO.Write("*IDN?;*OPC");
vi.WaitOnEvent(EventType.ServiceRequest, 1000);
vi.ReadStatusByte();
vi.DisableEvent(EventType.ServiceRequest);
Console.WriteLine(vi.RawIO.ReadString());
// Sleep to monitor keep alive traffic using Wireshark
Thread.Sleep(5 * 60 * 60 * 1000);
// IDN query
vi.RawIO.Write("*IDN?");
Console.WriteLine(vi.RawIO.ReadString());
// IDN query using SRQ
vi.EnableEvent(EventType.ServiceRequest);
vi.DiscardEvents(EventType.ServiceRequest);
vi.RawIO.Write("*CLS;*ESE 1;*SRE 32");
vi.RawIO.Write("*IDN?;*OPC");
vi.WaitOnEvent(EventType.ServiceRequest, 1000);
vi.ReadStatusByte();
vi.DisableEvent(EventType.ServiceRequest);
Console.WriteLine(vi.RawIO.ReadString());
// Disconnect
vi.Dispose();
Using Wireshark I can see that:
GlobalResourceManager.Openopens two TCP connections (1: Port 61741 to Port 4880, 2: Port 61742 to 4880)((INativeVisaSession)vi).SetAttributeBoolean(NativeVisaAttribute.TcpKeepAlive, true)sets the keep alive traffic only for the first TCP connection and not for the second one. The second one seems to be used for the event handling.
This is a problem when I've a firewall in between which drops inactive connections after a while (e.g.: 3 hours).
After that the IDN query using SRQ fails. The IDN query is still working.
I have the following questions:
- Is there a way to set the keep alive for both TCP sessions?
- Or is there a way to get the underlaying
Socketconnections? - The default keep alive interval seems to be 2 hours. Can this be changed? This would be possible if I could access the
Socketconnection, see Inconsistent TCP Keep-Alive Frequency.
I'm using NI VISA 19.0, .NET Framework 4.6.2 and Windows 10. In this example I'm using the HiSLIP protocol, but I've a similar behavior using the VXI-11 protocol.
I've test it as well with Rohde & Schwarz VISA 5.12.8 (similar behavior).