Пример #1
0
        /// <summary>
        /// Test if the device support Message Format AT Command.
        /// </summary>
        /// <returns>
        /// Retun <see langword="true"/> if the device support the AT Command, otherwise
        /// <see langword="false"/>
        /// </returns>
        public bool MessageFormat_Test()
        {
            try
            {
                string command = ATCommands.MessageFormat_Test;
                _serialport.DiscardInBuffer();
                _serialport.DiscardOutBuffer();
                receiveNow.Reset();
                _serialport.Write(command);

                string response = ReadResponse(1000);
                if (response.EndsWith(ATFinalResultCode.ERROR) || response.Length == 0)
                {
                    return(false);
                }
                else if (response.EndsWith(ATFinalResultCode.OK))
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);
            }

            return(false);
        }
Пример #2
0
        private void CheckConnection2()
        {
            try
            {
                _serialport.DiscardInBuffer();
                _serialport.DiscardOutBuffer();
                receiveNow.Reset();
                _serialport.Write(ATCommands.CHECK_CONNECTION);

                string response = ReadResponse(1000);
                if (response.EndsWith(ATFinalResultCode.ERROR) || response.Length == 0)
                {
                    ResultRetievedEventArgs ev = new ResultRetievedEventArgs(false, response);
                    OnResultRetieved(ev);

                    ShowMessageInStatusbarEventArgs ev2 = new ShowMessageInStatusbarEventArgs("AT Commands test:Failed", 5 * 1000);
                    OnShowMessageInStatusbar(ev2);
                }
                else if (response.EndsWith(ATFinalResultCode.OK))
                {
                    ResultRetievedEventArgs ev = new ResultRetievedEventArgs(true, response);
                    OnResultRetieved(ev);

                    ShowMessageInStatusbarEventArgs ev2 = new ShowMessageInStatusbarEventArgs("AT Commands test:Successful", 5 * 1000);
                    OnShowMessageInStatusbar(ev2);
                }
            }
            catch (Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);
            }
        }
Пример #3
0
 /// <summary>
 /// Raises the <see cref="E:ShowMessageInStatusbar"/> event.
 /// </summary>
 /// <param name="e">The <see cref="LogicLayer.ShowMessageInStatusbarEventArgs"/> instance containing the event data.</param>
 protected virtual void OnShowMessageInStatusbar(ShowMessageInStatusbarEventArgs e)
 {
     if (ShowMessageInStatusbar != null)
     {
         ShowMessageInStatusbar(this, e);
     }
 }
Пример #4
0
 private void _serialport_DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
     try
     {
         if (e.EventType == SerialData.Chars)
         {
             receiveNow.Set();
         }
     }
     catch (Exception ex)
     {
         ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
         OnShowMessageInStatusbar(ev);
     }
 }
Пример #5
0
        // Public Methods (1) 

        /// <summary>
        /// Reads Service Centre Address from device
        /// </summary>
        public DeviceAddress AddressType_Read()
        {
            try
            {
                string command = ATCommands.AddressType_Read;
                _serialport.DiscardInBuffer();
                _serialport.DiscardOutBuffer();
                receiveNow.Reset();
                _serialport.Write(command);

                string response = ReadResponse(1000);
                if (response.EndsWith(ATFinalResultCode.ERROR) || response.Length == 0)
                {
                    return(null);
                }
                else if (response.EndsWith(ATFinalResultCode.OK))
                {
                    string        result      = ReadResult(response, command, ATFinalResultCode.OK);
                    char[]        delimiter   = { ',' };
                    string[]      finalResult = result.Split(delimiter);
                    SCAddressType _type       = new SCAddressType();
                    if (finalResult[1].Trim() == "145")
                    {
                        _type = SCAddressType.International;
                    }
                    else if (finalResult[1].Trim() == "129")
                    {
                        _type = SCAddressType.Other;
                    }

                    string        returnSubCSCA = "+CSCA: ";
                    string        number1       = finalResult[0].Trim();
                    string        number2       = number1.Remove(0, returnSubCSCA.Length);
                    char[]        triming       = { '"' };
                    string        number3       = number2.Trim(triming);
                    DeviceAddress da            = new DeviceAddress(number3, _type);
                    return(da);
                }
            }
            catch (Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);
            }

            return(null);
        }
