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 / 308
307  |  309
Subject: 
Re: Opening COM port error
Newsgroups: 
lugnet.robotics.rcx.java
Date: 
Sat, 29 Nov 2003 01:24:12 GMT
Viewed: 
5588 times
  
In lugnet.robotics.rcx.java, Chris Phillips wrote:
In lugnet.robotics.rcx.java, Tyler Derkin wrote:
Hi guys,

First of all, thanx Brian, but I managed to solve the weird firmdl.bat problem
by fooling around with different batteries and IR range levels.

Secondly,I'm trying to send data packets to the RCX through java methods called
from a C++ program (I know it's stupid but dont ask why). Here are two
implementations which tells the robot to move forward and backward:

public static void forward( int distance )
{
try {

      RCXPort port = new RCXPort();

      InputStream is = port.getInputStream();
      OutputStream os = port.getOutputStream();
      DataInputStream dis = new DataInputStream(is);
      DataOutputStream dos = new DataOutputStream(os);

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

      dos.write(dt);
      dos.flush();

    }
    catch (Exception e) {
    }
}

public static void reverse( int distance )
{
try {

      RCXPort port = new RCXPort();

      InputStream is = port.getInputStream();
      OutputStream os = port.getOutputStream();
      DataInputStream dis = new DataInputStream(is);
      DataOutputStream dos = new DataOutputStream(os);

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

      dos.write(dt);
      dos.flush();

    }
    catch (Exception e) {
    }
}

I know the implementation isnt very elegant but I want to keep the "forward" and
"reverse" methods separate, hence, the almost identical definitions.

Here's the problem: After calling "forward", when I try to call "reverse", I get
a COM port error which says "Error 5:Opening COM5". I'd figure that I need to
close the stream after "dos.flush()" with "dos.close()" before calling "reverse"
but I still have the same problem. I think I need to not only close the stream
but the port as well but I'm not sure of how to do this.

Any ideas anyone? Many thanks.

Tyler

This probably is an issue of leaving your Port object around after you're done
using it.  You have a memory leak in each routine, because you call 'new
RCXPort();' without ever calling 'delete port;' on the object.  You should
definitely call 'delete' after you have called 'dos.flush();' in each of these
routines.

Unfortunately, this may not be enough to solve your problem, depending on the
implementation of the RCXPort class and also on your Java VM.  You might be
better off creating a single RCXPort() object when you first start your program,
store a pointer to it in a global variable, and delete it only once, when your
program terminates.  This will make your program slightly more efficient, and
will avoid many potential issues where you might inadvertently try to use the
same physical COM port with multiple RCXPort objects.

Hope this helps!

- Chris.

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 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.

Thanks again.

Tyler



Message has 1 Reply:
  Re: Opening COM port error
 
(...) 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 (...) (21 years ago, 29-Nov-03, to lugnet.robotics.rcx.java)

Message is in Reply To:
  Re: Opening COM port error
 
(...) This probably is an issue of leaving your Port object around after you're done using it. You have a memory leak in each routine, because you call 'new RCXPort();' without ever calling 'delete port;' on the object. You should definitely call (...) (21 years ago, 28-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