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

📄 frmdemo.cs

📁 a sample code for send sms using GPRS Modem.
💻 CS
📖 第 1 页 / 共 4 页
字号:
		}

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

			string storage = GetMessageStorage();
			try
			{
				// Get memory status of the storage
				MemoryStatus status = comm.GetMessageMemoryStatus(storage);
				int percentUsed = (status.Total > 0) ? (int)((double)status.Used/(double)status.Total*100) : 0;
				Output(string.Format("Message memory status: {0} of {1} used ({2}% used, {3}% free)",
					status.Used, status.Total, percentUsed, 100-percentUsed));
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}
		#endregion

		#region Phonebook

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

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

			Cursor.Current = Cursors.Default;
		}

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

			string storage = GetPhonebookStorage();
			try
			{
				// Load and copy all saved phonebook entries to storage.
				const string filename = @"C:\phonebook.xml";
				Output("Importing phonebook entries from " + filename + "...");
				System.IO.StreamReader sr = System.IO.File.OpenText(filename);
				System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(PhonebookEntry[]));
				object temp = ser.Deserialize(sr);
				sr.Close();
				PhonebookEntry[] entries = (PhonebookEntry[])temp;
				Output(entries.Length.ToString() + " entries to create.");
				foreach(PhonebookEntry entry in entries)
				{
					comm.CreatePhonebookEntry(entry, storage);
					Output("Entry \"" + entry.Text + "\" created.");
				}
				Output("Done.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

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

			string storage = GetPhonebookStorage();
			try
			{
				// Read the whole phonebook from the storage
				PhonebookEntry[] entries = comm.GetPhonebook(storage);
				if (entries.Length > 0)
				{
					// Display the entries read
					foreach(PhonebookEntry entry in entries)
					{
						Output(string.Format("{0,-20}{1}", entry.Number, entry.Text));
					}
				}
				Output(string.Format("{0,9} entries read.", entries.Length.ToString()));
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

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

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

			string storage = GetPhonebookStorage();
			try
			{
				// Delete the phonebook entry with the specified index from storage
				comm.DeletePhonebookEntry(index, storage);
				Output("Entry deleted.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

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

			string storage = GetPhonebookStorage();
			try
			{
				// Delete all phonebook entries from storage
				comm.DeleteAllPhonebookEntries(storage);
				Output("Phonebook deleted.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

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

			string storage = GetPhonebookStorage();
			try
			{
				// Create the phonebook entry in storage
				comm.CreatePhonebookEntry(new PhonebookEntry(0, txtPbNumber.Text,
					txtPbNumber.Text.StartsWith("+") ? AddressType.InternationalPhone : AddressType.UnknownPhone,
					txtPbName.Text), storage);
				Output("Phonebook created.");
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

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

			try
			{
				// Get the supported phonebook storages
				string[] storages = comm.GetPhonebookStorages();
				Output("Supported phonebook storages: " + MakeArrayString(storages));
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

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

			string storage = GetPhonebookStorage();
			try
			{
				// Get memory status of the phonebook storage
				MemoryStatus status = comm.GetPhonebookMemoryStatus(storage);
				int percentUsed = (status.Total > 0) ? (int)((double)status.Used/(double)status.Total*100) : 0;
				Output(string.Format("Phonebook memory status: {0} of {1} used ({2}% used, {3}% free)",
					status.Used, status.Total, percentUsed, 100-percentUsed));
				Output("");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}

			Cursor.Current = Cursors.Default;
		}

		#endregion

		private void chkDebugTrace_CheckedChanged(object sender, System.EventArgs e)
		{
			try
			{
				if (chkDebugTrace.Checked)
				{
					comm.LoglineAdded += new GsmCommunication.GsmComm.LoglineAddedEventHandler(comm_LoglineAdded);
					Output("Debug trace on");
					Output("");
				}
				else
				{
					comm.LoglineAdded -= new GsmCommunication.GsmComm.LoglineAddedEventHandler(comm_LoglineAdded);
					Output("Debug trace off");
					Output("");
				}
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}
		}

		private void txtClearOutput_Click(object sender, System.EventArgs e)
		{
			txtOutput.Clear();
		}

/////////////////////// Event handlers for events from comm object

		private void comm_LoglineAdded(object sender, string text)
		{
			Output(text);
		}

		private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
		{
			try
			{
				IMessageIndicationObject obj = e.IndicationObject;
				if (obj is MemoryLocation)
				{
					MemoryLocation loc = (MemoryLocation)obj;
					Output(string.Format("New message received in storage \"{0}\", index {1}.",
						loc.Storage, loc.Index));
					Output("");
					return;
				}
				if (obj is ShortMessage)
				{
					ShortMessage msg = (ShortMessage)obj;
					SmsPdu pdu = comm.DecodeReceivedMessage(msg);
					Output("New message received:");
					ShowMessage(pdu);
					Output("");
					return;
				}
				Output("Error: Unknown notification object!");
			}
			catch(Exception ex)
			{
				ShowException(ex);
			}
		}

		private delegate void ConnectedHandler(bool connected);
		private void OnPhoneConnectionChange(bool connected)
		{
			lblNotConnected.Visible = !connected;
		}

		private void comm_PhoneConnected(object sender, EventArgs e)
		{
			this.Invoke(new ConnectedHandler(OnPhoneConnectionChange), new object[] { true });
		}

		private void comm_PhoneDisconnected(object sender, EventArgs e)
		{
			this.Invoke(new ConnectedHandler(OnPhoneConnectionChange), new object[] { false });
		}

/////////////////////// Helpers

		private string MakeArrayString(string[] array)
		{
			System.Text.StringBuilder str = new System.Text.StringBuilder();
			foreach(string item in array)
			{
				if (str.Length > 0)
					str.Append(", ");
				str.Append(item);
			}

			return str.ToString();
		}
		
		private void ShowMessage(SmsPdu pdu)
		{
			if (pdu is SmsSubmitPdu)
			{
				// Stored (sent/unsent) message
				SmsSubmitPdu data = (SmsSubmitPdu)pdu;
				Output("SENT/UNSENT MESSAGE");
				Output("Recipient: " + data.DestinationAddress);
				Output("Message text: " + data.UserDataText);
				Output("-------------------------------------------------------------------");
				return;
			}
			if (pdu is SmsDeliverPdu)
			{
				// Received message
				SmsDeliverPdu data = (SmsDeliverPdu)pdu;
				Output("RECEIVED MESSAGE");
				Output("Sender: " + data.OriginatingAddress);
				Output("Sent: " + data.SCTimestamp.ToString());
				Output("Message text: " + data.UserDataText);
				Output("-------------------------------------------------------------------");
				return;
			}
			if (pdu is SmsStatusReportPdu)
			{
				// Status report
				SmsStatusReportPdu data = (SmsStatusReportPdu)pdu;
				Output("STATUS REPORT");
				Output("Recipient: " + data.RecipientAddress);
				Output("Status: " + data.Status.ToString());
				Output("Timestamp: " + data.DischargeTime.ToString());
				Output("Message ref: " + data.MessageReference.ToString());
				Output("-------------------------------------------------------------------");
				return;
			}
			Output("Unknown message type: " + pdu.GetType().ToString());
		}

		private string StatusToString(PhoneMessageStatus status)
		{
			// Map a message status to a string
			string ret;
			switch(status)
			{
				case PhoneMessageStatus.All:
					ret = "All";
					break;
				case PhoneMessageStatus.ReceivedRead:
					ret = "Read";
					break;
				case PhoneMessageStatus.ReceivedUnread:
					ret = "Unread";
					break;
				case PhoneMessageStatus.StoredSent:
					ret = "Sent";
					break;
				case PhoneMessageStatus.StoredUnsent:
					ret = "Unsent";
					break;
				default:
					ret = "Unknown (" + status.ToString() + ")";
					break;
			}
			return ret;
		}

		/////////////////////// Other stuff

	}
}

⌨️ 快捷键说明

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