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

📄 form1.h

📁 microchip PIC18,pic24系列单片机USB驱动的开发源码
💻 H
📖 第 1 页 / 共 2 页
字号:
//Here are some useful articles when creating new PC applications for COM ports:
//(links valid as of June 3, 2008)
//
//"SerialPort Class"
//http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx
//
//"SerialPort Members"
//http://msdn.microsoft.com/en-us/library/system.io.ports.serialport_members.aspx
//
//"SerialPort.DataReceived Event"
//http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx
//
//"How to: Make Thread-Safe Calls to Windows Forms Controls"
//http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx

#pragma once

namespace VCCDC {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Threading;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>


	public ref class Form1 : public System::Windows::Forms::Form
	{
	static int numPorts = 0;
        //Create a delegate function for this thread that will take
        //  in a string and will write it to the txtDataReceived textbox

	delegate void SetTextCallback(String^ text);

		public:
		/****************************************************************************
			Function:
				public Form1()

			Summary:
				The main contructor for the Form1 class.

			Description:
				The main contructor for the Form1 class.  This function creates and
				initializes all of the form objects.

			Precondition:
				None

			Parameters:
				None

			Return Values:
				None

			Remarks:
				None
		***************************************************************************/
		Form1(void)
		{
			InitializeComponent();

			UpdateCOMPortList();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	protected: 
	private: System::IO::Ports::SerialPort^  serialPort1;
	private: System::Windows::Forms::Button^  btnConnect;
	private: System::Windows::Forms::Button^  btnSendData;
	private: System::Windows::Forms::TextBox^  txtSendData;
	private: System::Windows::Forms::Timer^  timer1;
	private: System::Windows::Forms::ComboBox^  lstCOMPorts;

	private: System::Windows::Forms::Button^  btnClose;
	private: System::Windows::Forms::TextBox^  txtDataReceived;
	private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma 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>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->txtDataReceived = (gcnew System::Windows::Forms::TextBox());
			this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
			this->btnConnect = (gcnew System::Windows::Forms::Button());
			this->btnSendData = (gcnew System::Windows::Forms::Button());
			this->txtSendData = (gcnew System::Windows::Forms::TextBox());
			this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
			this->lstCOMPorts = (gcnew System::Windows::Forms::ComboBox());
			this->btnClose = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// txtDataReceived
			// 
			this->txtDataReceived->Location = System::Drawing::Point(12, 66);
			this->txtDataReceived->Multiline = true;
			this->txtDataReceived->Name = L"txtDataReceived";
			this->txtDataReceived->Size = System::Drawing::Size(522, 155);
			this->txtDataReceived->TabIndex = 0;
			// 
			// serialPort1
			// 
			this->serialPort1->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived);
			// 
			// btnConnect
			// 
			this->btnConnect->Location = System::Drawing::Point(12, 12);
			this->btnConnect->Name = L"btnConnect";
			this->btnConnect->Size = System::Drawing::Size(76, 21);
			this->btnConnect->TabIndex = 1;
			this->btnConnect->Text = L"Connect";
			this->btnConnect->UseVisualStyleBackColor = true;
			this->btnConnect->Click += gcnew System::EventHandler(this, &Form1::btnConnect_Click);
			// 
			// btnSendData
			// 
			this->btnSendData->Location = System::Drawing::Point(12, 39);
			this->btnSendData->Name = L"btnSendData";
			this->btnSendData->Size = System::Drawing::Size(76, 21);
			this->btnSendData->TabIndex = 2;
			this->btnSendData->Text = L"Send Data";
			this->btnSendData->UseVisualStyleBackColor = true;
			this->btnSendData->Click += gcnew System::EventHandler(this, &Form1::btnSendData_Click);
			// 
			// txtSendData
			// 
			this->txtSendData->Location = System::Drawing::Point(94, 40);
			this->txtSendData->Name = L"txtSendData";
			this->txtSendData->Size = System::Drawing::Size(440, 20);
			this->txtSendData->TabIndex = 3;
			// 
			// timer1
			// 
			this->timer1->Enabled = true;
			this->timer1->Interval = 1;
			this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
			// 
			// lstCOMPorts
			// 
			this->lstCOMPorts->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->lstCOMPorts->FormattingEnabled = true;
			this->lstCOMPorts->Location = System::Drawing::Point(94, 12);
			this->lstCOMPorts->Name = L"lstCOMPorts";
			this->lstCOMPorts->Size = System::Drawing::Size(99, 21);
			this->lstCOMPorts->TabIndex = 4;
			// 
			// btnClose
			// 
			this->btnClose->Enabled = false;
			this->btnClose->Location = System::Drawing::Point(199, 12);
			this->btnClose->Name = L"btnClose";
			this->btnClose->Size = System::Drawing::Size(78, 22);
			this->btnClose->TabIndex = 5;
			this->btnClose->Text = L"Close";
			this->btnClose->UseVisualStyleBackColor = true;
			this->btnClose->Click += gcnew System::EventHandler(this, &Form1::btnClose_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(546, 236);
			this->Controls->Add(this->btnClose);
			this->Controls->Add(this->lstCOMPorts);
			this->Controls->Add(this->txtSendData);
			this->Controls->Add(this->btnSendData);
			this->Controls->Add(this->btnConnect);
			this->Controls->Add(this->txtDataReceived);
			this->Name = L"Form1";
			this->Text = L"VC++ COM port example";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		/****************************************************************************
			Function:
				private void UpdateCOMPortList()

			Summary:
				This function updates the COM ports listbox.

			Description:
				This function updates the COM ports listbox.  This function is launched 
				periodically based on its Interval attribute (set in the form editor under
				the properties window).

			Precondition:
				None

			Parameters:
				None

			Return Values:
				None

			Remarks:
				None
		***************************************************************************/
		private: void UpdateCOMPortList(void)
		{
			int i;
			bool foundDifference;
			cli::array<System::String^,1>^ portNames; 

			i = 0;
			foundDifference = false;

			//If the number of COM ports is different than the last time we
			//  checked, then we know that the COM ports have changed and we
			//  don't need to verify each entry.
			portNames = serialPort1->GetPortNames();
			if(lstCOMPorts->Items->Count == portNames->Length)
			{
				//Search the entire SerialPort object.  Look at COM port name
				//  returned and see if it already exists in the list.
				for(i=0;i<portNames->Length;i++)
				{
					if(lstCOMPorts->Items[i]->Equals(portNames[i]) ==  false)
					{
						foundDifference = true;
					}
				}
			}
			else
			{
				foundDifference = true;
			}

			//If nothing has changed, exit the function.
			if (foundDifference == false)
			{
				return;
			}

			//If something has changed, then clear the list
			lstCOMPorts->Items->Clear();

			//Add all of the current COM ports to the list
			for(i=0;i<portNames->Length;i++)
			{
				lstCOMPorts->Items->Add(portNames[i]);
			}

			//Set the listbox to point to the first entry in the list
			lstCOMPorts->SelectedIndex = 0;
		}

		/****************************************************************************
			Function:
				private void timer1_Tick(object sender, EventArgs e)

			Summary:
				This function updates the COM ports listbox.

			Description:
				This function updates the COM ports listbox.  This function is launched 
				periodically based on its Interval attribute (set in the form editor under
				the properties window).

			Precondition:
				None

			Parameters:
				object sender     - Sender of the event (this form)
				EventArgs e       - The event arguments

⌨️ 快捷键说明

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