Avid GPS reset/restart problem

7 views
Skip to first unread message

Alan Shapiro

unread,
Jul 16, 2008, 2:30:39 PM7/16/08
to AVIDdirector-M2M
I have a thread that reads the GPS. If it encounters an Exception, I
delete the thread and create a new one.

I forced an exception and it seems to work (killing and
restarting)10-30 minutes, then this happens:
inputstream is not null, closing
inputstream closed

outputstream is not null, closing
outputstream closed

serialPort is not null, closing
--------------------AND THERE IS NO MORE!!!!!




Here is the code for the cleanup. it seems to stop in the same place
each time.
protected void cleanup()
{
InputStream is = null;
OutputStream os = null;

try
{
if (serialPort != null)
{
is = serialPort.getInputStream();
os = serialPort.getOutputStream();
}

if (is != null)
{
DQUtil.consolePrint("inputstream is not null, closing");
is.close();
DQUtil.consolePrint("inputstream closed");
AvidUtil.waitHere(5);
}
else
{
DQUtil.consolePrint("inputstream null");
}
}
catch (Exception e)
{
}
finally
{
is = null;
try
{

if (os != null)
{
DQUtil.consolePrint("outputstream is not null, closing");
os.close();
DQUtil.consolePrint("outputstream closed");
AvidUtil.waitHere(5);
}
else
{
DQUtil.consolePrint("outputstream null");
}
}
catch (Exception e)
{
}
finally
{
os = null;

try
{
if (serialPort != null)
{
DQUtil.consolePrint("serialPort is not null, closing");
serialPort.close();
DQUtil.consolePrint("serialPort closed");
AvidUtil.waitHere(5);
}
else
{
DQUtil.consolePrint("serialPort null");
}
}
catch (Exception e)
{
}
finally
{
serialPort = null;
System.gc();
DQUtil.consolePrint("GPSAVID cleanup done");

}
}
}
}



Amber Ansari

unread,
Jul 16, 2008, 2:51:35 PM7/16/08
to aviddire...@googlegroups.com
Are you expecting to see the garbage collection happen and then the line
"GPSAVID cleanup done" to print to console, is that correct?
It is unclear when you see the messages appear. Are these messages you
posted here occuring when the thread is being closed or it is being
restarted? Does it take 10-30 minutes for these messages to appear?

Are you using the GPS_NMEA_RECEIVER class? If so, you should not need to
handle the serial port and parsing the GPS class will handle that and return
a GPSData object. Let me know if you need more information on this.
Also, from version 1.8.6 and higher the GPS class has a Continuous option
that can be run to automatically handle the reads and continuous reading
thread. This is available for applications that require high accuracy of GPS
data.
Just FYI, there is an AvidUtil.waitHereMs() that allows sleeping values in
milliseconds rather than seconds.

Amber Ansari.
(972)401-3655 x613.
AVIDwireless.

Alan Shapiro

unread,
Jul 16, 2008, 3:13:16 PM7/16/08
to AVIDdirector-M2M
This all started when I got a parsing error, and it hit an exception.
I have since fixed the parsing error, but am continuing to work at a
'fail-safe' object restart.

This is during the deletion(closing) of the thread.
This deletion and new instantiation happens about every 30 seconds (i
am forcing an Exception). It seems to work just fine, the messages,
including the "cleanup done" appear correctly, then, for no apparent
reason, it stops with the radio led=amber, app led=green, and system
led off. The snap console is unresponsive.

I am using a class I pared down from GPS_NMEA_RECEIVER. I have my own
application. If i can gracefully use the GPS_NMEA_RECEIVER object
gracefully, I'd be happy to use it. (I have my own watchdogs etc...).
I could even use the continuous read, though I am already doing
something like this.

I put the long waits in so I could read the console easier.

Thanks for all your help,
Alan Shapiro
Programmer, DQ Technologies



Amber Ansari

unread,
Jul 16, 2008, 3:36:50 PM7/16/08
to aviddire...@googlegroups.com
You can use the GPS_NMEA_RECEIVER class in your application. You will need
to start and stop it. You may not be able to set many of its configuration
as you are not starting it throug an M2MApp. But you will be able to get a
parsed reading. The JavaDocs have description of this class.

Also, does the AvidDirector become non-responsive after each thread cleanup
or it works correctly but fails after 10-30 minutes of running? At this
point does your watchdog timer catch this scenario and restart the
AvidDirector? If the avidDirector is indeed freezing it could be running out
of resources if there are objects still being left open. I would expect to
see an OutOfMemoryError when you are about to start a new instance of an
object. How do you close the thread before you start another one? Do you
toggle the power to the radio board when the thread is started?

Amber Ansari.
(972)401-3655 x613.
AVIDwireless.

-----Original Message-----
From: aviddire...@googlegroups.com
[mailto:aviddire...@googlegroups.com]On Behalf Of Alan Shapiro
Sent: Wednesday, July 16, 2008 2:13 PM
To: AVIDdirector-M2M

Rod Montrose[AVIDwireless]

unread,
Jul 16, 2008, 9:36:46 PM7/16/08
to AVIDdirector-M2M
(I renamed the subject since this is generic to the
javax.comm.SerialPort and nothing particular to do with GPS)

If you look at the com.avidwireless.radio.AT_Radio_Modem and
SerialPortRadio under closeSerialPort() method the code looks like:

try {
// close the Radio serial ports
if (serialPortIn != null) {
AvidUtil.clearInputStream(serialPortIn); // in AT_Radio_Modem
emptyInputBuffer() does same thing
serialPortIn.close();
serialPortIn = null;
if (isDebug) Logger.debugln(NAME,"serialPortIn Stopped");
}
if (serialPortOut != null) {
serialPortOut.flush();
serialPortOut.close();
serialPortOut = null;
if (isDebug) Logger.debugln(NAME,"serialPortOut Stopped");
}
// close the Radio serial port
if (serialPort != null) {
serialPort.removeEventListener();
if (isDebug) Logger.debugln(NAME,"serialPort removeEventListener");
AvidUtil.waitHere(3);
// close the Serial port and CommPortIdentifier
serialPort.close();
serialPort = null;
if (isDebug) Logger.debugln(NAME,"Closed");
serialPortOpen = false;
}
status = true;
}
catch (Exception e) {
Logger.log(NAME,e);
}
if (isDebug) Logger.debugln(NAME,"Stopped");

From AvidUtil

/**
* Empties the inputStream of any data in it, and returns the number
* of characters skipped over.
* <p>
* This will read until no more data is present in the inputstream
*
* @param inStream InputStream to skip ahead
* @return number of characters skipped
*/
public final static int clearInputStream(InputStream inStream) throws
java.io.IOException {
int inLen = 0;
int numAvailable;
while ((numAvailable = inStream.available()) > 0) {
inStream.skip(numAvailable);
inLen += numAvailable;
}
return inLen;
} // clearInputStream

The key point is to clear the serial streams before closing each
stream. Without doing this I have observed on occasion behavior like
you see when closing the SerialPort. There are 3 buffers: a 256 byte
buffer in the PSoC, a 255 byte buffer in the Imsys interrupt driver,
and a larger buffer (that's the size you set with the
setOutputBufferSize() method and defaults to 2K. Serial data is
transferred between the 3 buffers. I am guessing a byte can get
trapped between the buffers and one of the things the serialPort.close
does is to ensure all data is clear from the buffers and it waits for
this to get cleared, which it doesn't appear to do.

Also in your code when you use serialPort.getInputStream(); and
serialPort.getOutputStream(); it is important you ensure there are no
other objects keeping handles to these instances or their data won't
be marked to be garbage collected (though I don't think that has
anything to do with the problem you are seeing here). While Java is
much better than most OO languages, there are ways to have objects
persist on the heap when you don't expect them to. Joshua Bloch's
"Effective Java" Item 5: Eliminate obsolete object references explains
this very well. I highly recommend his book for how to write good code
for a device like the AVIDdirector-M2M.

Message has been deleted

Alan Shapiro

unread,
Jul 17, 2008, 5:16:36 AM7/17/08
to AVIDdirector-M2M
I am now using the GPS_NMEA_Receiver class. I call for an update every
10 seconds (rather than continuous).

The same thing happens. I can instantiate a reciever, and then kill
it. I can do this for about 20 minutes, then it stops suddenly, same
as before. I never get an out of memory error/Exception. I also do a
garbage collection fairly often, and I check available memory at that
time, and its doesnt seem to be significantly degraded, unless the
problem is fragmented memory. However I dont have this problem with
any other thread.

To clarify, I coded in an exception. the AvidDirector works after each
cleanup. Each cleanup and restart takes about 40 seconds. It starts
and stops for about 20 minutes or so, then it hangs while trying to
close the serialport.

I can start and stop every other thread for hours, (eg the serial port
connected to a Garmin GPS, or the modem, or any other
thread I have), with no problem.

Alan Shapiro
Programmer, DQ Technologies



Reply all
Reply to author
Forward
0 new messages