// Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
namespace OpenTap
{
///
/// Error codes from the IScpiIO methods
///
public enum ScpiIOResult
{
///
/// Indicates successful completion.
///
Success,
///
/// Indicates successful completion because the requested amount of bytes were read/written.
///
Success_MaxCount,
///
/// Indicates successful completion because a terminating character was found.
///
Success_TermChar,
///
/// General unspecified error.
///
Error_General,
///
/// The call failed due to another client having a lock on the instrument.
///
Error_ResourceLocked,
///
/// Indicates that the call timed out.
///
Error_Timeout,
///
/// Indicates that the connection to the instrument was lost.
///
Error_ConnectionLost,
///
/// The resource could not be found.
///
Error_ResourceNotFound
}
///
/// Types of VISA locks.
///
public enum ScpiLockType
{
///
/// Indicates that the client should get an exclusive lock.
///
Exclusive,
///
/// Indicates that a client should get a shared lock.
///
Shared
}
///
/// Represents low-level IO primitives for a given SCPI instrument.
///
public interface IScpiIO
{
///
/// Clears the SCPI state, including any errors in the error queue.
///
ScpiIOResult DeviceClear();
///
/// Reads the status byte of the instrument.
///
/// The current status byte.
ScpiIOResult ReadSTB(ref byte stb);
///
/// Reads a number of bytes from the instrument.
///
/// The target buffer to read to.
/// The number of bytes to read.
/// This will indicate whether an EOI indicator was received.
/// The number of bytes that was actually read if succesful.
ScpiIOResult Read(ArraySegment buffer, int count, ref bool eoi, ref int read);
///
/// Writes a number of bytes to the instrument.
///
/// The returned error code will indicate whether the EOI was sent.
/// The buffer to write from.
/// The number of bytes to read.
/// The number of bytes that was actually written if succesful.
ScpiIOResult Write(ArraySegment buffer, int count, ref int written);
///
/// Try to acquire a lock on the instrument.
///
/// Indicates which kind of lock should be acquired.
/// In case the type of lock is
ScpiIOResult Lock(ScpiLockType lockType, string sharedKey = null);
///
/// Unlock an instrument.
///
ScpiIOResult Unlock();
///
/// Indicates whether a should generate an end-of-message indicator when writing its last byte.
///
bool SendEnd { get; set; }
///
/// Indicates the timeout in milliseconds for any of the IO operations.
///
int IOTimeoutMS { get; set; }
///
/// Indicates the timeout in milliseconds for acquiring a lock.
///
int LockTimeoutMS { get; set; }
///
/// Sets the termination character, if any.
///
byte TerminationCharacter { get; set; }
///
/// Controls whether the IO operations should use a termination character.
///
bool UseTerminationCharacter { get; set; }
///
/// Returns the resource class of the connected instrument.
///
string ResourceClass { get; }
}
///
/// Represents low-level IO primitives for a given SCPI instrument.
///
public interface IScpiIO2 : IScpiIO
{
/// Opens a connection to the instrument.
/// Visa address of the instrument.
/// if the instrument should be locked when the connection is established.
/// Visa error code.
ScpiIOResult Open(string visaAddress, bool @lock);
/// Close the connection to the instrument.
/// Visa Error code.
ScpiIOResult Close();
/// Resource ID attached to this instrument.
int ID { get; }
/// Callback from SRQ instrument events.
event ScpiIOSrqDelegate SRQ;
/// Open SRQ Callback handling.
void OpenSRQ();
/// Close SRQ Callback handling.
void CloseSRQ();
}
///
/// Events for scpi.
///
public enum ScpiEvent
{
/// All enabled events.
EVENT_ALL_ENABLED_EVENTS = Visa.VI_ALL_ENABLED_EVENTS,
/// Notification that an asynchronous operation has completed.
EVENT_IO_COMPLETION = Visa.VI_EVENT_IO_COMPLETION,
/// Notification that a hardware trigger was received from a device.
EVENT_TRIG = Visa.VI_EVENT_TRIG,
/// Notification that a device is requesting service
EVENT_SERVICE_REQ = Visa.VI_EVENT_SERVICE_REQ,
/// Notification that the local controller has been sent a device clear message
EVENT_CLEAR = Visa.VI_EVENT_CLEAR,
/// Notification that an error condition has occurred during an operation invocation. (Note: the VI_QUEUE and VI_SUSPEND_HNDLR mechanisms cannot be used with this event.)
EVENT_EXCEPTION = Visa.VI_EVENT_EXCEPTION,
/// Notification that the GPIB controller has gained or lost CIC (controller in charge) status.
EVENT_GPIB_CIC = Visa.VI_EVENT_GPIB_CIC,
/// Notification that the GPIB controller has been addressed to talk.
EVENT_GPIB_TALK = Visa.VI_EVENT_GPIB_TALK,
/// Notification that the GPIB controller has been addressed to listen.
EVENT_GPIB_LISTEN = Visa.VI_EVENT_GPIB_LISTEN,
/// Notification that a vendor-specific PXI interrupt was received from the device.
EVENT_PXI_INTR = Visa.VI_EVENT_PXI_INTR,
/// Notification that the VXI/VME SYSFAIL* line has been asserted.
EVENT_VXI_VME_SYSFAIL = Visa.VI_EVENT_VXI_VME_SYSFAIL,
/// Notification that the VXI/VME SYSRESET* line has been asserted
EVENT_VXI_VME_SYSRESET = Visa.VI_EVENT_VXI_VME_SYSRESET,
/// Notification that a VXI signal or VXI interrupt has been received from a device.
EVENT_VXI_SIGP = Visa.VI_EVENT_VXI_SIGP,
/// Notification that a VXIbus interrupt was received from the device.
EVENT_VXI_VME_INTR = Visa.VI_EVENT_VXI_VME_INTR,
}
/// Mechanisms for events.
[Flags]
public enum ScpiEventMechanism
{
/// Queue event.
QUEUE = Visa.VI_QUEUE,
/// Event handler.
HNDLR = Visa.VI_HNDLR,
}
///
/// Represents low-level IO primitives for a given SCPI instrument. IScpiIO3 is the full interface needed to specify a VISA connection.
///
public interface IScpiIO3 : IScpiIO2
{
///
/// Enables an event.
///
public ScpiIOResult EnableEvent(ScpiEvent eventType, ScpiEventMechanism mechanism);
///
/// Disables an event.
///
public ScpiIOResult DisableEvent(ScpiEvent eventType, ScpiEventMechanism mechanism);
///
/// Waits on an event.
///
public ScpiIOResult WaitOnEvent(ScpiEvent eventType, int timeout, out ScpiEvent outEventType);
}
/// SCPI IO SRQ Event Delegate
///
public delegate void ScpiIOSrqDelegate(IScpiIO2 sender);
///
/// Represents a connection to talk to any SCPI-enabled instrument.
///
public interface IScpiInstrument : IInstrument
{
///
/// Get access to the low-level primitives of the connection.
///
IScpiIO IO { get; }
///
/// Sends a SCPI command to the instrument.
///
/// The command to send.
/// Non-blocking.
void ScpiCommand(string command);
///
/// Sends a SCPI query to the instrument and waits for a response.
///
/// The SCPI query to send.
/// True to suppress log messages.
/// The response from the instrument.
string ScpiQuery(string query, bool isSilent = false);
///
/// Sends a IEEE Block SCPI command to the instrument.
///
void ScpiIEEEBlockCommand(string command, byte[] data);
///
/// Sends a IEEE Block SCPI query to the instrument and waits for a response. The response is assumed to be IEEE block data.
///
/// The SCPI query to send.
/// The response from the instrument.
byte[] ScpiQueryBlock(string query);
}
}