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

📄 receiverform.cs

📁 这是一个gps的数据读写器。大家可以借鉴学习一下。
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using BLT;

namespace GPSReader
{
	/// <summary>
	/// This form is used display gps sentence
	/// </summary>
	public class ReceiverForm : System.Windows.Forms.Form
	{
		#region GUI
		private System.Windows.Forms.Button btnStart;
		private System.Windows.Forms.Label lbReadDisplay1;
		private System.Windows.Forms.Label lbReadDisplay2;
		private System.Windows.Forms.Label lbReadDisplay3;
		private System.Windows.Forms.Panel panReceive;
		private System.Windows.Forms.Label lbHeading;
		private System.Windows.Forms.ComboBox cbPort;
		private System.Windows.Forms.Label lbPort;
		private System.Windows.Forms.Button btnChangeState;
		#endregion GUI


		BLGPSReader gpsReader;
		/// <summary>
		/// this flag is used to control UI Elements
		/// </summary>
		int nUIState=0; 
		// 0: ready to start
		// 1: started to listen

	
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
   

		public ReceiverForm()
		{
			
			InitializeComponent();
			AdjustUIControl();
			
		}

		void AdjustUIControl()
		{
			if(this.cbPort.SelectedItem==null)
			{
				this.cbPort.SelectedIndex=3;

			}
	
			switch(this.nUIState)
			{
				case 0://Start
				{
					this.btnStart.Enabled=true;
					this.btnChangeState.Enabled=false;
					this.cbPort.Enabled=true;
					break;

				}
				case 1://Started to receive
				{
					this.btnStart.Enabled=false;
					this.btnChangeState.Enabled=true;
					this.cbPort.Enabled=false;
					break;

				}
			
				
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				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()
		{
			this.btnStart = new System.Windows.Forms.Button();
			this.lbReadDisplay1 = new System.Windows.Forms.Label();
			this.lbReadDisplay2 = new System.Windows.Forms.Label();
			this.lbReadDisplay3 = new System.Windows.Forms.Label();
			this.panReceive = new System.Windows.Forms.Panel();
			this.btnChangeState = new System.Windows.Forms.Button();
			this.lbPort = new System.Windows.Forms.Label();
			this.cbPort = new System.Windows.Forms.ComboBox();
			this.lbHeading = new System.Windows.Forms.Label();
			this.panReceive.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnStart
			// 
			this.btnStart.BackColor = System.Drawing.Color.DarkGray;
			this.btnStart.Location = new System.Drawing.Point(48, 80);
			this.btnStart.Name = "btnStart";
			this.btnStart.Size = new System.Drawing.Size(96, 23);
			this.btnStart.TabIndex = 0;
			this.btnStart.Text = "Start";
			this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
			// 
			// lbReadDisplay1
			// 
			this.lbReadDisplay1.Location = new System.Drawing.Point(24, 232);
			this.lbReadDisplay1.Name = "lbReadDisplay1";
			this.lbReadDisplay1.Size = new System.Drawing.Size(560, 25);
			this.lbReadDisplay1.TabIndex = 1;
			this.lbReadDisplay1.Text = "Not yet received!";
			// 
			// lbReadDisplay2
			// 
			this.lbReadDisplay2.Location = new System.Drawing.Point(24, 192);
			this.lbReadDisplay2.Name = "lbReadDisplay2";
			this.lbReadDisplay2.Size = new System.Drawing.Size(568, 25);
			this.lbReadDisplay2.TabIndex = 2;
			this.lbReadDisplay2.Text = "\"Not yet received!";
			// 
			// lbReadDisplay3
			// 
			this.lbReadDisplay3.Location = new System.Drawing.Point(24, 152);
			this.lbReadDisplay3.Name = "lbReadDisplay3";
			this.lbReadDisplay3.Size = new System.Drawing.Size(568, 25);
			this.lbReadDisplay3.TabIndex = 3;
			this.lbReadDisplay3.Text = "Not yet received!";
			// 
			// panReceive
			// 
			this.panReceive.BackColor = System.Drawing.Color.Silver;
			this.panReceive.Controls.Add(this.btnChangeState);
			this.panReceive.Controls.Add(this.lbPort);
			this.panReceive.Controls.Add(this.cbPort);
			this.panReceive.Controls.Add(this.lbHeading);
			this.panReceive.Controls.Add(this.lbReadDisplay3);
			this.panReceive.Controls.Add(this.lbReadDisplay1);
			this.panReceive.Controls.Add(this.lbReadDisplay2);
			this.panReceive.Controls.Add(this.btnStart);
			this.panReceive.Location = new System.Drawing.Point(8, 8);
			this.panReceive.Name = "panReceive";
			this.panReceive.Size = new System.Drawing.Size(632, 304);
			this.panReceive.TabIndex = 4;
			// 
			// btnChangeState
			// 
			this.btnChangeState.BackColor = System.Drawing.Color.DarkGray;
			this.btnChangeState.Location = new System.Drawing.Point(160, 80);
			this.btnChangeState.Name = "btnChangeState";
			this.btnChangeState.Size = new System.Drawing.Size(96, 23);
			this.btnChangeState.TabIndex = 7;
			this.btnChangeState.Text = "Pause";
			this.btnChangeState.Click += new System.EventHandler(this.btnPause_Click);
			// 
			// lbPort
			// 
			this.lbPort.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lbPort.Location = new System.Drawing.Point(24, 40);
			this.lbPort.Name = "lbPort";
			this.lbPort.Size = new System.Drawing.Size(100, 16);
			this.lbPort.TabIndex = 6;
			this.lbPort.Text = "Port";
			// 
			// cbPort
			// 
			this.cbPort.Items.AddRange(new object[] {
														"COM1",
														"COM2",
														"COM3",
														"COM4"});
			this.cbPort.Location = new System.Drawing.Point(136, 40);
			this.cbPort.Name = "cbPort";
			this.cbPort.Size = new System.Drawing.Size(72, 21);
			this.cbPort.TabIndex = 5;
			// 
			// lbHeading
			// 
			this.lbHeading.BackColor = System.Drawing.Color.Silver;
			this.lbHeading.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lbHeading.Location = new System.Drawing.Point(272, 8);
			this.lbHeading.Name = "lbHeading";
			this.lbHeading.Size = new System.Drawing.Size(88, 23);
			this.lbHeading.TabIndex = 4;
			this.lbHeading.Text = "Receiver";
			// 
			// ReceiverForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(688, 358);
			this.Controls.Add(this.panReceive);
			this.Name = "ReceiverForm";
			this.Text = "ReceiverForm";
			this.panReceive.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// callback function to start to receive
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnStart_Click(object sender, System.EventArgs e)
		{

			try
			{
				this.gpsReader = BLT.BLGPSReader.Instance;
				this.gpsReader.MovObjId="Taxi-1";
				this.gpsReader.MessageReceived+= new BLGPSReader.MessageReceivedHandler(this.MessageReceivedCallBack);
				string strPort=(string)this.cbPort.SelectedItem;
			
				if(strPort==null)
				{
					MessageBox.Show(this,"Please Select a suitable Port");

				}
				// trigger to receieve
                this.gpsReader.StartToReceive(strPort,4800);
				// change the state
				this.nUIState=1;
				this.AdjustUIControl();
			}
			catch(Exception oException)
			{
				MessageBox.Show(oException.Message);

			}
			
		
		}



		/// <summary>
		/// callback method to display gps sentence
		/// </summary>
		/// <param name="oSender"></param>
		/// <param name="gpsArgs"></param>
		void MessageReceivedCallBack(object oSender,GPSArgs gpsArgs)
		{
			string strDisplay=" -->"+":"+gpsArgs.ReceivedMessage;
			this.lbReadDisplay1.Text=this.lbReadDisplay2.Text;
			this.lbReadDisplay2.Text=this.lbReadDisplay3.Text;
			this.lbReadDisplay3.Text=strDisplay;

		}

		// stop gps receive
		private void btnStop_Click(object sender, System.EventArgs e)
		{
			gpsReader.Stop();
			nUIState=1;
			AdjustUIControl();
		}

		/// <summary>
		/// this method is used to pause or resume gps device
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnPause_Click(object sender, System.EventArgs e)
		{
			try
			{
				
				if(this.gpsReader.State==StateEnum.Pause)
				{
					this.gpsReader.Resume();
					this.btnChangeState.Text="Pause";
				}
				else
				{
					this.gpsReader.Pause();
					this.btnChangeState.Text="Resume";
				}

				this.nUIState=1;
				this.AdjustUIControl();
			}
			catch(Exception oException)
			{
				MessageBox.Show(oException.Message);

			}

		}
		
	}
}

⌨️ 快捷键说明

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