Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Device Initiator

How do I start a application using a NI GPIB interface and C#, I have red through the Simple Read Write application which is included with the drivers however when I open a Windows application it does not include the space to add the device initiator (shown below the NI Simple Read Write)

 

using System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
using
NationalInstruments.NI4882;

 

namespace
NationalInstruments.Examples.SimpleReadWrite
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button openButton;
        private System.Windows.Forms.Button closeButton;
        private System.Windows.Forms.TextBox stringToWrite;
        private System.Windows.Forms.Button writeButton;
        private System.Windows.Forms.Label stringToWriteLabel;
        private System.Windows.Forms.Button readButton;
        private System.Windows.Forms.Label stringReadLabel;
        private Device device;
       private System.Windows.Forms.NumericUpDown boardId;
        private System.Windows.Forms.Label boardIdLabel;
        private System.Windows.Forms.NumericUpDown primaryAddress;
        private System.Windows.Forms.NumericUpDown secondaryAddress;
        private System.Windows.Forms.Label primaryAddressLabel;
        private System.Windows.Forms.Label secondaryAddressLabel;
        private System.Windows.Forms.TextBox stringRead;


I just get.

 

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
NationalInstruments.NI4882;


namespace
WindowsappExtn
{
    public partial class Form1 : Form


    {
        public Form1()
        {
            InitializeComponent();
        }


Could sombody please help me to understand how to start my own program.  Many thanks

0 Kudos
Message 1 of 6
(4,486 Views)

Hi,

  I beleive that line was added by hand.

I'ts just a declaration of a private variable within the public form's class.

You can do similar just to one "area" instead by : (I've used c# express 2005 but you should get the idea)

using

System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NationalInstruments.NI4882; // included this line by hand - added the reference to the project in the solution explorer

namespace WindowsApplication1
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
   Device device1;
  }
 }
}

Hope that helps

Thanks

Sacha Emery
National Instruments (UK)

// it takes almost no time to rate an answer Smiley Wink
0 Kudos
Message 2 of 6
(4,461 Views)
Thank you
Peter

0 Kudos
Message 3 of 6
(4,422 Views)
When I try this loke you suggest.

        private void OpenButt_Click(object sender, EventArgs e)
        {
            Device device1;
            string CommunicatorID;
            CommunicatorID = this.BoardID.Value + "," + this.PimaryID.Value + "," + this.SecondaryID.Value;

            try
            {
                device1.Write(CommunicatorID);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

I get the error Error 
"1    Use of unassigned local variable 'device1'    C:\Documents and Settings\phaynes\My Documents\Visual Studio 2005\Projects\Glauce\Glauce\Form1.cs    116    17    Glauce"

Thrown could you suggest how to fix this problem.

Many thanks Peter

0 Kudos
Message 4 of 6
(4,399 Views)
Sorry did not make this very clear the error is thrown on the device1 part of the code

Peter

0 Kudos
Message 5 of 6
(4,400 Views)

Hi,

  my code example was just a place marker. You have to instantiate the device, not just declare it and then try writing to it.

For example to instantiate the "device1":

device1 =

new Device((int)boardId.Value,(byte)primaryAddress.Value,(byte)currentSecondaryAddress);

(This line is taken straight from the example c# gpib simple read and write code, so the boardId, primaryAddress and SecondaryAddress are
just control on the main form, but you can obviously replace them with the appropriate numbers as constants if you prefer)

Then you can start to use it with the writes and reads.

device1.Write(ReplaceCommonEscapeSequences(stringToWrite.Text));

and

stringRead.Text = InsertCommonEscapeSequences(device1.ReadString());

(the common escapesequences are just there to make sure we're handling for carriage returns etc)

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);
}

To dispose of it :

device1.Dispose();

Hope that helps

Sacha Emery
National Instruments (UK)

// it takes almost no time to rate an answer Smiley Wink
0 Kudos
Message 6 of 6
(4,380 Views)