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

📄 celltrack.cs

📁 用C#实现的取得CellID和LAC的程序源代码!
💻 CS
字号:
using System;
using System.Windows.Forms;

using NiceTracker.Config;
using NiceTracker.Libraries;

namespace NiceTracker
{
	/// <summary>
	/// Summary description for CellTrack.
	/// </summary>
	public class CellTrack
	{
		// TODO Custom event handler
		public event EventHandler AreaChanged;

		// TODO Custom event handler
		public event EventHandler CellChanged;

		// TODO Custom event handler
		public event EventHandler SignalChanged;

		private bool m_comPortWarningShown = false;

		public CellTrack()
		{			
		}
		
		public GSMCell CurrentCell = new GSMCell();
		public GSMCell LastCell = new GSMCell();

		public int Signal = 0;
		public string CellIDResponse = "";
		public string SignalResponse = "";
		
		private static string Operator = "";
		public static string OperatorCode
		{
			get
			{
				if ( Operator == "" )
					updateOperatorName();

				return Operator;
			}
		}

		public void Update()
		{			
			if ( ConfigManager.ComPortString != "" )
			{
				updateCellID();			
				updateSignal();
				
				m_comPortWarningShown = false;
			}
			else
			{
				if ( !m_comPortWarningShown )
				{
					string msg = "You have not selected a COM Port for your GSM Modem.\n\n";
						  msg += "Please go to 'Menu -> Settings' and choose a COM Port.";
					MessageBox.Show( msg );
					m_comPortWarningShown = true;
				}
			}
		}

		private static void updateOperatorName()
		{
			char delimiter = ',';
			string copsString = stripCommand( GSMLib.PerformCOPS( ConfigManager.ComPortString ));
			string[] data = copsString.Split( delimiter );

			if ( data.Length >= 3 )
			{
				Operator = data[2].Replace( "\"", "" );
			}
		}

		private void updateSignal()
		{
			
			string csqString = stripCommand( GSMLib.PerformCSQ( ConfigManager.ComPortString ));			
			bool updatedSignal = false;
			char delimiter = ',';

			string[] data = csqString.Split( delimiter );

			if ( data.Length >= 1 )
			{
				string new_Signal = stripNoise( data[0] );

				if ( new_Signal != Signal.ToString() )
				{
					updatedSignal = true;
				}
		
				int rxLevel = 0;
				try
				{
					rxLevel = Convert.ToInt32( new_Signal );
					
					if ( rxLevel >= 0 && rxLevel < 99 )
						Signal = rxLevel;	
					else
						Signal = 0;
				}
				catch ( FormatException )
				{
					Signal = 0;
				}
			}
			else
			{
				Signal = 0;
			}

			if ( updatedSignal )
			{
				if ( SignalChanged != null )
					SignalChanged( this, null );
			}

			SignalResponse = csqString;

		}

		private static string stripCommand( string inStr )
		{
			int colonIdx = inStr.IndexOf( ":" );

			if ( colonIdx != -1 )
			{
				int crIdx = inStr.IndexOf( "\r", colonIdx );

				if ( crIdx != -1 )
					return inStr.Substring( colonIdx + 1, crIdx - ( colonIdx + 1 ) );
				else
					return inStr.Substring( colonIdx + 1, inStr.Length - ( colonIdx + 1 ) );
			}
			else
				return inStr;
		}

		private void updateCellID()
		{
			// +CREG: <n>,<stat>[,<lac>,<ci>].
			string cregString = stripCommand( GSMLib.PerformCREG( ConfigManager.ComPortString ) ).Replace( " ", "" );
			
			bool updatedCellID = false;
			bool updatedAreaID = false;
			char delimiter = ' ';

			if ( cregString.IndexOf( ',' ) != -1 )
				delimiter = ',';
						
			string[] items = cregString.Split( delimiter );
			
			if ( items.Length >= 4 )
			{
				GSMCell newCell = new GSMCell( stripNoise( items[0] ), stripNoise( items[1] ), stripNoise( items[2] ), stripNoise( items[3] ), Operator );

				if ( newCell.CI != CurrentCell.CI )
				{
					updatedCellID = true;
				}

				if ( newCell.LAC != CurrentCell.LAC )
				{
					updatedAreaID = true;
					updatedCellID = true;
				}
			
				LastCell = CurrentCell;
				CurrentCell = newCell;
			}

			if ( updatedCellID )
			{
				if ( CellChanged != null )
					CellChanged( this, null );
			}

			if ( updatedAreaID )
			{
				if ( AreaChanged != null )
					AreaChanged( this, null );
			}

			CellIDResponse = cregString;
		}

		private string stripNoise( string inStr )
		{
			string outStr = inStr.Replace( ",", "" );
			outStr = outStr.Replace( "\n", "" );
			outStr = outStr.Replace( "\r", "" );
			outStr = outStr.Replace( "\"", "" );

			return outStr;
		}
	}
}

⌨️ 快捷键说明

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