VB .NET 2019 and NI-VISA Example Needed - Page 1
Forum section: electronics / general

author: NY2KW
author_rank: Regular Contributor
timestamp: April 10, 2020, 03:12:21 am
content: I am teaching myself VS 2019 Community with VB .NET using NI-VISA to communicate with a Siglent device. All of the examples I can find are in C# or old VB6. I am having difficultty with the NI-VISA ResourceManager class. I have found older examples that useCode:Public Class Form1Dim result As StringDim dut_tcpip As TcpipSessionDim visa_string As StringPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Clickvisa_string = TextBox1.Textdut_tcpip = ResourceManager.GetLocalManager.Open(visa_string)visa_string = TextBox1.Textdut_write("*IDN?")dut_read()MsgBox(result)End SubThe problem is the latest version of NI-VISA ResourceManager does not list GetLocalManager. I have searched through current and previous NI-VISA documents and cannot find how this changed or to what. Can someone help me get past this first door?Thank you in advance and stay safe and healthy

author: BroMarduk
author_rank: Unknown
timestamp: April 10, 2020, 03:27:15 am
content: Just a quick check...do you have:Code:Imports NationalInstruments.VisaNSat the top of the file?Then tryCode:Public Class Form1Dim mbSession As MessageBasedSessionDim visa_string As StringPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Clickvisa_string = TextBox1.TextmbSession = CType(ResourceManager.GetLocalManager().Open(visa_string ), MessageBasedSession)Dim responseString As String = mbSession.Query("*IDN?")MsgBox(responseString)mbSession.Dispose()End SubFrom the documentation here:https://www.ni.com/en-us/support/documentation/supplemental/17/archived--c--and-visual-basic--net-instrument-control.htmlYou can then use the mbSession object to transfer data and get the response.** NOTE I realized this is from the old library....Let me see if I can find the new one **

author: NY2KW
author_rank: Regular Contributor
timestamp: April 10, 2020, 03:37:50 am
content: Quote from: BroMarduk on April 10, 2020, 03:27:15 amThanks for the reply. VisaNS is no longer in the current NI-VISA download. I believe the VisaNS version had 'GetLocalManager' under ResourceManager. I have added the new NI version under References in Visual Studio and in the code I have:Code:Imports NationalInstruments.VisaImports Ivi.VisaI cannot find any examples in VB .NET with the new version of NI-VISA Resource Manager... I have Googled literally for a few hours looking

author: BroMarduk
author_rank: Unknown
timestamp: April 10, 2020, 03:40:46 am
content: LOL... I caught that a bit late.Let me see...

author: BroMarduk
author_rank: Unknown
timestamp: April 10, 2020, 03:41:24 am
content: Do you have a C# example I can translate?

author: NY2KW
author_rank: Regular Contributor
timestamp: April 10, 2020, 03:50:34 am
content: First, Many many thanks for your kind help. Yes, I think I can find a C example from NI-VISA. Gi8ve me a few minutes. In the meantime I did your code:Code:Imports NationalInstruments.VisaImports Ivi.VisaPublic Class Form1Dim mbSession As MessageBasedSessionDim visa_string As StringPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Clickvisa_string = TextBox1.TextmbSession = CType(ResourceManager.GetLocalManager().Open(visa_string), MessageBasedSession)Dim responseString As String = mbSession.Query("*IDN?")MsgBox(responseString)mbSession.Dispose()End SubEnd ClassI get errors that GetLocalManager is not a member of ResourceManager and that Query is not a member of mbSession.Let me get that C example

