Problem connecting NXT With Pc

Post your NXJ projects, project ideas, etc here!

Moderators: roger, 99jonathan, imaqine

Problem connecting NXT With Pc

Postby rahat khan » Fri Aug 03, 2012 5:49 am

Hi All I'm trying to connect My NXt With PC here is my code ..
Code: Select all
import lejos.nxt.Motor;
import lejos.robotics.navigation.DifferentialPilot;   
import lejos.pc.comm.NXTConnector;


public class RoboClass {

public static void main (String args[]){

   
NXTConnector link = new NXTConnector();
   
     if (!link.connectTo("usb://"))
     {
        System.out.println("\nNo NXT find using USB");
        }
     
     //outData = link.getDataOut();
     

else
 {

 

System.out.println("\nNXT is Connected");





      DifferentialPilot pilot = new DifferentialPilot(2.1f,4.4f, Motor.A , Motor.C);

   
      int distance;
      distance= 10;      
      
      pilot.travel(distance);
      
      
      pilot.rotate(-360.00);
      pilot.travel(distance);
   
   
      
      distance=-1*distance;
      pilot.travel(distance);
      
   
      pilot.rotate(360.00);
      pilot.travel(distance);
   
      
}      
      
}
}


Every time I get "NO Nxt Find USing USB"

INFO I am Compiling this program by JAVAC
Added All the Jar Files in CLASSPATH
HAve my NXT turned On
using lejos 0.9.1 version


