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

📄 modem.cs

📁 gsmmodem通讯/手机编程 通讯/手机编程
💻 CS
字号:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Tollkuci.GsmModem
{
    /// <summary>
    /// Represents a modem connected to the serial port.
    /// </summary>
	public partial class Modem : Component
    {
        #region Constructors
        /// <summary>
        /// Initialize a new instance of the <see cref="Modem"/> class.
        /// </summary>
        public Modem()
		{
			InitializeComponent();

			this.SetDefaultValues();
		}

        /// <summary>
        /// Initialze a new instance of the <see cref="Modem"/> class using the specified container.
        /// </summary>
        /// <param name="container">A <see cref="IContainer"/> object that will contain the newly created object.</param>
		public Modem(IContainer container)
		{
			container.Add(this);

			InitializeComponent();

			this.SetDefaultValues();
        }
        #endregion

        #region Events
        /// <summary>
        /// Occurs when the connection with the modem has been established.
        /// </summary>
        public event EventHandler Connected;

        /// <summary>
        /// Occurs when the modem has been disconnected.
        /// </summary>
        public event EventHandler Disconnected;

        /// <summary>
        /// Occurs when a new message is received.
        /// </summary>
        public event System.EventHandler<MessageEventArgs> NewMessageReceived;
        #endregion

        #region Event Raisers
        /// <summary>
        /// Raises the <see cref="Connected"/> event.
        /// </summary>
        /// <param name="e">A <see cref="EventArgs"/> object that contains the event data.</param>
        protected virtual void OnConnected(EventArgs e)
        {
            if (this.Connected != null)
            {
                this.Connected(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="Disconnected"/> event.
        /// </summary>
        /// <param name="e">A <see cref="EventArgs"/> object that contains the event data.</param>
        protected virtual void OnDisconnected(EventArgs e)
        {
            if (this.Disconnected != null)
            {
                this.Disconnected(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="NewMessageReceived"/> event.
        /// </summary>
        /// <param name="e">A <see cref="MessageEventArgs"/> object that containes the event data.</param>
        protected virtual void OnNewMessageReceived(MessageEventArgs e)
        {
            if (this.NewMessageReceived != null)
            {
                this.NewMessageReceived(this, e);
            }
        }
        #endregion

		#region Fields
		private bool isConnected;
		private string pin;
		private MessageMemory memory;
		#endregion

		#region Properties
		/// <summary>
        /// Gets or sets the name of the port where the modem is connected.
        /// </summary>
		/// <value>
		/// The communication port name.
		/// </value>
        public string Port
        {
            get
            {
				return this.serialPort.PortName;
            }
            set
            {
				this.serialPort.PortName = value;
            }
        }

        /// <summary>
        /// Gets or sets the serial communication baud rate. Default to 9600.
        /// </summary>
        public int BaudRate
        {
            get
            {
				return this.serialPort.BaudRate;
            }
            set
            {
				this.serialPort.BaudRate = value;
            }
        }

        /// <summary>
        /// Gets or sets the parity checking control used in serial communication. Default to Parities.None.
        /// </summary>
        public Parity Parity
        {
            get
            {
				return Helper.ConvertToTollkuciParity(this.serialPort.Parity);
            }
            set
            {
				this.serialPort.Parity = Helper.ConvertToSystemParity(value);
            }
        }

        /// <summary>
        /// Gets or sets the length of data bits per byte in serial communication. Default to 8.
        /// </summary>
        public int DataBits
        {
            get
            {
				return this.serialPort.DataBits;
            }
            set
            {
				this.serialPort.DataBits = value;
            }
        }

        /// <summary>
        /// Gets or sets the number of stop bits per byte in serial communication. Default to 1.
        /// </summary>
		public StopBits StopBits
        {
            get
            {
				return Helper.ConvertToTollkuciStopBits(this.serialPort.StopBits);
            }
            set
            {
				this.serialPort.StopBits = Helper.ConvertToSystemStopBits(value);
            }
        }

        /// <summary>
        /// Gets or sets the handshaking protocol used in serial communication. Default to FlowControl.None.
        /// </summary>
        public FlowControl FlowControl
        {
            get
            {
				return Helper.ConvertToTollkuciFlowControl(this.serialPort.FlowControl);
            }
            set
            {
				this.serialPort.FlowControl = Helper.ConvertToSystemHandshake(value);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating if the modem is connected or not.
        /// </summary>
        public bool IsConnected
        {
            get
            {
				return this.isConnected;
            }
            set
            {
				this.isConnected = value;
            }
        }

        /// <summary>
        /// Gets or sets the PIN to be used for activating the GSM modem services.
        /// </summary>
        public string Pin
        {
            get
            {
				return this.pin;
            }
            set
            {
				this.pin = value;
            }
        }

        /// <summary>
        /// Gets or sets the message memory to be used. Default to SM memory.
        /// </summary>
        public MessageMemory MessageMemory
        {
            get
            {
				return this.memory;
            }
            set
            {
				this.memory = value;
            }
        }
        #endregion

        #region Methods
		private void SetDefaultValues()
		{
			this.serialPort.BaudRate = 9600;
			this.serialPort.DataBits = 8;
			this.serialPort.StopBits = System.IO.Ports.StopBits.One;
			this.serialPort.FlowControl = System.IO.Ports.Handshake.None;
			this.serialPort.Parity = System.IO.Ports.Parity.None;
			this.memory = MessageMemory.SM;
		}
		
		/// <summary>
        /// Connect with the modem using the parameters specified in <see cref="Port"/>, <see cref="BaudRate"/>,
        /// <see cref="DataBits"/>, <see cref="StopBits"/>, <see cref="Parity"/> and <see cref="FlowControl"/> 
        /// properties.
		/// </summary>
		public void Connect()
		{
			this.serialPort.Connect();
			this.OnConnected(EventArgs.Empty);
		}

		/// <summary>
		/// Disconnect the modem.
		/// </summary>
		public void Disconnect()
		{
			this.serialPort.Disconnect();
			this.OnDisconnected(EventArgs.Empty);
		}

		/// <summary>
		/// Send the specified message.
		/// </summary>
        /// <param name="message">A <see cref="OutgoingMessage"/> object to send.</param>
        public void Send(OutgoingMessage message)
		{
        }

        /// <summary>
        /// Read the message at the specified index.
        /// </summary>
        /// <param name="index">Message index.</param>
        /// <returns>A <see cref="IncomingMessage"/> object.</returns>
        public IncomingMessage Read(int index)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Read all messages.
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.List<Tollkuci.GsmModem.IncomingMessage> ReadAll()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Delete the message at the specified index.
        /// </summary>
        public void Delete(int index)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Delete all messages.
        /// </summary>
        public void DeleteAll()
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Send raw AT commands to the modem
        /// </summary>
        public string Command(string command)
        {
            throw new System.NotImplementedException();
        }
        #endregion
    }
}

⌨️ 快捷键说明

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