Пример #6
0
        /// <summary>
        /// Reads the result without AT Command and Final Result Code.
        /// </summary>
        /// <param name="Response">The response.</param>
        /// <param name="ATCommand">The AT command.</param>
        /// <param name="FinalCode">The final code.</param>
        /// <returns></returns>
        private string ReadResult(string Response, string ATCommand, string FinalCode)
        {
            try
            {
                int    finalcodeindex = Response.LastIndexOf(FinalCode);
                string temp           = Response.Remove(finalcodeindex, FinalCode.Length);
                int    atcommandindex = temp.IndexOf(ATCommand);
                temp = temp.Remove(atcommandindex, ATCommand.Length);
                string result = temp.Trim();
                return(result);
            }
            catch (Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);

                return(string.Empty);
            }
        }
Пример #7
0
 public void Connect2()
 {
     try
     {
         if (_serialport.IsOpen == false)
         {
             ShowMessageInStatusbarEventArgs e = new ShowMessageInStatusbarEventArgs("Connecting...", 10 * 1000);
             OnShowMessageInStatusbar(e);
             LoadSetting();
             _serialport.Open();
             e = new ShowMessageInStatusbarEventArgs("Connected.", 5 * 1000);
             OnShowMessageInStatusbar(e);
         }
     }
     catch (Exception ex)
     {
         ShowMessageInStatusbarEventArgs e = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
         OnShowMessageInStatusbar(e);
     }
 }
Пример #8
0
        public void SendToTerminal2(object state)
        {
            try
            {
                string command = (string)state;
                _serialport.DiscardInBuffer();
                _serialport.DiscardOutBuffer();
                receiveNow.Reset();
                _serialport.Write(command + "\r\n");

                string response            = ReadResponse(1000);
                ResultRetievedEventArgs ev = new ResultRetievedEventArgs(true, response);
                OnTerminalResponseRetrieved(ev);
            }
            catch (Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);
            }
        }
Пример #9
0
 public void Disconnect2()
 {
     try
     {
         ShowMessageInStatusbarEventArgs e = new ShowMessageInStatusbarEventArgs("Disconnecting...", 10 * 1000);
         OnShowMessageInStatusbar(e);
         _serialport.Close();
         _serialport = new SerialPort();
         e           = new ShowMessageInStatusbarEventArgs("Disconnected.", 5 * 1000);
         OnShowMessageInStatusbar(e);
     }
     catch (System.Exception ex)
     {
         ShowMessageInStatusbarEventArgs e = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
         OnShowMessageInStatusbar(e);
     }
     finally
     {
         _serialport.Dispose();
     }
 }
Пример #10
0
        private void SendSMS2(object state)
        {
            try
            {
                object syncRoot = new object();
                lock (syncRoot)
                {
                    ShowMessageInStatusbarEventArgs ev1 = new ShowMessageInStatusbarEventArgs("Sending...", 10 * 1000);
                    OnShowMessageInStatusbar(ev1);
                    string[] commands = (string[])state;
                    string   command1 = commands[0];
                    string   command2 = commands[1];

                    //_serialport.DiscardInBuffer();
                    //_serialport.DiscardOutBuffer();
                    receiveNow.Reset();
                    _serialport.Write(command1);
                    Thread.Sleep(100);
                    _serialport.Write(command2);

                    string response = ReadResponse(1000);

                    if (response.EndsWith(ATFinalResultCode.OK))
                    {
                        ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs("Message Sent", 5 * 1000);
                        OnShowMessageInStatusbar(ev);
                    }
                    else
                    {
                        ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs("Sending Failed", 5 * 1000);
                        OnShowMessageInStatusbar(ev);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);
            }
        }
Пример #11
0
        /// <summary>
        /// Reads the response from selected COM port.
        /// </summary>
        /// <param name="timeout">The timeout.</param>
        /// <returns></returns>
        public string ReadResponse(int timeout)
        {
            string buffer = "";

            try
            {
                do
                {
                    if (receiveNow.WaitOne(timeout, false))
                    {
                        string t = _serialport.ReadExisting();
                        buffer += t;
                    }
                } while (!buffer.EndsWith(ATFinalResultCode.ERROR) && !buffer.EndsWith(ATFinalResultCode.OK));
            }
            catch (System.Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);
            }

            return(buffer);
        }
