Как определить аппаратный идентификатор USB в UsbRaw от Ni Visa - PullRequest
0 голосов
/ 03 апреля 2019

, поэтому я пытаюсь использовать UsbRaw из национальной библиотеки инструментов для связи с моим USB-устройством и начать потоковую передачу данных с моего USB. Моя первая проблема - это мой первый шаг - определить, с каким USB-устройством я хочу работать. Я уверен, что мне нужно установить идентификатор поставщика и идентификатор продукта моего USB внутри моего кода, но я не знаю, как это сделать, и, к сожалению, нет примеров для UsbRaw в новых версиях NiVisa, и все, что есть, для Библиотека VisaNs, которая больше не поддерживается. ее пример VisaNs от VisaNs:

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


namespace NationalInstruments.Examples.SimpleReadWrite
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
        private UsbRaw mySession;
        private string lastResourceString = null;
        private System.Windows.Forms.TextBox readText;
        private System.Windows.Forms.Button openSessionButton;
        private System.Windows.Forms.Button clearButton;
        private System.Windows.Forms.Button closeSessionButton;
        private System.Windows.Forms.Label stringToReadLabel;
        private System.Windows.Forms.Button eInterruptsButton;
        private System.Windows.Forms.Button dInterruptsButton;
        /// <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(mySession != null)
                {
                    mySession.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.eInterruptsButton = new System.Windows.Forms.Button();
            this.openSessionButton = new System.Windows.Forms.Button();
            this.readText = new System.Windows.Forms.TextBox();
            this.clearButton = new System.Windows.Forms.Button();
            this.closeSessionButton = new System.Windows.Forms.Button();
            this.stringToReadLabel = new System.Windows.Forms.Label();
            this.dInterruptsButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // eInterruptsButton
            // 
            this.eInterruptsButton.Location = new System.Drawing.Point(8, 38);
            this.eInterruptsButton.Name = "eInterruptsButton";
            this.eInterruptsButton.Size = new System.Drawing.Size(111, 23);
            this.eInterruptsButton.TabIndex = 3;
            this.eInterruptsButton.Text = "Enable Interrupts";
            this.eInterruptsButton.Click += new System.EventHandler(this.eInterruptsButton_Click);
            // 
            // openSessionButton
            // 
            this.openSessionButton.Location = new System.Drawing.Point(8, 5);
            this.openSessionButton.Name = "openSessionButton";
            this.openSessionButton.Size = new System.Drawing.Size(111, 23);
            this.openSessionButton.TabIndex = 0;
            this.openSessionButton.Text = "Open Session";
            this.openSessionButton.Click += new System.EventHandler(this.openSession_Click);
            // 
            // readText
            // 
            this.readText.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.readText.Location = new System.Drawing.Point(9, 101);
            this.readText.Multiline = true;
            this.readText.Name = "readText";
            this.readText.ReadOnly = true;
            this.readText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.readText.Size = new System.Drawing.Size(224, 108);
            this.readText.TabIndex = 6;
            this.readText.TabStop = false;
            this.readText.TextChanged += new System.EventHandler(this.readText_TextChanged);
            // 
            // 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(5, 215);
            this.clearButton.Name = "clearButton";
            this.clearButton.Size = new System.Drawing.Size(231, 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(126, 5);
            this.closeSessionButton.Name = "closeSessionButton";
            this.closeSessionButton.Size = new System.Drawing.Size(111, 23);
            this.closeSessionButton.TabIndex = 1;
            this.closeSessionButton.Text = "Close Session";
            this.closeSessionButton.Click += new System.EventHandler(this.closeSession_Click);
            // 
            // stringToReadLabel
            // 
            this.stringToReadLabel.Location = new System.Drawing.Point(8, 87);
            this.stringToReadLabel.Name = "stringToReadLabel";
            this.stringToReadLabel.Size = new System.Drawing.Size(167, 15);
            this.stringToReadLabel.TabIndex = 9;
            this.stringToReadLabel.Text = "Data:";
            // 
            // dInterruptsButton
            // 
            this.dInterruptsButton.Location = new System.Drawing.Point(126, 38);
            this.dInterruptsButton.Name = "dInterruptsButton";
            this.dInterruptsButton.Size = new System.Drawing.Size(111, 23);
            this.dInterruptsButton.TabIndex = 12;
            this.dInterruptsButton.Text = "Disable Interrupts";
            this.dInterruptsButton.Click += new System.EventHandler(this.dInterruptsButton_Click);
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(243, 244);
            this.Controls.Add(this.dInterruptsButton);
            this.Controls.Add(this.stringToReadLabel);
            this.Controls.Add(this.closeSessionButton);
            this.Controls.Add(this.clearButton);
            this.Controls.Add(this.readText);
            this.Controls.Add(this.openSessionButton);
            this.Controls.Add(this.eInterruptsButton);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimumSize = new System.Drawing.Size(251, 278);
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Get USB RAW Interrupts";
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        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;
                    try
                    {
                        mySession = (UsbRaw)ResourceManager.GetLocalManager().Open(sr.ResourceName);
                        SetupControlState(true);
                    }
                    catch(InvalidCastException)
                    {
                        MessageBox.Show("Resource selected must be a USB session");
                    }
                    catch(Exception exp)
                    {
                        MessageBox.Show(exp.Message);
                    }
                    finally
                    {
                        Cursor.Current = Cursors.Default;
                    }
                }
            }
        }

        private void closeSession_Click(object sender, System.EventArgs e)
        {
            SetupControlState(false);
            mySession.Dispose();
        }


        private void clear_Click(object sender, System.EventArgs e)
        {
            readText.Text = String.Empty;
        }

        private void SetupControlState(bool isSessionOpen)
        {
            openSessionButton.Enabled = !isSessionOpen;
            closeSessionButton.Enabled = isSessionOpen;
            eInterruptsButton.Enabled = isSessionOpen;
            dInterruptsButton.Enabled = isSessionOpen;
            clearButton.Enabled = isSessionOpen;
            if(isSessionOpen)
            {
                readText.Text = String.Empty;
            }
        }

        private void eInterruptsButton_Click(object sender, System.EventArgs e)
        {
            mySession.UsbInterrupt += new UsbRawInterruptEventHandler(OnUSBInterrupt);
            mySession.EnableEvent(UsbRawEventType.UsbInterrupt, EventMechanism.Handler);
        }

        private void dInterruptsButton_Click(object sender, System.EventArgs e)
        {
            mySession.DisableEvent(UsbRawEventType.UsbInterrupt, EventMechanism.Handler);
        }

        private void OnUSBInterrupt(object sender, UsbRawInterruptEventArgs e)
        {
            try
            {
                byte [] data = e.DataBuffer;

                readText.Text = String.Empty;
                for(int i=0; i<data.Length; i++)
                    readText.Text = readText.Text + " " + data[i].ToString();

            }
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }




    }
}
...