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 / 307
306  |  308
Subject: 
Re: Opening COM port error
Newsgroups: 
lugnet.robotics.rcx.java
Date: 
Fri, 28 Nov 2003 18:18:18 GMT
Viewed: 
5477 times
  
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.



Message has 1 Reply:
  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)

Message is in Reply To:
  Opening COM port error
 
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++ (...) (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