Пример #12
0
        private void TestDevice2()
        {
            try
            {
                Test_AT_Commands atCommands = new Test_AT_Commands();
                Test_Commands[]  commands   = atCommands.GetAllCommands();
                foreach (Test_Commands command in commands)
                {
                    _serialport.DiscardInBuffer();
                    _serialport.DiscardOutBuffer();
                    receiveNow.Reset();
                    _serialport.Write(command.Command);

                    string response = ReadResponse(1000);

                    if (response.EndsWith(ATFinalResultCode.OK))
                    {
                        ResultRetievedEventArgs ev = new ResultRetievedEventArgs(true, command.Description);
                        OnTestCompleted(ev);
                    }
                    else
                    {
                        ResultRetievedEventArgs ev = new ResultRetievedEventArgs(false, command.Description);
                        OnTestCompleted(ev);
                    }

                    // Release Memory
                    atCommands = null;
                    //
                }
            }
            catch (Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);
            }
        }
Пример #13
0
        private void Request_Signal_Quality2()
        {
            try
            {
                _serialport.DiscardOutBuffer();
                _serialport.DiscardInBuffer();
                receiveNow.Reset();
                _serialport.Write(ATCommands.SIGNAL_QUALITY);

                string response = ReadResponse(1000);

                string result;
                if (response.EndsWith(ATFinalResultCode.OK))
                {
                    result = ReadResult(response, ATCommands.SIGNAL_QUALITY, ATFinalResultCode.OK);
                    ResultRetievedEventArgs ev = new ResultRetievedEventArgs(true, result);
                    OnSignalQualityRetrieved(ev);
                }
                else if (response.EndsWith(ATFinalResultCode.ERROR))
                {
                    result = ReadResult(response, ATCommands.SIGNAL_QUALITY, ATFinalResultCode.ERROR);
                    ResultRetievedEventArgs ev = new ResultRetievedEventArgs(false, result);
                    OnSignalQualityRetrieved(ev);
                }
                else if (response.Length == 0)
                {
                    result = "";
                    ResultRetievedEventArgs ev = new ResultRetievedEventArgs(false, result);
                    OnSignalQualityRetrieved(ev);
                }
            }
            catch (System.Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);
            }
        }
Пример #14
0
        private void Request_Model_Identification2()
        {
            try
            {
                _serialport.DiscardOutBuffer();
                _serialport.DiscardInBuffer();
                receiveNow.Reset();
                _serialport.Write(ATCommands.MODEL_IDENTIFICATION);

                string response = ReadResponse(1000);

                string result;
                if (response.EndsWith(ATFinalResultCode.OK))
                {
                    result = ReadResult(response, ATCommands.MODEL_IDENTIFICATION, ATFinalResultCode.OK);
                    ResultRetievedEventArgs ev = new ResultRetievedEventArgs(true, result);
                    OnModelRetrieved(ev);
                }
                else if (response.EndsWith(ATFinalResultCode.ERROR))
                {
                    result = ReadResult(response, ATCommands.MODEL_IDENTIFICATION, ATFinalResultCode.ERROR);
                    ResultRetievedEventArgs ev = new ResultRetievedEventArgs(false, result);
                    OnModelRetrieved(ev);
                }
                else if (response.Length == 0)
                {
                    result = "";
                    ResultRetievedEventArgs ev = new ResultRetievedEventArgs(false, result);
                    OnModelRetrieved(ev);
                }
            }
            catch (System.Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);
            }
        }
Пример #15
0
        /// <summary>
        /// Read <b>Message Format</b> setting for current device
        /// </summary>
        /// <returns>
        /// PDU Mode or TEXT Mode
        /// </returns>
        public SMSMode MessageFormat_Read()
        {
            try
            {
                string command = ATCommands.MessageFormat_Read;
                _serialport.DiscardInBuffer();
                _serialport.DiscardOutBuffer();
                receiveNow.Reset();
                _serialport.Write(command);

                string response = ReadResponse(1000);
                if (response.EndsWith(ATFinalResultCode.ERROR) || response.Length == 0)
                {
                    return(SMSMode.NULL);
                }
                else if (response.EndsWith(ATFinalResultCode.OK))
                {
                    string result = ReadResult(response, command, ATFinalResultCode.OK);
                    if (result.Contains("0"))
                    {
                        return(SMSMode.PDU);
                    }
                    else if (result.Contains("1"))
                    {
                        return(SMSMode.TEXT);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowMessageInStatusbarEventArgs ev = new ShowMessageInStatusbarEventArgs(ex.Message, 5 * 1000);
                OnShowMessageInStatusbar(ev);
            }

            return(SMSMode.NULL);
        }