Programming Reference

/// </summary>

public void Disconnect()

{

if (m_Client != null)

m_Client.Close();

}

///<summary>

///Write to a SCPI command to a network socket and return

the

///response if it was a query.

///</summary>

///<param name="s">SCPI command to be written to the socket</param>

///<returns>Response of the instrument or an empty string

///if there is no response.</returns>

public string Send(string s)

{

string response = ""; int i;

//Write the string to the network socket byte[] bufferout = new byte[s.Length + 1]; for (i = 0; i < s.Length; i++)

bufferout[i] = (byte)s[i]; bufferout[s.Length] = (byte)'\n'; m_Stream.Write(bufferout, 0, bufferout.Length);

//If the string containes a '?', then it is a query and

//the response nneds to be read from the instrument

if (s.IndexOf('?') != -1)

{

// Read resonse from network stream

int numRead = m_Stream.Read(bufferin, 0, bufferin.Length);

// if the response contained any bytes, then convert

them

//to a valid string if (numRead > 0)

{

StringBuilder sb = new StringBuilder(); for (i = 0; i < numRead; i++)

sb.Append((char)bufferin[i]);

//make sure that all kind of terminators are

//reported as \r\n

sb.Append("\r\n");

response = sb.ToString().Trim(new char[] { '\r', '\n'

});

if (response.IndexOf("\r\n") != -1) response = response.Replace("\r\n", "\n");

if (response.IndexOf("\r") != -1) response = response.Replace("\r", "\r");

if (response.IndexOf("\n") != -1) response = response.Replace("\n", "\r\n");

}

}

return response;

}

}

}

Communicate with the instrument

Given the class above, the communication with the instrument can be done like this:

using System;

using System.Collections.Generic;

namespace TBertExample

{

static class Program

N5980A User Guide

43

Page 43
Image 43
Agilent Technologies N5980A manual Communicate with the instrument