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

📄 gsmlib.cs

📁 用C#实现的取得CellID和LAC的程序源代码!
💻 CS
字号:
using System;
using System.Collections;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;

namespace NiceTracker.Libraries
{
	/// <summary>
	/// Summary description for GSMLib.
	/// </summary>
	public class GSMLib
	{
	
		private static int m_foundModem = -1;

		public GSMLib()
		{}

		public static int DetectModem()
		{				
			int timeout = 10;					
			ArrayList threads = new ArrayList();

			m_foundModem = -1;

			for ( int i=0; i<=9; i++ )
			{
				DetectPort detector = new DetectPort( i );
				detector.ModemFound += new NiceTracker.Libraries.DetectPort.ModemDetectEventHandler(detector_ModemFound);
				detector.Start();
				threads.Add( detector );
			}

			while ( m_foundModem == -1 && timeout > 0 )
			{
				Thread.Sleep( 1000 );
				timeout--;
			}

			return m_foundModem;
		}

		private static void detector_ModemFound(object sender, ModemFoundEventArgs eventArgs)
		{
			m_foundModem = eventArgs.ComPort;
		}

		public static string PerformManufacturerInfo( string port )
		{			
			return toFirstLF( SerialLib.SendModemCommand( port, "AT+CGMI\r" ) );
		}

		public static string PerformModelInfo( string port )
		{			
			return toFirstLF( SerialLib.SendModemCommand( port, "AT+CGMM\r" ) );
		}

		public static string PerformFirmwareInfo( string port )
		{			
			return toFirstLF( SerialLib.SendModemCommand( port, "AT+CGMR\r" ) );
		}

		public static string PerformIMEI( string port )
		{			
			return toFirstLF( SerialLib.SendModemCommand( port, "AT+CGSN\r" ) );
		}

		public static string PerformIMSI( string port )
		{			
			return SerialLib.SendModemCommand( port, "AT+CIMI\r" );						
		}

		public static string PerformCCID( string port )
		{			
			return SerialLib.SendModemCommand( port, "AT+CCID\r" );						
		}	
		
		public static string PerformCREG( string port )
		{			
			return SerialLib.SendModemCommand( port, "AT+CREG=2\r", "AT+CREG?\r" );						
		}

		public static string PerformCCED( string port )
		{
			return SerialLib.SendModemCommand( port, "AT+CCED\r" );		
		}

		public static string PerformCOPS( string port )
		{
			return SerialLib.SendModemCommand( port, "AT+COPS?\r" );		
		}

		public static string PerformCOPN( string port )
		{
			return SerialLib.SendModemCommand( port, "AT+COPN?\r" );		
		}

		public static string PerformCSQ( string port )
		{
			return SerialLib.SendModemCommand( port, "AT+CSQ\r" );
		}

		private static string toFirstLF( string inStr )
		{
			int idx = inStr.IndexOf( "\r" );
			if ( idx != -1 )
				return inStr.Substring( 0, idx );
			else
				return inStr;
		}

		private static string IntPtrToString( IntPtr ptr )
		{
			int maxLen = 32;
			string output = "";
			if ( ptr != IntPtr.Zero )
			{
				char c = (char)Marshal.ReadByte( ptr );

				while ( c != '\r' && c != '\n' && c != '\0' && maxLen > 0 )
				{			
					output += c;
					ptr = (IntPtr)((int)ptr + 1);				
					c = (char)Marshal.ReadByte( ptr );
					maxLen--;
				}
			}
			return output;
		}
	}

	public class DetectPort
	{
		public delegate void ModemDetectEventHandler( object sender, ModemFoundEventArgs eventArgs );

		public event ModemDetectEventHandler ModemFound;
		private Thread m_detectThread = null;
		private int m_comPort = -1;

		public DetectPort( int comPort )
		{
			m_comPort = comPort;
		}

		public void Start()
		{
			m_detectThread = new Thread( new ThreadStart( Run ) );
			m_detectThread.Start();		
				
		}
		
		protected void Run()
		{
			string modemOutput = GSMLib.PerformCSQ( "COM" + m_comPort.ToString() + ":" );				

			if ( modemOutput.IndexOf( "CSQ:" ) != -1 )
			{
				if ( ModemFound != null )
					ModemFound( null, new ModemFoundEventArgs( m_comPort ) );
			}	
		}
	}

	public class ModemFoundEventArgs : EventArgs
	{
		public int ComPort = -1;

		public ModemFoundEventArgs()
		{}

		public ModemFoundEventArgs( int comPort )
		{
			ComPort = comPort;
		}
	}
}

⌨️ 快捷键说明

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