Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > Visual Studio 2005
|
Visual Studio 2005 For discussing Visual Studio 2005. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 14th, 2007, 06:18 AM
Registered User
 
Join Date: Mar 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to determine response time of an ATcommand

Hi Rod,

I found this code posted by u:


using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Collections;
using log4net;
using log4net.Config;
using System.Threading;

namespace SMSdll
{
    public class Class1
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(Class1));
        static SerialPort ComPort;

        private static string data = "Not Ready.";

        private static void OpenPort()
        {
            try
            {
                ComPort = new SerialPort("COM9", 9600);
                ComPort.Open();
                logger.Info("ComPort Opened." + Environment.NewLine);
            }
            catch (Exception)
            {
                Console.WriteLine("Port not present/available.");
            }
        }


        public static bool SendSMS()
        {
            DateTime timeout = DateTime.Now.AddMinutes(5);
            try
            {
                ComPort.Write("AT+GMM\r");
                data = "";
                do
                {
                    data += ComPort.ReadExisting();
                    logger.Info(data);
                    if (DateTime.Now > timeout)
                    {
                        throw new Exception("AT Command timed out without receiving 'OK'.");
                    }
                }
                while (!data.Contains("OK"));
            }
            catch (Exception ex)
            {
                logger.Error("Sms sending failed miserably! Fix it!", ex);
                return false;
            }
            return true;
        }


        private static void ClosePort()
        {
            if (ComPort.IsOpen)
                ComPort.Close();
            logger.Info(Environment.NewLine + "ComPort Closed" + Environment.NewLine);
        }


        public static void SMSMain()
        {
            log4net.Appender.FileAppender fa = new log4net.Appender.FileAppender(new log4net.Layout.SimpleLayout(), "SMSdllTest.log", true);
            BasicConfigurator.Configure(fa);
            logger.Info("Log created on : " + System.DateTime.Now + Environment.NewLine);

            OpenPort();
            SendSMS();
            ClosePort();
        }
    }
}


My question:

 "DateTime timeout = DateTime.Now.AddMinutes(5);"

Please correct me if I'm wrong, I think the code sample that u posted, to initialize a modem and receive SMS, assumes a random time as the time needed to generate all the responses for that particular AT command. Is there any way to wait till all the responses for a command are received, without using a random response time?



 
Old March 14th, 2007, 10:03 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Hi,
There is no need to know when the response will end as the loop will stop automatically when all the response is received. ie The 'OK' is received.

I put the timeout in just as a backstop incase there is something wrong, a way out for the loop.
In reality the response time is more immediate on the modems I am using.

I think my final code had this at one minute, 10 seconds [u]might</u> be plenty.

Also, it would be an advantage to put a few second thread sleep in between loops as this code puts the CPU in a bit of a spin if there is a problem and you have to wait for the timeout.

The five/one minute fallback should never be reached if all is going well, and the loop/process will stop as soon as the OK is received.

I am not aware of a definative way to tell in advance what response time the modem will give, and I would imagine that it will vary form modem to modem and depending on how many messages there are.
I suggest logging the datetime when you send the command and again when you receive the OK a few times to work out the most appropriate time for you.

Also, my final code had the timeout value set from the config file so it can be improved if needed.

If you improve on this code please post on the original thread as it gets a lot of hits.

Hope this helps
Rod

======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================





Similar Threads
Thread Thread Starter Forum Replies Last Post
Server Response Time sparun1607 Servlets 2 March 24th, 2008 07:59 AM
Determine if numeric Scootterp Access VBA 4 March 2nd, 2006 08:44 AM
Determine Credentials bmumph C# 2 November 1st, 2005 12:18 PM
Need Help: Can't determine cause of error xgbnow Visual C++ 3 September 22nd, 2003 05:00 PM
Determine if something has already been selected harpua Classic ASP Basics 1 June 13th, 2003 01:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.