Exemplo n.º 1
0
        /// <summary>
        /// Sets the size of the PDU.
        /// </summary>
        /// <param name="pduMessage">The PDU formatted SMS message to send.</param>
        private void SetSize(PduSmsMessage pduMessage)
        {
            // Set the size.
            this.port.Write(string.Format(CultureInfo.InvariantCulture, "AT+CMGS={0}\r\n", pduMessage.Length));

            // Validate response.
            while (true)
            {
                var response = this.GetPortResponse();

                if (response.IndexOf("ERROR", StringComparison.OrdinalIgnoreCase) != -1)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Got error response '{0}'.", response));
                }

                if (SmsSender.Debug)
                {
                    Console.WriteLine("SetContent() got response: '{0}'.", response.Trim());
                }

                if (response.IndexOf(">", StringComparison.OrdinalIgnoreCase) != -1)
                {
                    break;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SmsSender" /> class.
        /// </summary>
        /// <param name="portName">The name of the serial port.</param>
        /// <param name="countryCode">The number's country code.</param>
        /// <param name="localNumber">The number in local format.</param>
        /// <param name="text">The text to send.</param>
        public SmsSender(string portName, short countryCode, long localNumber, string text)
        {
            if (string.IsNullOrWhiteSpace(portName))
            {
                throw new ArgumentException("A port name must be specified.", "portName");
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentNullException("A text must be specified.", "text");
            }

            // Port creation.
            this.port = new SerialPort(portName, SmsSender.BaudRate);
            this.port.DataReceived += this.DataReceived;

            // Number composition.
            var numberOfDigits = localNumber.ToString(CultureInfo.InvariantCulture).Length;
            var fullNumber     = ((long)(countryCode * Math.Pow(10, numberOfDigits))) + localNumber;

            this.pduMessages = text.Length <= PduSmsMessage.MaximumSmsTextLength ? new[] { new PduSmsMessage(fullNumber, text) } : PduSmsMessage.GetConcatenatedMessages(fullNumber, text);
        }