author: NY2KW
author_rank: Regular Contributor
timestamp: April 10, 2020, 03:58:23 am
content: Here is a read/write example from NI-VISA with C NET. Its probably a lot more than you needCode:using System;using System.Windows.Forms;using NationalInstruments.Visa;namespace NationalInstruments.Examples.SimpleReadWrite{/// <summary>/// Summary description for Form1./// </summary>public class MainForm : System.Windows.Forms.Form{private MessageBasedSession mbSession;private string lastResourceString = null;private System.Windows.Forms.TextBox writeTextBox;private System.Windows.Forms.TextBox readTextBox;private System.Windows.Forms.Button queryButton;private System.Windows.Forms.Button writeButton;private System.Windows.Forms.Button readButton;private System.Windows.Forms.Button openSessionButton;private System.Windows.Forms.Button clearButton;private System.Windows.Forms.Button closeSessionButton;private System.Windows.Forms.Label stringToWriteLabel;private System.Windows.Forms.Label stringToReadLabel;/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.Container components = null;public MainForm(){//// Required for Windows Form Designer support//InitializeComponent();SetupControlState(false);}/// <summary>/// Clean up any resources being used./// </summary>protected override void Dispose( bool disposing ){if( disposing ){if(mbSession != null){mbSession.Dispose();}if (components != null){components.Dispose();}}base.Dispose( disposing );}#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));this.queryButton = new System.Windows.Forms.Button();this.writeButton = new System.Windows.Forms.Button();this.readButton = new System.Windows.Forms.Button();this.openSessionButton = new System.Windows.Forms.Button();this.writeTextBox = new System.Windows.Forms.TextBox();this.readTextBox = new System.Windows.Forms.TextBox();this.clearButton = new System.Windows.Forms.Button();this.closeSessionButton = new System.Windows.Forms.Button();this.stringToWriteLabel = new System.Windows.Forms.Label();this.stringToReadLabel = new System.Windows.Forms.Label();this.SuspendLayout();//// queryButton//this.queryButton.Location = new System.Drawing.Point(5, 83);this.queryButton.Name = "queryButton";this.queryButton.Size = new System.Drawing.Size(74, 23);this.queryButton.TabIndex = 3;this.queryButton.Text = "Query";this.queryButton.Click += new System.EventHandler(this.query_Click);//// writeButton//this.writeButton.Location = new System.Drawing.Point(79, 83);this.writeButton.Name = "writeButton";this.writeButton.Size = new System.Drawing.Size(74, 23);this.writeButton.TabIndex = 4;this.writeButton.Text = "Write";this.writeButton.Click += new System.EventHandler(this.write_Click);//// readButton//this.readButton.Location = new System.Drawing.Point(153, 83);this.readButton.Name = "readButton";this.readButton.Size = new System.Drawing.Size(74, 23);this.readButton.TabIndex = 5;this.readButton.Text = "Read";this.readButton.Click += new System.EventHandler(this.read_Click);//// openSessionButton//this.openSessionButton.Location = new System.Drawing.Point(5, 5);this.openSessionButton.Name = "openSessionButton";this.openSessionButton.Size = new System.Drawing.Size(92, 22);this.openSessionButton.TabIndex = 0;this.openSessionButton.Text = "Open Session";this.openSessionButton.Click += new System.EventHandler(this.openSession_Click);//// writeTextBox//this.writeTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)| System.Windows.Forms.AnchorStyles.Right)));this.writeTextBox.Location = new System.Drawing.Point(5, 54);this.writeTextBox.Name = "writeTextBox";this.writeTextBox.Size = new System.Drawing.Size(275, 20);this.writeTextBox.TabIndex = 2;this.writeTextBox.Text = "*IDN?\\n";//// readTextBox//this.readTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)| System.Windows.Forms.AnchorStyles.Left)| System.Windows.Forms.AnchorStyles.Right)));this.readTextBox.Location = new System.Drawing.Point(5, 136);this.readTextBox.Multiline = true;this.readTextBox.Name = "readTextBox";this.readTextBox.ReadOnly = true;this.readTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;this.readTextBox.Size = new System.Drawing.Size(275, 119);this.readTextBox.TabIndex = 6;this.readTextBox.TabStop = false;//// clearButton//this.clearButton.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)| System.Windows.Forms.AnchorStyles.Right)));this.clearButton.Location = new System.Drawing.Point(6, 257);this.clearButton.Name = "clearButton";this.clearButton.Size = new System.Drawing.Size(275, 24);this.clearButton.TabIndex = 7;this.clearButton.Text = "Clear";this.clearButton.Click += new System.EventHandler(this.clear_Click);//// closeSessionButton//this.closeSessionButton.Location = new System.Drawing.Point(97, 5);this.closeSessionButton.Name = "closeSessionButton";this.closeSessionButton.Size = new System.Drawing.Size(92, 22);this.closeSessionButton.TabIndex = 1;this.closeSessionButton.Text = "Close Session";this.closeSessionButton.Click += new System.EventHandler(this.closeSession_Click);//// stringToWriteLabel//this.stringToWriteLabel.Location = new System.Drawing.Point(5, 34);this.stringToWriteLabel.Name = "stringToWriteLabel";this.stringToWriteLabel.Size = new System.Drawing.Size(91, 14);this.stringToWriteLabel.TabIndex = 8;this.stringToWriteLabel.Text = "String to Write:";//// stringToReadLabel//this.stringToReadLabel.Location = new System.Drawing.Point(5, 117);this.stringToReadLabel.Name = "stringToReadLabel";this.stringToReadLabel.Size = new System.Drawing.Size(101, 15);this.stringToReadLabel.TabIndex = 9;this.stringToReadLabel.Text = "String Read:";//// MainForm//this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);this.ClientSize = new System.Drawing.Size(287, 289);this.Controls.Add(this.stringToReadLabel);this.Controls.Add(this.stringToWriteLabel);this.Controls.Add(this.closeSessionButton);this.Controls.Add(this.clearButton);this.Controls.Add(this.readTextBox);this.Controls.Add(this.writeTextBox);this.Controls.Add(this.openSessionButton);this.Controls.Add(this.readButton);this.Controls.Add(this.writeButton);this.Controls.Add(this.queryButton);this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));this.MaximizeBox = false;this.MinimumSize = new System.Drawing.Size(295, 316);this.Name = "MainForm";this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;this.Text = "Simple Read/Write";this.ResumeLayout(false);this.PerformLayout();}#endregion/// <summary>/// The main entry point for the application./// </summary>static void Main(){Application.Run(new MainForm());}private void openSession_Click(object sender, System.EventArgs e){using (SelectResource sr = new SelectResource()){if(lastResourceString != null){sr.ResourceName = lastResourceString;}DialogResult result = sr.ShowDialog(this);if(result == DialogResult.OK){lastResourceString = sr.ResourceName;Cursor.Current = Cursors.WaitCursor;using (var rmSession = new ResourceManager()){try{mbSession = (MessageBasedSession)rmSession.Open(sr.ResourceName);SetupControlState(true);}catch (InvalidCastException){MessageBox.Show("Resource selected must be a message-based session");}catch (Exception exp){MessageBox.Show(exp.Message);}finally{Cursor.Current = Cursors.Default;}}}}}private void closeSession_Click(object sender, System.EventArgs e){SetupControlState(false);mbSession.Dispose();}private void query_Click(object sender, System.EventArgs e){readTextBox.Text = String.Empty;Cursor.Current = Cursors.WaitCursor;try{string textToWrite = ReplaceCommonEscapeSequences(writeTextBox.Text);mbSession.RawIO.Write(textToWrite);readTextBox.Text = InsertCommonEscapeSequences(mbSession.RawIO.ReadString());}catch(Exception exp){MessageBox.Show(exp.Message);}finally{Cursor.Current = Cursors.Default;}}private void write_Click(object sender, System.EventArgs e){try{string textToWrite = ReplaceCommonEscapeSequences(writeTextBox.Text);mbSession.RawIO.Write(textToWrite);}catch(Exception exp){MessageBox.Show(exp.Message);}}private void read_Click(object sender, System.EventArgs e){readTextBox.Text = String.Empty;Cursor.Current = Cursors.WaitCursor;try{readTextBox.Text = InsertCommonEscapeSequences(mbSession.RawIO.ReadString());}catch(Exception exp){MessageBox.Show(exp.Message);}finally{Cursor.Current = Cursors.Default;}}private void clear_Click(object sender, System.EventArgs e){readTextBox.Text = String.Empty;}private void SetupControlState(bool isSessionOpen){openSessionButton.Enabled = !isSessionOpen;closeSessionButton.Enabled = isSessionOpen;queryButton.Enabled = isSessionOpen;writeButton.Enabled = isSessionOpen;readButton.Enabled = isSessionOpen;writeTextBox.Enabled = isSessionOpen;clearButton.Enabled = isSessionOpen;if(isSessionOpen){readTextBox.Text = String.Empty;writeTextBox.Focus();}}private string ReplaceCommonEscapeSequences(string s){return s.Replace("\\n", "\n").Replace("\\r", "\r");}private string InsertCommonEscapeSequences(string s){return s.Replace("\n", "\\n").Replace("\r", "\\r");}}}

author: BroMarduk
author_rank: Unknown
timestamp: April 10, 2020, 04:05:08 am
content: Try it without the GetLocalManager.:Code:Imports NationalInstruments.VisaImports Ivi.VisaPublic Class Form1Dim result As StringDim dut_tcpip As TcpipSessionDim visa_string As StringPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Clickvisa_string = TextBox1.Textdut_tcpip = ResourceManager.Open(visa_string)dut_tcpip.RawIO.Write("*IDN?" + vbLf)MsgBox(dut_tcpip.RawIO.ReadString())End Sub

author: BroMarduk
author_rank: Unknown
timestamp: April 10, 2020, 04:08:31 am
content: The new line character may be wrong...You can try it without the + VbLf as well.

author: NY2KW
author_rank: Regular Contributor
timestamp: April 10, 2020, 04:21:21 am
content: I tried it and getting an error for line:Code:dut_tcpip = ResourceManager.GetLocalManager.Open(visa_string)Error says "Reference to a non-shared member requires an object reference"

author: BroMarduk
author_rank: Unknown
timestamp: April 10, 2020, 05:07:09 am
content: Looks like you have to create an instance of ResourceManager class:Code:Imports NationalInstruments.VisaImports Ivi.VisaPublic Class Form1Dim result As StringDim dut_tcpip As TcpipSessionDim visa_string As StringPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Clickvisa_string = TextBox1.TextUsing rm As New ResourceManagerdut_tcpip = rm.Open(visa_string)End Usingdut_tcpip.RawIO.Write("*IDN?" + vbLf)MsgBox(dut_tcpip.RawIO.ReadString())End Sub

author: NY2KW
author_rank: Regular Contributor
timestamp: April 10, 2020, 01:02:16 pm
content: No errors building but getting exception on the Open line. Reading NI-VISA changes from the now legacy VisaNS:QuoteYou would think that NI would provide at least some examples of code with these changes.

author: NY2KW
author_rank: Regular Contributor
timestamp: April 10, 2020, 02:56:08 pm
content: Thank you again for all your help. I found an obscure thread in an NI Forum on Instrument control discussing the changes to ResourceManager from legacy VisaNS to NI-VISA. You were 100% in your suggestions. In console style program, the following returns the Instrument type, name and serial number in a Command window. Now moving to Windows FormCode:Imports SystemImports System.Collections.GenericImports Ivi.VisaImports NationalInstruments.VisaModule Module1Dim visa_string = "USB?*"Sub Main()Using rm = New ResourceManager()TryDim resources = rm.Find(visa_string)'Dim resources As IEnumerable(Of String) = rm.Find(visa_string)For Each s As String In resourcesDim parseResult As ParseResult = rm.Parse(s)System.Console.WriteLine(s, parseResult.InterfaceType)NextCatch ex As ExceptionSystem.Console.WriteLine("Exception" + ex.Message)End TryEnd UsingSystem.Console.ReadLine()End SubI didn't understand the commented line why IEnumerable was used so I simplified it and still worked fine. Now onto using VS to put this and more into a Windows Form app.Will keep you posted.

author: BroMarduk
author_rank: Unknown
timestamp: April 10, 2020, 09:00:24 pm
content: Glad you found the solution. It's hard debugging an API you don't know in your head (but the basics still applied).