⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 GPIB 相关编程
💻 CS
📖 第 1 页 / 共 2 页
字号:
			this.label9.Location = new System.Drawing.Point(24, 352);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(72, 16);
			this.label9.TabIndex = 17;
			this.label9.Text = "Bytes Read";
			// 
			// Start
			// 
			this.Start.Enabled = false;
			this.Start.Location = new System.Drawing.Point(256, 368);
			this.Start.Name = "Start";
			this.Start.Size = new System.Drawing.Size(184, 23);
			this.Start.TabIndex = 18;
			this.Start.Text = "Start";
			this.Start.Click += new System.EventHandler(this.Start_Click);
			// 
			// axCWButton1
			// 
			this.axCWButton1.Location = new System.Drawing.Point(216, 72);
			this.axCWButton1.Name = "axCWButton1";
			this.axCWButton1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axCWButton1.OcxState")));
			this.axCWButton1.Size = new System.Drawing.Size(30, 40);
			this.axCWButton1.TabIndex = 19;
			// 
			// axCWButton2
			// 
			this.axCWButton2.Location = new System.Drawing.Point(217, 216);
			this.axCWButton2.Name = "axCWButton2";
			this.axCWButton2.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axCWButton2.OcxState")));
			this.axCWButton2.Size = new System.Drawing.Size(30, 40);
			this.axCWButton2.TabIndex = 20;
			// 
			// StopBits2
			// 
			this.StopBits2.Items.AddRange(new object[] {
														   "1.0",
														   "1.5",
														   "2.0"});
			this.StopBits2.Location = new System.Drawing.Point(24, 328);
			this.StopBits2.Name = "StopBits2";
			this.StopBits2.Size = new System.Drawing.Size(176, 17);
			this.StopBits2.TabIndex = 21;
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(24, 312);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(72, 16);
			this.label10.TabIndex = 22;
			this.label10.Text = "Stop Bits";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(48, 8);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(352, 56);
			this.label11.TabIndex = 23;
			this.label11.Text = "Select a VISA Resource Name from the drop down list. Configure the serial port wi" +
				"th the proper settings. Choose whether to read, write or both. Enter a string to" +
				" write to the instrument. Press start to execute the serial communication.";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(472, 406);
			this.Controls.Add(this.label11);
			this.Controls.Add(this.label10);
			this.Controls.Add(this.StopBits2);
			this.Controls.Add(this.axCWButton2);
			this.Controls.Add(this.axCWButton1);
			this.Controls.Add(this.Start);
			this.Controls.Add(this.label9);
			this.Controls.Add(this.BytesRead);
			this.Controls.Add(this.label8);
			this.Controls.Add(this.ReadBuffer);
			this.Controls.Add(this.label7);
			this.Controls.Add(this.WriteBuffer);
			this.Controls.Add(this.label6);
			this.Controls.Add(this.FlowControl);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.Parity);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.Delay);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.DataBits);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.BaudRate);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.VISAResource);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.axCWButton1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.axCWButton2)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			//Start a new thread to enable the delay function, Thread.Sleep(), between 
			//writing and reading from the port.
			Thread newThread = 
				new Thread(new ThreadStart(ThreadMethod));
			newThread.ApartmentState = ApartmentState.STA;
			newThread.Start();
			Application.Run(new Form1());
		}

		static void ThreadMethod()
		{
			Thread.Sleep(1000);
		}


		private void Start_Click(object sender, System.EventArgs e)
		{
			//Read in the selected values from the panel and configure the serial port accordingly.
			myAlias = VISAResource.SelectedItem.ToString();
			baudRate = int.Parse(BaudRate.Text);
			dataBits = short.Parse(DataBits.Text);
			parity = Parity.Text;
			stopBits = StopBits2.Text;
			readDelay = int.Parse(Delay.Text);
			flowControlText = FlowControl.Text;
			writeBuffer = WriteBuffer.Text;
			try
				{
					//Open a new VISA session by creating an instance of a SerialSession
					//object.
					mySession = new NationalInstruments.VisaNS.SerialSession(myAlias);

					//Configure the VISA session.
					mySession.BaudRate = baudRate;
					mySession.DataBits = dataBits;
					
					//Set the flow control.
					switch(flowControlText)
					{
						case "XON/XOFF":
							mySession.FlowControl = FlowControlTypes.XOnXOff;
							break;

						case "None":
							flowControl = FlowControlTypes.None;
							break;

						case "RTS/CTS":
							flowControl = FlowControlTypes.RtsCts;
							break;

						case "DTR/DSR":
							flowControl = FlowControlTypes.DtrDsr;
							break;
					}
							
					mySession.FlowControl = flowControl;

					//Set the parity.
					switch(parity)
					{
						case "None":
							mySession.Parity = NationalInstruments.VisaNS.Parity.None;
							break;

						case "Even":
							mySession.Parity = NationalInstruments.VisaNS.Parity.Even;
							break;

						case "Odd":
							mySession.Parity = NationalInstruments.VisaNS.Parity.Odd;
							break;

						case "Mark":
							mySession.Parity = NationalInstruments.VisaNS.Parity.Mark;
							break;

						case "Space":
							mySession.Parity = NationalInstruments.VisaNS.Parity.Space;
							break;
					}
					
					//Set the stop bits.
					switch(stopBits)
					{
						case "1.0":
							mySession.StopBits = NationalInstruments.VisaNS.StopBitType.One;
							break;

						case "1.5":
							mySession.StopBits = NationalInstruments.VisaNS.StopBitType.OneAndOneHalf;
							break;

						case "2.0":
							mySession.StopBits = NationalInstruments.VisaNS.StopBitType.Two;
							break;
					}

					//Check if the Write button is set to on or off.
					if (axCWButton1.Value)
						mySession.Write(writeBuffer); //Write the data to the buffer
			
					//Delay between writing and reading.
					Thread.Sleep(readDelay);

					//Check if the Read button is set to on or off.
					if (axCWButton2.Value)
					{
						//Read all available data from the buffer and display it onscreen.
						readBuffer = mySession.ReadString(mySession.AvailableNumber);
						ReadBuffer.Text = readBuffer;
						bytesRead = readBuffer.Length;
						BytesRead.Text = bytesRead.ToString();
					}
				}
				catch (VisaException ex)
				{
					MessageBox.Show(ex.Message);
				}
				finally
				{
					//Dispose of the SerialSession instance to close the VISA session.
					mySession.Dispose();
				}

		}

		private void Form1_Load(object sender, System.EventArgs e)
		{

			string[] resources = ResourceManager.GetLocalManager().FindResources("?*");
			foreach(string s in resources)
			{
				string Alias, resClass, unAlias;
				short intNum;
				NationalInstruments.VisaNS.HardwareInterfaceType hwit;

				//Display all non-GPIB devices.
				if (s.Substring(0,4) != "GPIB")
				{
					//Read in device names and determine their VISA aliases.
					ResourceManager.GetLocalManager().ParseResource(s,out hwit, out intNum, out resClass, out unAlias, out Alias);
					//Add the VISA aliases to the VISA Resource Name combobox.
					//If the resource has no alias, return the full VISA resource name.
					if (Alias == "")
						VISAResource.Items.Add(s);
					else 
						VISAResource.Items.Add(Alias);
				}
			}
			
		}

		private void VISAResource_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			//Do not allow execution until a VISA resource name has been selected.
			Start.Enabled = true;
		}

	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -