To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.javaOpen lugnet.robotics.rcx.java in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / Java / 309
308  |  310
Subject: 
Re: Opening COM port error
Newsgroups: 
lugnet.robotics.rcx.java
Date: 
Sat, 29 Nov 2003 09:12:34 GMT
Viewed: 
5782 times
  
In lugnet.robotics.rcx.java, Tyler Derkin wrote:
Thanks for your reply. You're reply confirmed my suspicion about leaving the
port object lying around.

I think I'll try your first suggestion of 'deleting' the port object at the end
of each routine. I hope you dont mind me asking but how do you explicitly
'delete' an object(Im still a Java newbie)? As I understand, Java takes care of
that for you and I was looking through Sun's Java tutorials which also say that
Java takes care of that for you through it's garbage collection mechanism.

As for the reason I'm not creating only one port object, well, I'm just trying
to make life easier for myself as I'm calling these methods through JNI and I
thought having distinct methods with their own data and implementation would
just do the job.

Ahh, Garbage Collection.

<rant soapbox="on" eyes="glazed" dinosaur="true">
In my mind the worst thing that has happened to modern compiler languages is
excessive reliance on Garbage Collection.  This is analogous to leaving
half-empty soda cans all over the place on the assumption that someone will come
along and pick them up to redeem the nickel.  Regardless of how often I read
about how great GC is, I'll never believe that this is a positive development in
the evolution of programming languages.
</rant>

Ok, sorry about that, I had slipped into C++ mode when I originally answered
your question.  GC is never guaranteed to happen, and can't even reliably be
suggested to the Java VM.  You can't rely on GC to clean up the 'port' object
and close the COM port like this.  You can't even rely on your objects ever
being finalized, so the COM port is likely to remain open for the duration of
your program.

If your RCXPort class has its own explicit Close() method, simply call it after
your dos.flush(); statement.  If it doesn't, you'll have to define class members
to hold references to your port and stream objects, and a helper method to 'new'
them up as you do at the top of each function.  But only call 'new' if the
objects are not already initialized, ie: their values are not null.  Then call
this function at the top of your forward() and reverse() methods.

My Java is a bit rusty, and I don't know if this will work in the scenario that
you describe, but it would look something like this:

// ~~~~~~~  Start of Java code  ~~~~~~~

protected RCXPort port;

protected InputStream is;
protected OutputStream os;
protected DataInputStream dis;
protected DataOutputStream dos;

public static void getPort()      // Got milk?
{
try {

      if (null == port) {
         port = new RCXPort();

         is = port.getInputStream();
         os = port.getOutputStream();
         dis = new DataInputStream(is);
         dos = new DataOutputStream(os);
       }
    }
    catch (Exception e) {
    }
}

public static void forward( int distance )
{
try {
      getPort();       // Open the RCXPort if we haven't already

      byte[] dt = { 3, (byte)distance};

      dos.write(dt);
      dos.flush();     // You can probably delete this line of code
    }
    catch (Exception e) {
    }
}

// Do the same for reverse()...

// ~~~~~~~  End of Java code  ~~~~~~~

You could also call getPort() in the class constructor, but it is considered bad
form to do any operation that could fail in a constructor since there is no way
(short of throwing and exception) to indicate to the caller that the operation
failed.

This is why I disagree with the common assertion in Java programming texts that
GC is so cool.  This is one of those language features that lulls you into a
false sense of security and then jumps out from behind a rock every so often to
whap you upside the head.  Then you're forced to jump through all kinds of hoops
to solve subtle problems that you haven't been trained to anticipate.  Better to
just be in the habit of dropping that soda can into the recycle bin yourself.

Oops, I slipped into <rant> mode again there without a tag.  I hope this helps!

- Chris.

p.s.  I know this is good code because I wrote it at 4:00am without running it
through a compiler.



Message has 1 Reply:
  Re: Opening COM port error
 
In lugnet.robotics.rcx.java, Chris Phillips wrote: Hahaha, Your passionate condemnation of GC makes entertaining reading in light of the amount of work I have to do with lejos,Java,C++, etc for my final year undergraduate project. I will certainly (...) (21 years ago, 30-Nov-03, to lugnet.robotics.rcx.java)

Message is in Reply To:
  Re: Opening COM port error
 
(...) Hi Chris, Thanks for your reply. You're reply confirmed my suspicion about leaving the port object lying around. I think I'll try your first suggestion of 'deleting' the port object at the end of each routine. I hope you dont mind me asking (...) (21 years ago, 29-Nov-03, to lugnet.robotics.rcx.java)

6 Messages in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR