-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example updated (cleaner method for checking installed components)
- Loading branch information
Showing
1 changed file
with
57 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,82 @@ | ||
using Ivi.Visa; | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
namespace IviVisaNetSample | ||
{ | ||
static class Program | ||
{ | ||
static void Main(string[] args) | ||
static void Main() | ||
{ | ||
// Get a VISA.NET library version. | ||
Version VisaNetSharedComponentsVersion = typeof(GlobalResourceManager).Assembly.GetName().Version; | ||
Console.WriteLine("VISA.NET Shared Components version {0}.", VisaNetSharedComponentsVersion); | ||
|
||
// Check whether VISA Shared Components is installed before using VISA.NET. | ||
// If access VISA.NET without the visaConfMgr.dll library, an unhandled exception will | ||
// be thrown during termination process due to a bug in the implementation of the | ||
// VISA.NET Shared Components, andthe application will crash. | ||
FileVersionInfo VisaSharedComponentsInfo; | ||
try | ||
{ | ||
// Get an available version of the VISA Shared Components. | ||
VisaSharedComponentsInfo = FileVersionInfo.GetVersionInfo(Path.Combine(Environment.SystemDirectory, "visaConfMgr.dll")); | ||
Console.WriteLine("VISA Shared Components version {0} detected.", VisaSharedComponentsInfo.ProductVersion); | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
Console.WriteLine("VISA implementation compatible with VISA.NET Shared Components {0} not found. Please install corresponding vendor-specific VISA implementation first.", VisaNetSharedComponentsVersion); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
using (var res = GlobalResourceManager.Open("TCPIP:localhost::inst0::INSTR")) | ||
// Connect to the instrument. | ||
using (IVisaSession res = GlobalResourceManager.Open("TCPIP0::localhost::5025::SOCKET", AccessModes.ExclusiveLock, 2000)) | ||
{ | ||
if (res is IMessageBasedFormattedIO session) | ||
if (res is IMessageBasedSession session) | ||
{ | ||
session.WriteLine("*IDN?"); | ||
var idn = session.ReadLine(); | ||
Console.WriteLine("Connected to {0}", idn); | ||
// Ensure termination character is enabled as here in example we use a SOCKET connection. | ||
session.TerminationCharacterEnabled = true; | ||
|
||
// Request information about an instrument. | ||
session.FormattedIO.WriteLine("*IDN?"); | ||
string idn = session.FormattedIO.ReadLine(); | ||
Console.WriteLine("Instrument information: {0}", idn); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Not a message-based session."); | ||
} | ||
} | ||
} | ||
catch (TypeInitializationException ex) | ||
catch (Exception ex) | ||
{ | ||
if (ex.InnerException != null && ex.InnerException is DllNotFoundException) | ||
if (ex is TypeInitializationException && ex.InnerException is DllNotFoundException) | ||
{ | ||
var VisaNetSharedComponentsVersion = typeof(GlobalResourceManager).Assembly.GetName().Version.ToString(); | ||
// VISA Shared Components is not installed. | ||
Console.WriteLine("VISA implementation compatible with VISA.NET Shared Components {0} not found. Please install corresponding vendor-specific VISA implementation first.", VisaNetSharedComponentsVersion); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
} | ||
|
||
// suppress DllNotFoundException exception in Ivi.Visa dispose | ||
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionEventHandler; | ||
} | ||
|
||
// suppress DllNotFoundException exception in Ivi.Visa dispose | ||
private static void UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs e) | ||
{ | ||
var ex = e.ExceptionObject as Exception; | ||
if (ex is DllNotFoundException dllNotFoundException) | ||
{ | ||
// Dead programs tell no lies | ||
Process.GetCurrentProcess().Kill(); | ||
else if (ex is VisaException | ||
&& ex.Message == "No vendor-specific VISA .NET implementation is installed.") | ||
{ | ||
// Vendor-specific VISA.NET implementation is not available. | ||
Console.WriteLine("VISA implementation compatible with VISA.NET Shared Components {0} not found. Please install corresponding vendor-specific VISA implementation first.", VisaNetSharedComponentsVersion); | ||
} | ||
else if (ex is EntryPointNotFoundException) | ||
{ | ||
// Installed VISA Shared Components is not compatible with VISA.NET Shared Components | ||
Console.WriteLine("Installed VISA Shared Components version {0} does not support VISA.NET {1}. Please upgrade VISA implementation.", VisaSharedComponentsInfo.ProductVersion, VisaNetSharedComponentsVersion); | ||
} | ||
else | ||
{ | ||
// Handle remaining errors. | ||
Console.WriteLine("Exception: {0}", ex.Message); | ||
} | ||
} | ||
|
||
throw ex; | ||
Console.ReadKey(); | ||
} | ||
} | ||
} |