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

📄 blgpsreader.cs

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


namespace BLT
{

	/// <summary>
	/// gps device state
	/// </summary>
	public enum StateEnum {Receive,Pause,Stop}
	/// <summary>
	/// state of reception
	/// </summary>
	public class GPSReceiveState
	{
		public StateEnum State = StateEnum.Pause;

	}
	
	/// <summary>
	/// event argument for the event MessageReceivedHandler
	/// </summary>
	public class GPSArgs:EventArgs
	{
		public GPSArgs(string strMessage)
		{
			this.strReceivedMessage=strMessage;

		}

		public string ReceivedMessage
		{
			get
			{
				return this.strReceivedMessage;
			}
		}
		string strReceivedMessage;

		
	}

	

	/// <summary>
	/// used to read gps sentences from a specified com port
	/// implemented according to Singleton Pattern
	/// </summary>
	public class BLGPSReader
	{
		#region event definition
		public delegate void MessageReceivedHandler(object oSender,GPSArgs gpsArgs);
		public event MessageReceivedHandler MessageReceived;
		#endregion event definition
		

		#region GPSInfraStructure
		/// <summary>
		/// used to read from com port
		/// </summary>
		SerialPort serialPort;
		/// <summary>
		/// timer to receive 
		/// </summary>
		TimerCallback tcGPSCallback;
		System.Threading.Timer tGPSTimer;
		/// <summary>
		/// Help Class extract single gps sentence
		/// </summary>
		GPSSentenceExtractor oExtractor;
		/// <summary>
		/// used to save gps senteence in database
		/// </summary>
		BLT.BLPersistManager blPersistManager = new BLPersistManager();
		
		#endregion GPSInfraStructure

		 
		
		private BLGPSReader()
		{
			this.oExtractor=new GPSSentenceExtractor();
			// Default value
			this.nReceiveInterval=5000;


			this.tcGPSCallback=new TimerCallback(this.CollectData);
			this.tGPSTimer=new Timer(this.tcGPSCallback,this.gpsState,this.nReceiveInterval,0);	
			
		}

		public static BLGPSReader Instance = new BLGPSReader();


		~BLGPSReader()
		{
			if(this.tGPSTimer!=null)
			{
				this.tGPSTimer.Dispose();

			}
			if(this.serialPort!=null)
			{
                this.serialPort.Dispose();

			}
		
		}

		#region public properties

		/// <summary>
		/// State of the listening process
		/// </summary>
		public StateEnum State
		{
			get
			{
				return gpsState.State;

			}

		}
		GPSReceiveState gpsState = new GPSReceiveState();


		public string MovObjId
		{
			get
			{
				return strMovObjId;

			}
			set
			{
				strMovObjId=value.ToString();


			}

		}
		string strMovObjId;

		/// <summary>
		/// interval to collect data which is buffed in gps receiver
		/// </summary>
		public int ReceiveInterval
		{
			set
			{
				this.nReceiveInterval=Convert.ToInt32(value);
			}
			get
			{
				return this.nReceiveInterval;
			}

		}
		int nReceiveInterval;



		// com port to listen
		public string Port
		{
			get
			{
				return this.strPort;
			}

		}
		string strPort;

		/// <summary>
		/// BautRate
		/// </summary>
		public int BautRate
		{
			get
			{
				return this.nBautRate;
			}

		}
		int nBautRate;

		#endregion public properties


		/// <summary>
		/// triggers to listen gps by setting gps state
		/// </summary>
		/// <param name="strPort">Port</param>
		/// <param name="nBautRate">Baut Rate</param>
		
		public void  StartToReceive(string strPort,int nBautRate)
		{
			try
			{
				this.strPort=strPort;
				this.nBautRate=nBautRate;

				serialPort = new SerialPort(this.strPort,this.nBautRate);
				if(serialPort.IsOpen==false)
				{
					serialPort.Open();
					
				}
				this.gpsState.State=StateEnum.Receive;
				
			}
			catch(Exception oException)
			{
				throw oException;

			}
		

	    }


		public void Pause()
		{
			this.gpsState.State=StateEnum.Pause;

		}

		public void Resume()
		{
			this.gpsState.State=StateEnum.Receive;


		}

		public void Stop()
		{
			this.gpsState.State=StateEnum.Stop;

			
		}

		
		
	
		

		
		/// <summary>
		/// internal method to fire "MessageReceived" event
		/// </summary>
		/// <param name="strGPSSentence"></param>
		void NotifyClients(string strGPSSentence)
		{
			GPSArgs gpsArgs = new GPSArgs(strGPSSentence);
			this.SendMessage(gpsArgs);

		}
		void SendMessage(GPSArgs gpsArgs)
		{
			if(this.MessageReceived!=null)
			{
				this.MessageReceived(this,gpsArgs);

			}

		}

		
		/// <summary>
		/// Callback from tGPSTimer Timer
		/// </summary>
		/// <param name="oState"></param>
		protected void CollectData(object oState)
		{
			try
			{
				if(this.gpsState.State == StateEnum.Receive)
				{

					// read from gps device
					string strRead=this.serialPort.ReadAvailable();
					strRead=strRead.Trim();
					if(strRead!=String.Empty)
					{
						// add to extractor
						this.oExtractor.Add(strRead);
						string strGPSSentence=String.Empty;
						// if a full sentence available, then notify clients and store in data base
						while(this.oExtractor.Get(out strGPSSentence)==true)
						{
						   this.NotifyClients(strGPSSentence);

						   WaitCallback oWaitCallback=new WaitCallback(this.blPersistManager.Persist);
                           BEC.PersistContent oPersistContent = new BEC.PersistContent(strGPSSentence,this.strMovObjId);
	                       ThreadPool.QueueUserWorkItem(oWaitCallback,oPersistContent);

						}

					}
					
				} //if(this.gpsState.State == StateEnum.Receive)

				
			}
			catch(Exception oException)
			{
				string strTest=oException.Message;

			}
			finally
			{
				if((this.gpsState.State != StateEnum.Stop))
				{
					this.tGPSTimer.Change(this.nReceiveInterval,0);
				}
			}

		}


	}
}

⌨️ 快捷键说明

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