Please REply me Its Urgent :( :( :(
rahat khan
New User
 
Posts: 13
Joined: Mon Jul 30, 2012 8:07 am

Re: Problem connecting NXT With Pc

Postby gloomyandy » Fri Aug 03, 2012 7:41 am

Does nxjbrowse work to your NXT using a USB connection? If so use the lejos pc side command line tools to compile and run your program these will ensure that the class path and other parameters are set correctly. The tutorial has all of the details:
http://lejos.sourceforge.net/nxt/nxj/tu ... dRun.htm#6
Once you have things working then you can take a look at the nxj scripts and work out how to use the java commands directly if that is what you want to do...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Problem connecting NXT With Pc

Postby rahat khan » Fri Aug 03, 2012 7:58 am

yeah nxjbrowse showing my NXT brick and its connecting it .. I just want to have a simple code that can connect to my nxt brick and then finally i'll be able to run my wriiten code on nxt please do help me

and should there be a program run on nxt while we are trying to connect with the PC ?? please help me I'm really confused :?
rahat khan
New User
 
Posts: 13
Joined: Mon Jul 30, 2012 8:07 am

Re: Problem connecting NXT With Pc

Postby gloomyandy » Fri Aug 03, 2012 8:22 am

No need to run a program on the NXT if you are using LCP to control your robot (which it looks like you are). Why not take a step back and try out some of the sample code provided in the pcsamples directory (things like the sensortest sample - see below). Follow the tutorial for how to build and run it. As you will see from the code there is no need to open the connection yourself to the nxt, the lejos classes will do that for you...

http://lejos.svn.sourceforge.net/viewvc ... iew=markup

Oh and why is this so urgent?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Problem connecting NXT With Pc

Postby rahat khan » Fri Aug 03, 2012 8:25 am

thnx alot for your replies actually i m doing internship somewhere and my duration has been over this is why it is so urgent because i've to submit report on time

And one thing more can you please let me know that why the code i've posted isn't working??
rahat khan
New User
 
Posts: 13
Joined: Mon Jul 30, 2012 8:07 am

Re: Problem connecting NXT With Pc

Postby rahat khan » Fri Aug 03, 2012 8:27 am

and yes I m going to try some sample programs as well :) aND wait wait wait

Just please guide me that which classes to use and which packages to import for the connection of nxt with the pc ... And the program runs and compiles on PC

And I'm compiling and running it on command line not in eclipse and the code is wriiten in Notepad not on Eclipse IDE.....
rahat khan
New User
 
Posts: 13
Joined: Mon Jul 30, 2012 8:07 am

Re: Problem connecting NXT With Pc

Postby rahat khan » Fri Aug 03, 2012 8:43 am

ANd these are the errors i am getting


Code: Select all
USBSend Log.listener: Error: Failed to load USB comms driver.
USBSend Log.listener: Caused by lejos.pc.comm.NXTCommException: Cannot load NXTC
omm driver
USBSend Log.listener:   at lejos.pc.comm.NXTCommFactory.newNXTCommInstance(NXTCo
mmFactory.java:110)
USBSend Log.listener: Caused by lejos.internal.jni.JNIException: library jfantom
.dll was not found in D:\Program Files\axis-bin-1_4\axis-1_4\lib\native, archite
cture windows/x86
USBSend Log.listener:   at lejos.internal.jni.JNILoader.loadLibrary(JNILoader.ja
va:127)
USBSend Log.listener: Failed to find any NXTs
USBSend Log.listener: Failed to connect to any NXT
No NXT found using USB


while compiling this code


Code: Select all
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.pc.comm.NXTCommLogListener;
import lejos.pc.comm.NXTConnector;
 
/**
 * This is a PC sample. It connects to the NXT, and then
 * sends an integer and waits for a reply, 100 times.
 *
 * Compile this program with javac (not nxjc), and run it
 * with java.
 *
 * You need pccomm.jar on the CLASSPATH and the jfantom.dll
 * DLL or liblibnxt.so shared library on the Java library path.
 *
 * Run the program by:
 *
 *   java USBSend
 *
 * Your NXT should be running a sample such as USBReceive.
 *
 * @author Lawrie Griffiths
 *
 */
public class USBSend {   
   public static void main(String[] args) {
      NXTConnector conn = new NXTConnector();
      
      conn.addLogListener(new NXTCommLogListener(){

         public void logEvent(String message) {
            System.out.println("USBSend Log.listener: "+message);
            
         }

         public void logEvent(Throwable throwable) {
            System.out.println("USBSend Log.listener - stack trace: ");
             throwable.printStackTrace();
            
         }
         
      }
      );
      
      if (!conn.connectTo("usb://")){
         System.err.println("No NXT found using USB");
         System.exit(1);
      }
      
      DataInputStream inDat = new DataInputStream(conn.getInputStream());
      DataOutputStream outDat = new DataOutputStream(conn.getOutputStream());
      
      int x = 0;
      for(int i=0;i<100;i++)
      {
         try {
            outDat.writeInt(i);
            outDat.flush();
   
         } catch (IOException ioe) {
            System.err.println("IO Exception writing bytes");
         }
          
         try {
               x = inDat.readInt();
           } catch (IOException ioe) {
              System.err.println("IO Exception reading reply");
           }           
           System.out.println("Sent " +i + " Received " + x);
      }
      
      try {
         inDat.close();
         outDat.close();
         System.out.println("Closed data streams");
      } catch (IOException ioe) {
         System.err.println("IO Exception Closing connection");
      }
      
      try {
         conn.close();
         System.out.println("Closed connection");
      } catch (IOException ioe) {
         System.err.println("IO Exception Closing connection");
      }
   }
}
rahat khan
New User
 
Posts: 13
Joined: Mon Jul 30, 2012 8:07 am

Re: Problem connecting NXT With Pc

Postby skoehler » Fri Aug 03, 2012 9:20 am

rahat khan wrote:ANd these are the errors i am getting


Code: Select all
USBSend Log.listener: Error: Failed to load USB comms driver.
USBSend Log.listener: Caused by lejos.pc.comm.NXTCommException: Cannot load NXTC
omm driver
USBSend Log.listener:   at lejos.pc.comm.NXTCommFactory.newNXTCommInstance(NXTCo
mmFactory.java:110)
USBSend Log.listener: Caused by lejos.internal.jni.JNIException: library jfantom
.dll was not found in D:\Program Files\axis-bin-1_4\axis-1_4\lib\native, archite
cture windows/x86
USBSend Log.listener:   at lejos.internal.jni.JNILoader.loadLibrary(JNILoader.ja
va:127)
USBSend Log.listener: Failed to find any NXTs
USBSend Log.listener: Failed to connect to any NXT
No NXT found using USB



So when you are running the code you provided, pccomm.jar tries to locate the narive libraries. You must have copied pccomm.jar to D:\Program Files\axis-bin-1_4\axis-1_4\lib but you obviously forgot to copy the native folder from you leJOS installation.
skoehler
leJOS Team Member
 
Posts: 1112
Joined: Thu Oct 30, 2008 4:54 pm

Re: Problem connecting NXT With Pc

Postby gloomyandy » Fri Aug 03, 2012 9:27 am

Why are you running that sample rather then the one I recommended? The sample I recommended is much closer to the code that you originally posted in terms of what it does. As I said before if you are using the remote NXT API to control motors etc. there is no need to open a connection explicitly, the leJOS classes will take care of that for you. Why are you not using the standard lejos scripts nxjpcc and nxjpc to compile and run the samples?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Problem connecting NXT With Pc

Postby rahat khan » Fri Aug 03, 2012 10:23 am

I've given that path of jfantom.dll and its working nicely my half of the work is done ... but now i've modified the code to
Code: Select all
import lejos.pc.comm.NXTCommLogListener;
import lejos.pc.comm.NXTConnector;
import lejos.nxt.Motor;
import lejos.robotics.navigation.DifferentialPilot;   


public class USBSendRobo {   
   
   public static void main(String[] args) {
      
      NXTConnector conn = new NXTConnector();
      
      conn.addLogListener(new NXTCommLogListener()

      {

         public void logEvent(String message)

         {
            System.out.println("USBSend Log.listener: "+message);
            
         }

         public void logEvent(Throwable throwable)

         {
            System.out.println("USBSend Log.listener - stack trace: ");
             throwable.printStackTrace();
            
         }
         
      }
      );
      
      
      
      if (!conn.connectTo("usb://"))

      {
         System.err.println("No NXT found using USB");
         System.exit(1);
      }
      
            
      else {



         System.out.println("\nNXT is Connected");

         DifferentialPilot pilot = new DifferentialPilot(2.1f,4.4f, Motor.A , Motor.C);

   
         int distance;
         distance= 10;      
      
         pilot.travel(distance);
      
      
         pilot.rotate(-360.00);
         pilot.travel(distance);
   
   
      
         distance=-1*distance;
         pilot.travel(distance);
      
   
         pilot.rotate(360.00);
         pilot.travel(distance);
   
   


         //conn.close();
         //System.out.println("Closed connection");
         
   
      }





      
}      
}

it is connecting NXT but failed to open the connection with these lines on console
Code: Select all
Found NXT: NXT 00165310AEBB

NXT is Connected
Found NXT: Unknown 00165310AEBB
Failed to open connection to the NXT




whats happening wrong here ??? ??


And To Skoehler

I've tried it with nxjcpp but it the compiler is not being found should i've to set the PATH variable for the lejos bin Directory ???
rahat khan
New User
 
Posts: 13
Joined: Mon Jul 30, 2012 8:07 am

Re: Problem connecting NXT With Pc

Postby gloomyandy » Fri Aug 03, 2012 10:53 am

Please read my previous comments. If you are using the PC side leJOS API (things like pilot.rotateTo etc.), you do not need to opena connection to the NXT. The leJOS classes will do that for you. The reason you are getting that error is that you are now opening two connections to the same NXT, one that you have opened, and one that the leJOS classes are trying to open.

Please look at the sample code I pointed you at earlier. See how it does not have any code to open a connection? That is what your code should look like...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Problem connecting NXT With Pc

Postby skoehler » Fri Aug 03, 2012 1:49 pm

rahat khan wrote:I've tried it with nxjcpp but it the compiler is not being found should i've to set the PATH variable for the lejos bin Directory ???

I assume that LEJOS_NXT_JDK_HOME is not set. If you use a recent leJOS version, the batch/shell scripts like nxjpcc even print warnings about a misconfigured JAVA_HOME or LEJOS_NXT_JAVA_HOME.
skoehler
leJOS Team Member
 
Posts: 1112
Joined: Thu Oct 30, 2008 4:54 pm

Re: Problem connecting NXT With Pc

Postby skoehler » Fri Aug 03, 2012 1:53 pm

gloomyandy wrote:Please read my previous comments. If you are using the PC side leJOS API (things like pilot.rotateTo etc.), you do not need to opena connection to the NXT. The leJOS classes will do that for you. The reason you are getting that error is that you are now opening two connections to the same NXT, one that you have opened, and one that the leJOS classes are trying to open.


Another possibility is to open the connection manually, and then call NXTCommandConnector.setNXTCommand(new NXTCommand(conn.getNXTComm())). The setNXTCommand methods sets the NXTCommand object used by Motor.A, etc.
He just needs to add that line after System.out.println("\nNXT is Connected");
skoehler
leJOS Team Member
 
Posts: 1112
Joined: Thu Oct 30, 2008 4:54 pm

Re: Problem connecting NXT With Pc

Postby rahat khan » Sat Aug 04, 2012 4:08 am

Thank you Gloomyandy and Skoehler :mrgreen:

YOu guys are greattttt!! :!:

It has been accomplished by the way you told me Gloomyandy So I didn't need to do that the way you told me Skoehler :)

I was and am fool that i wasn't getting your point :oops: :D

I must again say !! you guys are awesome and very supportive Thanks a lot again :)


This Is My Code And It's Working perfectly

Code: Select all



import lejos.nxt.Motor;
import java.util.Scanner;
import lejos.robotics.navigation.DifferentialPilot;

public class PcMoveR {

   
   public static void main(String[] args) {
      
      
      DifferentialPilot pilot= new DifferentialPilot(2.1f,4.4f, Motor.A, Motor.C);
      
      Scanner key= new Scanner(System.in);
      
      
      
   
         
      
      System.out.println("Do you want to drive?");
      System.out.println("Press Y for Yes");
      System.out.println("Press N for No");
      
      
      String chk1;
      char a ;
   
      chk1= key.next();
      
       a = chk1.charAt(0);
      
      if(a=='y' || a=='Y')
      {
         
         
      do {
      
      System.out.println("Welcome to the driving seat :");
      System.out.println("Press F for car to move forward :");
      System.out.println("Press B for car to move bacward :");
      System.out.println("Press L for car to move Left :");
      System.out.println("Press R for car to move Right :");
      System.out.println("Press N to exit driving  :");
      System.out.println("________________________________");
      System.out.println("________________________________");
      //System.out.println("/n");
      
      String  rotation ;
      rotation=key.next();
      
      char rotate=rotation.charAt(0);
      float cm;
      
      switch(rotate){
      
   case 'F':
   case 'f':
            
      System.out.println("How much distance you want to cover in Centimeters ?");
       cm=key.nextFloat();
      
         pilot.travel(cm);
         
         break;
         
   case 'B':
   case 'b':
         
      System.out.println("How much distance you want to cover in Centimeters ?");
       cm=key.nextFloat();
      cm=-1*cm;
      pilot.travel(cm);
      
         break;
   case 'L':
   case 'l':
      
      System.out.println("How much distance you want to cover in Centimeters ?");
       cm=key.nextFloat();
       pilot.rotate(-360.00);
         pilot.travel(cm);
      
         break;
         
   case 'R':
   case 'r':
   
   System.out.println("How much distance you want to cover in Centimeters ?");
    cm=key.nextFloat();
    pilot.rotate(360.00);
      pilot.travel(cm);
   
      break;
      
   case 'N':
   case 'n':
      
      System.out.println("The program is Exiting");
      System.exit(0);
   
   default:
      
      System.out.println("Invalid Direction command...!!");
      
      break;
      
      
      
   //   System.out.println("It has travelled "+pilot.getMovement().getDistanceTraveled()+" distance");
      
   
      
      
      }
      
    }
   
    while(a=='y'|| a=='Y');
   
    System.out.println("Do you want to continue drive?");
      System.out.println("Press Y for Yes");
      System.out.println("Press N for No");
   
       chk1= key.next();
      
       a = chk1.charAt(0);
      
      }
      
   
      
   else {
   
      System.out.println("The program is Exiting");
      System.exit(0);
      
   }      
   
   }
   }
   






If Anybody wants to ask anything about it ,free to ask !!

In the End Again Big Thanks to you guyz Gloomyandy and Skoehler :)
rahat khan
New User
 
Posts: 13
Joined: Mon Jul 30, 2012 8:07 am


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff