Project

General

Profile

Check if device is still active

Added by lucase about 9 years ago

Dear support,

I'm trying to implement requirement that application is going to check after specified interval if connection to device (phone, modem) is still active.
For example if bluetooth phone is connected and someone takes phone and walk away outside bluetooth range application will still be in a "connected mode".
Is there a way to ping device?

Kind regards,
Lucas


Replies (40)

RE: Check if device is still active - Added by admin about 9 years ago

Hi,

The library already have a watchdog that sends a heart beat check to the modem at periodic internal. You can try to use the event WatchDogFailed which will be invoked when the modem is disconnected.

Thanks
Regards
admin

RE: Check if device is still active - Added by lucase about 9 years ago

Hello,

I've tried watchdog, but after 1 minute nothing happened. What interval is set for watchdog?
Testing method was based on physical device switch off.

Regards,
Lucas

RE: Check if device is still active - Added by lucase about 9 years ago

What more, if gsm modem is removed while connected, there is no way to connect again: requested resource is in use.

RE: Check if device is still active - Added by admin about 9 years ago

Hi,

Try setting config.WatchDogInterval to a shorter period and see if it helps, e.g.

// Set watch dog interval to 5 seconds. Default to 60 seconds
config.WatchDogInterval = 5000;

You can use ValidateConnection to check the connection. This method relies on the watchdog to detect if connection is valid

if (mobileGateway.ValidateConnection())
{
  MessageBox.Show("Gateway is connected", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
  MessageBox.Show("Gateway is disconnected", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

If you unplug the modem, and plug it back. Once you issue any command, e.g. to retrieve modem information, it should automatically try to connect again.

Thanks
Regards
admin

RE: Check if device is still active - Added by lucase about 9 years ago

Hello,

I've tried to set lower watchdog interval but with no effect related to WatchDogFailed event.
ValidateConnection() is working fine, but when i shutdown modem and then turn on nothing happens. Still Gateway is disconnected (no auto connection).

Another way is to do ValidateConnection every X seconds as written below.

timerTest = new System.Threading.Timer(new System.Threading.TimerCallback(delegate(Object state)
            {
                try
                {
                    if (mobileGateway.ValidateConnection())
                    {
                        Console.WriteLine("Gateway is connected", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        Console.WriteLine("Gateway is disconnected " + (++timerTestcounter), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        mobileGateway = messageGateway.Find(config);

                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }), null, 0, 2000);

Without mobileGateway = messageGateway.Find(config);
everything is working fine - I know exact moment when modem is disconnected.
When I'm using my autoconnect (just messageGateway.Find) something unexpected happens:
A first chance exception of type 'System.IO.IOException' occurred in dll
The requested resource is in use.

There is nothing to keep resource in use. When I switch off and run again application there is no problem with establishing connection with modem but restart is not an option.

RE: Check if device is still active - Added by admin about 9 years ago

Hi,

Try replacing

mobileGateway = messageGateway.Find(config);

with

mobileGateway = messageGateway.SendCommand("AT");

Thanks
Regards
admin

RE: Check if device is still active - Added by lucase about 9 years ago

Hello again,

I've tried

else
                    {
                        Console.WriteLine("Gateway is disconnected " + (++timerTestcounter), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        mobileGateway.SendCommand("AT");;

                    }


and for the first time it worked fine. In order to check functionality I've tried to send SMS from another phone to modem. Nothing happened. I've restarted app and this time mobileGateway.SendCommand("AT"); didn't do any good (no auto reconnect).

In attachment you will find logs with level VERBOSE.

Regards,
Lucas

RE: Check if device is still active - Added by admin about 9 years ago

Hi,

Are you using .NET 3.5 or 4.0?

Thanks
Regards
admin

RE: Check if device is still active - Added by lucase about 9 years ago

Hello,

I'm using .NET version 4.0

Regards
Lucas

RE: Check if device is still active - Added by admin about 9 years ago

Hi,

I am guessing that when you unplug and plug in the modem again, all the settings are reset to default. So whenever you plug in the modem again, after

mobileGateway.SendCommand("AT");

Try to enable whatever features you need again, e.g. to enable incoming message notification..

Thanks
Regards
admin

RE: Check if device is still active - Added by lucase about 9 years ago

that seems to be the right way, but I can't get it working. It worked only once. I didn't change anything and it stopped working.
Is library somehow blocking access to particular port? After restarting application I can connect to modem without "resource is busy".

RE: Check if device is still active - Added by lucase about 9 years ago

I've tried MessagingToolkit.Core.Utilities.exe same result: no automatic reconnection.
When I'm trying to connect again same error:
---------------------------
MessagingToolkit.Core.Utilities
---------------------------
The requested resource is in use.
---------------------------
OK
---------------------------

RE: Check if device is still active - Added by lucase about 9 years ago

Another test:
1. first MessagingToolkit.Core.Utilities application working + modem connected
2. modem switched off
3. first application response for AT command:
Error in sending command at : The port is closed.
4. second MessagingToolkit.Core.Utilities run
5. same error:

---------------------------
MessagingToolkit.Core.Utilities
---------------------------
The requested resource is in use.
---------------------------
OK   
---------------------------

6. first application closed
7. second application: gateway connected

RE: Check if device is still active - Added by admin about 9 years ago

Hi,

I have expose the Dispose method of mobile gateway. Please use the attached patch.

What you can do is that now you can call the Dispose method to release the resources used by the gateway now.

Thanks
Regards
admin

RE: Check if device is still active - Added by lucase about 9 years ago

Hello,

I've checked library and

mobileGateway.Dispose();

didn't do any good. Does Dispose contain for example port release or any other cleanup stuff?

Maybe messageGateway is the source of our problems?

Regards,
Lucas

RE: Check if device is still active - Added by admin about 9 years ago

Hi,

messageGateway uses SerialPort to connect to the modem, and the Dispose method releases the port by calling SerialPort.Dispose method.

We have tried the same scenario using the Core.Utilities program and it does not cause any problem for us.

Perhaps can you share with us your code snippet and we will see if we can produce the scenario on our side.

Thanks
Regards
admin

RE: Check if device is still active - Added by lucase about 9 years ago

Hello,

we can assume that I'm using MessagingToolkit Demo source code. There is sequence of steps that I'm using in order to check if it's working:
1. Connect modem
2. Disable/ Switch Off/ Remove modem
3. Disconnect button
4. Connect / Turn on modem
5. Connect Messaging application demo to modem
I'm using two modems for tests - first one is located at the main board Qualcomm GOBI 2000, second one is connected via USB: HUAWEI E180.
Second one is working and is able to connect without application restart (all steps passed ok) - problem is only when modem is removed and I'm trying to check connection - mobileGateway.ValidateConnection. It gives always valid connection...
First modem gives proper mobileGateway.ValidateConnection check: if modem is switched off or physically shutdown validation gives information that modem is not connected. Problem with first one is that when I'm trying to reconnect it gives The requested resource is in use. response.

Hope this helps, because I really don't know where to look for solution...

Regards,
Lucas

RE: Check if device is still active - Added by lucase about 9 years ago

Another hint.

I'm running two applications: MessagingToolkit DEMO and my own, which checks if port is available:

SerialPort serialPort = new SerialPort();
            //Thread.Sleep(1000);
            string[] ports = SerialPort.GetPortNames();
            serialPort.PortName = "COM5";
            serialPort.BaudRate = 115200;
            serialPort.DataBits = 8;
            serialPort.StopBits = System.IO.Ports.StopBits.One;
            serialPort.Parity = System.IO.Ports.Parity.None;
            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }
            serialPort.Open();
            System.Threading.Thread.Sleep(1000);
            serialPort.Close();

1. At the beginning port is open and available.
2. MessagingToolkit Demo connects to COM5 modem Qualcomm
3. Second application gives: The process cannot access the port 'COM5' because it is being used by another process.
4. Modem switched off / disconnected
5. Second application gives: The requested resource is in use.
6. Modem is turned on
7. First application gives: The requested resource is in use.
8. First application is closed
9. Second application is able to connect to COM port

RE: Check if device is still active - Added by lucase about 9 years ago

For Qualcomm GOBI
I've written application which is sending AT command (but this is not crucial). At first I've commented lines for receiving messages

//serialPort.DataReceived += new SerialDataReceivedEventHandler(delegate(object sender, SerialDataReceivedEventArgs e)
//{
// Console.WriteLine((++counter) + "\tData received");
//});

Console output was:
1 Port Open
1 Port Close
2 Port Open
2 Port Close
3 Port Open
3 Port Close
4 Port Open
4 Port Close
5 Port Open
5 Port Close
6 Port Open
6 Port Close
7 Port Open
-----------MODEM DISCONNECTED
A first chance exception of type 'System.IO.IOException' occurred in System.dll
7 Port Close
The requested resource is in use.

A first chance exception of type 'System.IO.IOException' occurred in System.dll
The requested resource is in use.

A first chance exception of type 'System.IO.IOException' occurred in System.dll
The requested resource is in use.
-----------MODEM CONNECTED
8 Port Open
8 Port Close
9 Port Open
9 Port Close
10 Port Open

so we see that The requested resource is in use. occured but wasn't blocking for all the time.
Next test was with data receive uncommented. Result: till The requested resource is in use. was the same. At this time point modem was connected. Couple of "requested resource is in use" and application crashed.
I've changed the line: from serialPort.Write("ATD09364191752"); to if(serialPort.IsOpen) serialPort.Write("ATD09364191752"); and it was connected again, application didn't crash with error pointing null reference.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace ImioGenerator
{
    public class Tests
    {

        public Tests()
        {

        }

        private System.Threading.Timer timerTest;
        private object block = new object();
        private long counter = 0;
        public void StartThread()
        {
            timerTest = new System.Threading.Timer(new System.Threading.TimerCallback(delegate(Object state)
            {
                lock (block)
                {
                    try
                    {
                        SerialPort serialPort = new SerialPort();
                        //Thread.Sleep(1000);
                        string[] ports = SerialPort.GetPortNames();
                        serialPort.PortName = "COM5";
                        serialPort.BaudRate = 115200;
                        serialPort.DataBits = 8;
                        serialPort.StopBits = System.IO.Ports.StopBits.One;
                        serialPort.Parity = System.IO.Ports.Parity.None;
                        if (serialPort.IsOpen)
                        {
                            serialPort.Close();
                        }
                        serialPort.DataReceived += new SerialDataReceivedEventHandler(delegate(object sender, SerialDataReceivedEventArgs e)
                        {
                            Console.WriteLine((++counter) + "\tData received");
                        });
                        serialPort.Open();
                        Console.WriteLine(+ (++counter) + "\tPort Open");
                        serialPort.Write("ATD09364191752");
                        System.Threading.Thread.Sleep(2000);
                        serialPort.Close();
                        Console.WriteLine((counter) + "\tPort Close ");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }), null, 0, 2000);
        }
    }
}

For HUAWEI E180
This was interesting test because it didn't recognize that modem was disconnected. Application was still able to open and close port, but didn't receive any messages It was just Port Open -> Port Close. When I plugged in modem again there was additional line with message receive - Data received.

Bottom line is that for all modems we need to have watchdog which will check if modem is returning anything after AT command and we still have a problem with library not being able to connect again...

Sorry for the amount of materials, but I really would like to get that working...

Regards,
Lucas

RE: Check if device is still active - Added by lucase about 9 years ago

and last but not least,

I think, that HUAWEI driver was able to hold some requests sent by test aplication, because when I connected HUAWEI modem and port was opened modem returned couple of "Data received" literals between "Port open" and "Port closed"

RE: Check if device is still active - Added by admin about 9 years ago

Hi,

Can you try with the attached patch and send us back the log file? Please invoke the Dispose method before you reconnect to modem.

Thanks
Regards
admin

RE: Check if device is still active - Added by admin about 9 years ago

Also in your program, after serialPort.Close() try to explicity call serialPort.Dispose() to see if it helps.

RE: Check if device is still active - Added by lucase about 9 years ago

Hello,

thank you for a quick answer, but still same error:
---------------------------
MessagingToolkit.Core.Utilities
---------------------------
The requested resource is in use.

Below you will find the code which is used for disconnect button. I've added the line with dispose method.


            try
            {
                if (mobileGateway != null)
                {
                    btnDisconnect.Enabled = false;
                    if (mobileGateway.Port.IsOpen) Console.WriteLine("port open");
                    if (mobileGateway.Disconnect())
                    {
                        mobileGateway.Dispose();
                        mobileGateway = null;
                        mobileGateway = MobileGatewayFactory.Default;
                        MessageBox.Show("Gateway is disconnected successfully", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

Test steps:
1. Connect mobileGateway to modem
2. Switch off modem
3. Disconnect button
4. Connect button couple of times (after minute, after 10... no difference)

Regards,
Lucas

RE: Check if device is still active - Added by admin about 9 years ago

Hi,

I have tried to reproduce your problem on my side using a few modems (Huawei, Siemen Mc35i, a few handsets) but unfortunately i cannot reproduce it.

Once I unplug and plug in the modem again, I have no problem connecting to it using any of the 2 instances of program that I started.

1. Start 2 instances of the demo program
2. 1st instance connects to modem
3. Unplug and plug in modem
4. 2nd instance can connect to modem with problem. (Actually both can connect to the modem, even without calling Disconnect).

Attached is the demo program I used. Do you notice any errors in the log file?

RE: Check if device is still active - Added by lucase about 9 years ago

Hello,

I've follow the steps and in the end got the same error.

---------------------------
MessagingToolkit.Core.Utilities
---------------------------
The requested resource is in use.
---------------------------
OK
---------------------------
I think connection depends on driver. Some drivers could handle several connections, some don't.

---------------------------
MessagingToolkit.Core.Utilities
---------------------------
The process cannot access the port 'COM5' because it is being used by another process.
---------------------------
OK
---------------------------

In order to connect to modem I need to close first instance.

I hope we will find a solution, because application restart is not an option :(.

Regads,
Lucas

(1-25/40)