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

📄 frmdemo.cs

📁 a sample code for send sms using GPRS Modem.
💻 CS
📖 第 1 页 / 共 4 页
字号:
				Cursor.Current = Cursors.Default;
				ShowException(ex);
				return false;
			}
		}

		private string GetMessageStorage()
		{
			string storage = string.Empty;
			if (rbMessageSIM.Checked)
				storage = PhoneStorageType.Sim;
			if (rbMessagePhone.Checked)
				storage = PhoneStorageType.Phone;

			if (storage.Length == 0)
				throw new ApplicationException("Unknown message storage.");
			else
				return storage;
		}

		private string GetPhonebookStorage()
		{
			string storage = string.Empty;
			if (rbPhonebookSIM.Checked)
				storage = PhoneStorageType.Sim;
			if (rbPhonebookPhone.Checked)
				storage = PhoneStorageType.Phone;

			if (storage.Length == 0)
				throw new ApplicationException("Unknown phonebook storage.");
			else
				return storage;
		}

		#region General
		private void btnIdentify_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			try
			{
				IdentificationInfo info = comm.IdentifyDevice();
				Output("Manufacturer: " + info.Manufacturer);
				Output("Model: " + info.Model);
				Output("Revision: " + info.Revision);
				Output("Serial number: " + info.SerialNumber);
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnReset_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			try
			{
				// Reset to default configuration
				comm.ResetToDefaultConfig();
				Output("Config reset");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}
	
		private void btnIsConnected_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			try
			{
				if (!comm.IsConnected())
					Output("Phone NOT connected");
				else
					Output("Phone connected.");

				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		#endregion

		#region SMS Messages
		private void btnSendMessage_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			try
			{
				// Send a SMS message
				Cursor.Current = Cursors.WaitCursor;
				comm.SendMessage(new SmsSubmitPdu(txtMessage.Text, txtNumber.Text, string.Empty));
				Output("Message sent.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnReadMessages_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			string storage = GetMessageStorage();

			try
			{
				// Read all SMS messages from the storage
				DecodedShortMessage[] messages = comm.ReadMessages(PhoneMessageStatus.All, storage);
				foreach(DecodedShortMessage message in messages)
				{
					Output(string.Format("Message status = {0}, Location = {1}/{2}",
						StatusToString(message.Status),	message.Storage, message.Index));
					ShowMessage(message.Data);
					Output("");
				}
				Output(string.Format("{0,9} messages read.", messages.Length.ToString()));
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnDelMessage_Click(object sender, System.EventArgs e)
		{
			int index;
			try
			{
				index = int.Parse(txtDelMsgIndex.Text);
			}
			catch(Exception ex)
			{
				ShowException(ex);
				return;
			}

			if (!Confirmed()) return;
			Cursor.Current = Cursors.WaitCursor;

			string storage = GetMessageStorage();
			try
			{
				// Delete the message with the specified index from storage
				Cursor.Current = Cursors.WaitCursor;
				comm.DeleteMessage(index, storage);
				Output("Message deleted.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnMsgStorages_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			try
			{
				// Get the supported short message storages
				MessageStorageInfo storages = comm.GetMessageStorages();
				Output("Supported read storages: " + MakeArrayString(storages.ReadStorages));
				Output("Supported write storages: " + MakeArrayString(storages.WriteStorages));
				Output("Supported receive storages: " + MakeArrayString(storages.ReceiveStorages));
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnDelAllMsgs_Click(object sender, System.EventArgs e)
		{
			if (!Confirmed()) return;
			Cursor.Current = Cursors.WaitCursor;

			string storage = GetMessageStorage();
			try
			{
				// Delete all messages from phone memory
				comm.DeleteMessages(DeleteScope.All, storage);
				Output("Messages deleted.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnCopyMessages_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			try
			{
				Cursor.Current = Cursors.WaitCursor;

				// Copy all messages from SIM memory to phone memory
				ShortMessageFromPhone[] messages = comm.ReadRawMessages(PhoneMessageStatus.All, PhoneStorageType.Sim);
				Output(messages.Length.ToString() + " messages read.");
				foreach(ShortMessageFromPhone msg in messages)
				{
					int index = comm.WriteRawMessage(msg, PhoneStorageType.Phone);
					Output("Message copied from SIM index " + msg.Index.ToString() +
						" to phone index " + index.ToString() + ".");
				}
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnExportMessages_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			string storage = GetMessageStorage();
			try
			{
				// Read and serialize all short messages from storage into an XML file.
				ShortMessage[] messages = comm.ReadRawMessages(PhoneMessageStatus.All, storage);
				Output(messages.Length.ToString() + " messages read.");
			
				const string filename = @"C:\messages.xml";
				Output("Exporting the messages to " + filename + "...");
				System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(
					typeof(ShortMessageFromPhone[]));
				System.IO.StreamWriter sw = System.IO.File.CreateText(filename);
				ser.Serialize(sw, messages);
				sw.Close();
				Output("Done.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnImportMessages_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			string storage = GetMessageStorage();
			try
			{
				// Load and copy all saved short messages to storage
				const string filename = @"C:\messages.xml";
				Output("Importing messages from " + filename + "...");
				System.IO.StreamReader sr = System.IO.File.OpenText(filename);
				System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(ShortMessageFromPhone[]));
				object temp = ser.Deserialize(sr);
				sr.Close();
				ShortMessageFromPhone[] messages = (ShortMessageFromPhone[])temp;
				foreach(ShortMessageFromPhone msg in messages)
				{
					int index = comm.WriteRawMessage(msg, storage);
					Output("Saved message to storage index " + index.ToString() + ".");
				}
				Output("Done.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnMsgNotification_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			try
			{
				// Enable notifications about new received messages
				if (!registerMessageReceived)
				{
					comm.MessageReceived += new MessageReceivedEventHandler(comm_MessageReceived);
					registerMessageReceived = true;
				}
				comm.EnableMessageNotifications();
				Output("Message notifications activated.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnMsgNotificationOff_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			try
			{
				// Disable message notifications
				comm.DisableMessageNotifications();
				if (registerMessageReceived)
				{
					comm.MessageReceived -= new MessageReceivedEventHandler(comm_MessageReceived);
					registerMessageReceived = false;
				}
				Output("Message notifications deactivated.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnMsgRoutingOn_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			try
			{
				// Enable direct message routing to the application
				if (!registerMessageReceived)
				{
					comm.MessageReceived += new MessageReceivedEventHandler(comm_MessageReceived);
					registerMessageReceived = true;
				}
				comm.EnableMessageRouting();
				Output("Message routing activated.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		private void btnMsgRoutingOff_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			try
			{
				// Disable message routing
				comm.DisableMessageRouting();
				if (registerMessageReceived)
				{
					comm.MessageReceived -= new MessageReceivedEventHandler(comm_MessageReceived);
					registerMessageReceived = false;
				}
				Output("Message routing deactivated.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;

⌨️ 快捷键说明

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