[solved] Bluetooth: Sending File from NXT to NXT using LCP

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc

Moderators: roger, 99jonathan, imaqine

[solved] Bluetooth: Sending File from NXT to NXT using LCP

Postby astral » Mon Jan 26, 2009 2:24 pm

Hi NXT'maniacs ;p:)

I have some problems with using a LCP commands to send file from NXT to another NXT.

Q: Do anybody send succesly file from nxt to another nxt ?

My problem:
Legend:
NXTBT - wants send a file
NXTRC - wants to recive a file

I wrote simple code, usung standard LCP commands to send file, but only what is working is "creating a file" on NXTRC ( with 0 file lenght ) with OpenWrite Command - afert sending this Command, NXTBT recive FileHandle in return package from NXTRC and that is OK , but when NXTRC wants to send WriteCommand - there is somethings wrong, becouse NXTRC holds up :/ and freezing, and do nothing.

Simply: I have problem with sending file from nxt to nxt... :/, and I need this for my project :/

I'm usung LeJOS 0.7 with 0.7 firmware.

Code - send file method (is similar to lejos.pc..tools.SendFile ):
Code: Select all
   public void sendFile(String filename) throws IOException {
         setVerify(true);

         File file = new File(filename);
         byte[] data = new byte[60];
         int len, sent = 0;
         FileInputStream in = null;
         byte fielHandle = 0;

         in = new FileInputStream(file);

         LCD.drawString("DATA: " + file.length(), 0, 0);
         fielHandle = openWrite(file.getName(), (int) file.length());
         LCD.drawString("HANDLE: " + fielHandle, 0, 2);

         setVerify(false);
         
         while ((len = in.read(data)) > 0) {
            byte[] sendData = new byte[len];

            for (int i = 0; i < len; i++)
               sendData[i] = data[i];

            sent += len;
            LCD.drawString("SEND: " + sent, 0, 3);
            writeFile(fielHandle, sendData);
         }
         
         setVerify(true);
         closeFile(fielHandle);
   }


Code - sendRequest:
Code: Select all
private byte sendRequest(byte[] request) throws IOException {

      byte verify = 0; // default of 0 means success
      if (verifyCommand)
         request[0] = DIRECT_COMMAND_REPLY;

      nxtComm.sendData(request);
      if (verifyCommand) {
         byte[] reply = nxtComm.readData();
         verify = reply[2];
      }
      return verify;
   }


Code - OpenWrite:
Code: Select all
   public byte openWrite(String fileName, int size) throws IOException {
      byte[] command = { SYSTEM_COMMAND_REPLY, OPEN_WRITE };
      byte[] asciiFileName = new byte[fileName.length()];

      for (int i = 0; i < fileName.length(); i++)
         asciiFileName[i] = (byte) fileName.charAt(i);

      command = appendBytes(command, asciiFileName);
      byte[] request = new byte[22];
      System.arraycopy(command, 0, request, 0, command.length);

      byte[] fileLength = { (byte) size, (byte) (size >>> 8),
            (byte) (size >>> 16), (byte) (size >>> 24) };

      request = appendBytes(request, fileLength);

      nxtComm.sendData(request);
      LCD.drawString("Sends: " + request.length, 0, 7);
      byte[] reply = nxtComm.readData();

      LCD.drawString("Replay: " + reply.length, 0, 5);

      if (reply == null || reply.length != 4) {
         throw new IOException("Invalid return from OPEN WRITE");
      } else if (reply[2] != 0) {
         if (reply[2] == (byte) 0xFB)
            throw new IOException("NXJ Flash Memory Full");
         else if (reply[2] == (byte) 0xFC)
            throw new IOException("NXJ Directory Full");
         else if (reply[2] == (byte) 0x8F)
            throw new IOException("FILE EXISTS !!");
         else
            throw new IOException("OPEN WRITE failed");

      }
      return reply[3]; // The handle number
   }


I can post Full my code if nessecery...

Please, help if you can:)

Thanks :)
Last edited by astral on Wed Jan 28, 2009 5:32 pm, edited 2 times in total.
astral
New User
 
Posts: 4
Joined: Mon Jan 26, 2009 1:35 pm

Postby lawrie » Mon Jan 26, 2009 4:41 pm

Please post your full code and I will try it out. I am currently looking at implementing the full NXTCommand in lejos.nxt.remote for the next release, which will include what you are doing here. We have RS485 comms working in the SVN version of leJOS and this will allow faster NXT to NXT streams and LCP communications by connecting two or more NXTs with a NXT sensor cable using port 4.

Is there a reason why you want to use LCP rather than streams for what you are doing?
lawrie
leJOS Team Member
 
Posts: 677
Joined: Mon Feb 05, 2007 1:27 pm

Postby astral » Mon Jan 26, 2009 5:14 pm

Oh :) Thanks for replay :)

I am working on project with my friend. Our project its about two diffrent cars, on car is creating a road map while moving, and after reaching finish of the road, car is sending generated map to secound car and start special program, to move on the same road using recived map :) - i have hope you understand what I mean :), so I need LCP to work good.

I have two files:

File: BTTest.java - main test application
Code: Select all
import java.io.IOException;
import lejos.nxt.*;

/**
 * @author astral
 *
 */
public class BTtest {

   /**
    * @param args
    */
   public static void main(String[] args) {
      nxtBlue bt = new nxtBlue();
      LCD.clearDisplay();
      LCD.drawString("Test", 5, 5);

      try {

         // connecting to NXT called 'Tester'
         bt.connect("Tester");
         bt.playTone( 55, 100 );
         bt.playTone( 140, 500 );

         LCD.refresh();
         
         // sending local file to remote NXT
         bt.sendFile("dzid.txt");
         LCD.refresh();

         bt.close();
      } catch (IOException e1) {
         LCD.clearDisplay();
         LCD.drawString("ERROR!!!", 0, 0);
      }
      

      try {
         Button.ESCAPE.waitForPressAndRelease();
      } catch (InterruptedException e) {
      }
   }

}



File2: nxtBlue.java
Code: Select all
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import lejos.nxt.remote.NXTComm;
import lejos.nxt.remote.NXTProtocol;
import lejos.nxt.*;
import lejos.nxt.comm.Bluetooth;

/**
 * @author astral
 *
 */
public class nxtBlue implements NXTProtocol {

   private NXTComm nxtComm = new NXTComm();
   private boolean verifyCommand = false;
   private boolean open = false;

   public nxtBlue() {
      LCD.drawString("nxtBlue Module", 10, 10);
   }

   public void connect(String nxtName) throws IOException {
      open = nxtComm.open(nxtName);
      if (!open)
         throw new IOException("Open failed");
   }

   public static void main(String[] args) { }

   public void sendFile(String filename) throws IOException {
         setVerify(true);

         File file = new File(filename);
         byte[] data = new byte[60];
         int len, sent = 0;
         FileInputStream in = null;
         byte fielHandle = 0;

         in = new FileInputStream(file);

         LCD.drawString("DATA: " + file.length(), 0, 0);
         fielHandle = openWrite(file.getName(), (int) file.length());
         LCD.drawString("HANDLE: " + fielHandle, 0, 2);

         setVerify(false);
         
         while ((len = in.read(data)) > 0) {
            byte[] sendData = new byte[len];

            for (int i = 0; i < len; i++)
               sendData[i] = data[i];

            sent += len;
            LCD.drawString("SEND: " + sent, 0, 3);
            writeFile(fielHandle, sendData);
         }
         
         setVerify(true);
         closeFile(fielHandle);
   }

   public void turnOn() {
      Bluetooth.setPower(true);
   }

   public void turnOff() {
      Bluetooth.setPower(false);
   }

   // for 'sendRequest' test :)
   public byte playTone(int frequency, int duration) throws IOException {
      synchronized (this) {
         byte[] request = { DIRECT_COMMAND_NOREPLY, PLAY_TONE,
               (byte) frequency, (byte) (frequency >>> 8),
               (byte) duration, (byte) (duration >>> 8) };
         return sendRequest(request);
      }
   }

   public void close() throws IOException {
      nxtComm.close();
   }

   public void setVerify(boolean verify) {
      verifyCommand = verify;
   }

   /**
    * Small helper method to send request to NXT and return verification
    * result.
    *
    * @param request
    * @return 0 for success
    */
   private byte sendRequest(byte[] request) throws IOException {

      byte verify = 0; // default of 0 means success
      if (verifyCommand)
         request[0] = DIRECT_COMMAND_REPLY;

      nxtComm.sendData(request);
      if (verifyCommand) {
         byte[] reply = nxtComm.readData();
         verify = reply[2];
      }
      return verify;
   }

   /**
    * Small helper method to send a SYSTEM COMMAND request to NXT and return
    * verification result.
    *
    * @param request
    * @return
    */
   private byte sendSystemRequest(byte[] request) throws IOException {
      byte verify = 0; // default of 0 means success

      if (verifyCommand)
         request[0] = SYSTEM_COMMAND_REPLY;

      nxtComm.sendData(request);
      byte[] reply = nxtComm.readData();

      if (request[0] == SYSTEM_COMMAND_REPLY) {
         verify = reply[2];
      }

      return verify;
   }

   /**
    * Opens a file on the NXT for writing.
    *
    * @param fileName
    *            e.g. "Woops.rso"
    * @return File Handle number
    */
   public byte openWrite(String fileName, int size) throws IOException {
      byte[] command = { SYSTEM_COMMAND_REPLY, OPEN_WRITE };
      byte[] asciiFileName = new byte[fileName.length()];

      for (int i = 0; i < fileName.length(); i++)
         asciiFileName[i] = (byte) fileName.charAt(i);

      command = appendBytes(command, asciiFileName);
      byte[] request = new byte[22];
      System.arraycopy(command, 0, request, 0, command.length);

      byte[] fileLength = { (byte) size, (byte) (size >>> 8),
            (byte) (size >>> 16), (byte) (size >>> 24) };

      request = appendBytes(request, fileLength);

      nxtComm.sendData(request);
      LCD.drawString("SendLEN: " + request.length, 0, 7);
      
      byte[] reply = nxtComm.readData();
      LCD.drawString("ReplayLEN: " + reply.length, 0, 5);

      if (reply == null || reply.length != 4) {
         throw new IOException("Invalid return from OPEN WRITE");
      } else if (reply[2] != 0) {
         if (reply[2] == (byte) 0xFB)
            throw new IOException("NXJ Flash Memory Full");
         else if (reply[2] == (byte) 0xFC)
            throw new IOException("NXJ Directory Full");
         else if (reply[2] == (byte) 0x8F)
            throw new IOException("FILE EXISTS !!");
         else
            throw new IOException("OPEN WRITE failed");

      }
      return reply[3]; // The handle number
   }

   public byte writeFile(byte handle, byte[] data) throws IOException {
      byte[] request = new byte[data.length + 3];
      byte[] command = { SYSTEM_COMMAND_NOREPLY, WRITE, handle };
      System.arraycopy(command, 0, request, 0, command.length);
      System.arraycopy(data, 0, request, 3, data.length);

      return sendSystemRequest(request);
   }
   
   /**
    * Closes an open file.
    *
    * @param handle
    *            File handle number.
    * @return Error code 0 = success
    */
   public byte closeFile(byte handle) throws IOException {
      byte[] request = { SYSTEM_COMMAND_NOREPLY, CLOSE, handle };
      return sendSystemRequest(request);
   }

   /**
    * Helper code to append a string and null terminator at the end of a
    * command request. Should use String.concat if I could add a zero to end
    * somehow.
    *
    * @param command
    * @param str
    * @return
    */
   private byte[] appendString(byte[] command, String str) {
      byte[] buff = new byte[command.length + str.length() + 1];
      for (int i = 0; i < command.length; i++)
         buff[i] = command[i];
      for (int i = 0; i < str.length(); i++)
         buff[command.length + i] = (byte) str.charAt(i);
      buff[command.length + str.length()] = 0;
      return buff;
   }

   private byte[] appendBytes(byte[] array1, byte[] array2) {
      byte[] array = new byte[array1.length + array2.length];
      System.arraycopy(array1, 0, array, 0, array1.length);
      System.arraycopy(array2, 0, array, array1.length, array2.length);
      return array;
   }
}


PS. This code are in very early studium, so pleasy by gntly :)
astral
New User
 
Posts: 4
Joined: Mon Jan 26, 2009 1:35 pm

Postby gloomyandy » Mon Jan 26, 2009 6:19 pm

Hi,
shouldn't you have the following code....
Code: Select all
      if (verifyCommand) {
         byte[] reply = nxtComm.readData();
         verify = reply[2];
      }

In your sendSystemRequest method rather than
Code: Select all
           
     byte[] reply = nxtComm.readData();

      if (request[0] == SYSTEM_COMMAND_REPLY) {
         verify = reply[2];
      }



I'm pretty sure that you won't get a response from a system request unless you ask for it, but your code will be waiting for a response that may never come...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby astral » Wed Jan 28, 2009 5:30 pm

Big thanks gloomyandy ! :),

I fixed my code with yours sugestions , and gues what :) , IT'S WOOORKING !!:)
Now my Class can send a file to another NXT using LCP Protocol :) - just great!:)

So, I'm going to do some tests and write new code, so probably I will release new Code soon :) with planty of comments of course:)

Thanks again :)
Astral :)

Przemyslaw Danysz
astral20@gmail.com
astral
New User
 
Posts: 4
Joined: Mon Jan 26, 2009 1:35 pm

Postby astral » Mon Mar 16, 2009 12:14 pm

Hi !!:)

It's me again :)
I finished working on my Class, so I want to share with you my work :) I hope this Class helps you a lot :)

How to use my class:
Code: Select all
BlueRemoteNXT tracker2;
tracker2 = new BlueRemoteNXT();

tracker2.connect( "SecoundNXT" );
tracker2.sendFile( "roadMap.dat" );
tracker2.defrag();
tracker2.startProgram("Tracker2.nxj");
tracker2.disconnect();


BlueRemoteNXT Class:
Code: Select all
package danysz.nxt.bluetooth;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import danysz.nxt.utils.FileInfo;

import lejos.nxt.remote.NXTComm;
import lejos.nxt.remote.NXTProtocol;
import lejos.nxt.*;

/**
 * @author Przemyslaw Danysz [Poland] <astral20@gmail.com>
 * @version 1.0
 */
public class BlueRemoteNXT implements NXTProtocol {

   private NXTComm nxtComm = new NXTComm();

   private boolean verifyCommand = false;

   private boolean open = false;

   public BlueRemoteNXT() {

   }

   /**
    * The constructor creates a class of a connection between two robots
    *
    * @param String
    *            nxtName - Robot's name
    * @throws IOException
    */
   public BlueRemoteNXT(String nxtName) throws IOException {

      LCD.drawString("Author: Przemyslaw Danysz", 0, 1);
      LCD.drawString("nxtBlue Module", 0, 2);
      connect(nxtName);
   }

   /**
    * The method make connection with another robot.
    * If the connection fails, throws an exception
    *
    * @param String
    *            nxtName - Robot's name
    * @throws IOException
    */
   public void connect(String nxtName) throws IOException {
      open = nxtComm.open(nxtName);
      if (!open)
         throw new IOException("Open failed");
   }

   /**
    * The method by which we can download the file from another NXT
    * and save it to memory
    *
    * @param String
    *            fileName - remote file name to download
    */
   public void downloadFile(String fileName) {
      synchronized (this) {
         FileInfo file = null;
         int received = 0;
         File f = null;
         FileOutputStream out = null;

         try {
            f = new File(fileName);

            if (!f.exists()) {
               f.createNewFile();
            } else {
               f.delete();
               f.createNewFile();
            }

            out = new FileOutputStream(f);

            setVerify(true);
            file = openRead(fileName);

            LCD.clear();

            do {
               byte[] data = readFile(file.fileHandle, (file.fileSize
                     - received < 51 ? file.fileSize - received : 51));

               received += data.length;

               LCD.drawString("Received " + data.length + " bytes", 0, 5);
               LCD.drawString("Data: " + received, 0, 6);
               LCD.refresh();

               out.write(data);

            } while (received < file.fileSize);

            setVerify(false);

            out.close();
            closeFile(file.fileHandle);

            LCD.drawString("-- OK --", 2, 2);

         } catch (IOException e) {
            LCD.drawString(e.getMessage(), 0, 0);
         }
      }
   }

   /**
    * Method to transfer a file to another NXT robot.
    * With this method it is possible to file send file to another NXT robot
    *
    * @param String
    *            filename - local file name to send
    * @throws IOException
    */
   public void sendFile(String filename) throws IOException {
      synchronized (this) {

         setVerify(true);

         File file = new File(filename);
         byte[] data = new byte[60];
         int len, sent = 0;

         FileInputStream in = new FileInputStream(file);
         byte fielHandle = 0;

         LCD.drawString("DATA: " + file.length(), 0, 0);

         fielHandle = openWrite(file.getName(), (int) file.length());
         LCD.drawString("HANDLE: " + fielHandle, 0, 2);

         setVerify(false);

         while ((len = in.read(data)) > 0) {
            byte[] sendData = new byte[len];

            for (int i = 0; i < len; i++)
               sendData[i] = data[i];

            sent += len;
            LCD.drawString("SEND: " + sent, 0, 3);
            writeFile(fielHandle, sendData);
         }

         setVerify(true);
         closeFile(fielHandle);
      }
   }

   /**
    * 'sendRequest' test :)
    */
   public byte playTone(int frequency, int duration) throws IOException {
      synchronized (this) {
         byte[] request = { DIRECT_COMMAND_NOREPLY, PLAY_TONE,
               (byte) frequency, (byte) (frequency >>> 8),
               (byte) duration, (byte) (duration >>> 8) };
         return sendRequest(request);
      }
   }

   /**
    * The method runs programs on a remote robot
    *
    * @param String
    *            nazwa pliku (programu)
    * @return 0 if success
    * @throws IOException
    */
   public byte startProgram(String fileName) throws IOException {
      byte[] request = { DIRECT_COMMAND_NOREPLY, START_PROGRAM };
      request = appendString(request, fileName);

      return sendRequest(request);
   }

   /**
    * This method stops the program being executed on a remote robot
    *
    * @return
    * @throws IOException
    */
   public byte stopProgram() throws IOException {
      byte[] request = { DIRECT_COMMAND_NOREPLY, STOP_PROGRAM };
      return sendRequest(request);
   }

   /**
    * Disconnect with other NXT robot
    *
    * @throws IOException
    */
   public void disconnect() throws IOException {

      if (open == true) {

         open = false;

         byte[] request = { SYSTEM_COMMAND_NOREPLY, 0x20 };

         sendRequest(request);
         nxtComm.close();
      }
   }

   public int getBatteryLevel() throws IOException {
      synchronized (this) {
         byte[] request = { DIRECT_COMMAND_REPLY, GET_BATTERY_LEVEL };

         nxtComm.sendData(request);
         byte[] reply = nxtComm.readData();

         int batteryLevel = (0xFF & reply[3]) | ((0xFF & reply[4]) << 8);
         return batteryLevel;
      }
   }

   /**
    * Deletes user flash memory (not including system modules).
    *
    * @return 0 for success
    */
   public byte deleteUserFlash() throws IOException {
      synchronized (this) {
         byte[] request = { SYSTEM_COMMAND_REPLY, DELETE_USER_FLASH };

         nxtComm.sendData(request);
         byte[] reply = nxtComm.readData();

         return reply[2];
      }
   }

   /**
    * The method removes the file on the remote robot
    *
    * @param String
    *            fileName - nazwa pliku
    * @return 0 if success
    * @throws IOException
    */
   public byte deleteFile(String fileName) throws IOException {
      synchronized (this) {
         byte[] request = { SYSTEM_COMMAND_REPLY, DELETE };
         request = appendString(request, fileName);

         nxtComm.sendData(request);
         byte[] reply = nxtComm.readData();

         return reply[2];
      }
   }

   /**
    * This method sets the type of command: system or a direct
    *
    * @param boolean
    *            verify - true/false [tak/nie]
    */
   private void setVerify(boolean verify) {
      verifyCommand = verify;
   }

   /**
    * Small helper method to send request to NXT and return verification
    * result.
    *
    * @param request
    * @return 0 for success
    */
   private byte sendRequest(byte[] request) throws IOException {

      byte verify = 0; // default of 0 means success

      if (verifyCommand)
         request[0] = DIRECT_COMMAND_REPLY;

      nxtComm.sendData(request);
      if (verifyCommand) {
         byte[] reply = nxtComm.readData();
         verify = reply[2];
      }
      return verify;
   }

   /**
    * Small helper method to send a SYSTEM COMMAND request to NXT and return
    * verification result.
    *
    * @param request
    * @return
    */
   private byte sendSystemRequest(byte[] request) throws IOException {
      byte verify = 0; // default of 0 means success

      if (verifyCommand)
         request[0] = SYSTEM_COMMAND_REPLY;

      nxtComm.sendData(request);

      if (request[0] == SYSTEM_COMMAND_REPLY) {
         byte[] reply = nxtComm.readData();
         verify = reply[2];
      }

      return verify;
   }

   /**
    * The method generates a "system command" for the second robot,
    * to be prepared to read the file
    *
    * @param fileName -
    *            file name to read
    * @return FileInfo fileInfo - data about remote file
    * @throws IOException
    */
   private FileInfo openRead(String fileName) throws IOException {
      byte[] request = { SYSTEM_COMMAND_REPLY, OPEN_READ };
      request = appendString(request, fileName); // No padding required
      // apparently

      nxtComm.sendData(request);
      byte[] reply = nxtComm.readData();

      FileInfo fileInfo = new FileInfo(fileName);
      fileInfo.status = reply[2];

      if (reply.length == 8) { // Check if all data included in reply
         fileInfo.fileHandle = reply[3];
         fileInfo.fileSize = (0xFF & reply[4]) | ((0xFF & reply[5]) << 8)
               | ((0xFF & reply[6]) << 16) | ((0xFF & reply[7]) << 24);
      }

      return fileInfo;
   }

   /**
    * The method generates a "command system" for the robot
    * about the second portion of data from an open file
    *
    * @param byte
    *            handle - wskaznik do pliku dostarczony przez OpenRead
    * @param int
    *            length - ilosc danych do pobrania
    * @return byte[] data - pobrane dane
    * @throws IOException
    */
   private byte[] readFile(byte handle, int length) throws IOException {
      byte[] request = { SYSTEM_COMMAND_REPLY, READ, handle, (byte) length,
            (byte) (length >>> 8) };

      nxtComm.sendData(request);
      byte[] reply = nxtComm.readData();

      int dataLen = (reply[4] & 0xFF) + ((reply[5] << 8) & 0xFF);
      byte[] data = new byte[dataLen];

      for (int i = 0; i < dataLen; i++)
         data[i] = reply[i + 6];

      return data;
   }

   /**
    * Defrag remote user flash
    *
    * @return 0 if success
    * @throws IOException
    */
   public byte defrag() throws IOException {
      byte[] request = { SYSTEM_COMMAND_NOREPLY, 0x21 };
      return sendSystemRequest(request);
   }

   /**
    * Opens a file on the NXT for writing.
    *
    * @param String
    *            fileName e.g. "Woops.rso"
    * @return File Handle number
    */
   private byte openWrite(String fileName, int size) throws IOException {
      byte[] command = { SYSTEM_COMMAND_REPLY, OPEN_WRITE };
      byte[] asciiFileName = new byte[fileName.length()];

      for (int i = 0; i < fileName.length(); i++)
         asciiFileName[i] = (byte) fileName.charAt(i);

      command = appendBytes(command, asciiFileName);
      byte[] request = new byte[22];
      System.arraycopy(command, 0, request, 0, command.length);

      byte[] fileLength = { (byte) size, (byte) (size >>> 8),
            (byte) (size >>> 16), (byte) (size >>> 24) };

      request = appendBytes(request, fileLength);

      nxtComm.sendData(request);
      byte[] reply = nxtComm.readData();

      LCD.drawString("SendLEN: " + request.length, 0, 7);

      LCD.drawString("ReplayLEN: " + reply.length, 0, 5);

      if (reply == null || reply.length != 4) {
         throw new IOException("Invalid return from OPEN WRITE");
      } else if (reply[2] != 0) {
         if (reply[2] == (byte) 0xFB)
            throw new IOException("NXJ Flash Memory Full");
         else if (reply[2] == (byte) 0xFC)
            throw new IOException("NXJ Directory Full");
         else if (reply[2] == (byte) 0x8F)
            throw new IOException("FILE EXISTS !!");
         else
            throw new IOException("OPEN WRITE failed");

      }
      return reply[3]; // The handle number
   }

   /**
    * The method prepares and sends the data
    * to be recorded in a file on a remote NXT robot
    *
    * @param byte
    *            handle - file handle returned by OpenWrite Command
    * @param byte[]
    *            data - file data to save
    * @return
    * @throws IOException
    */
   private byte writeFile(byte handle, byte[] data) throws IOException {
      byte[] request = new byte[data.length + 3];
      byte[] command = { SYSTEM_COMMAND_NOREPLY, WRITE, handle };
      System.arraycopy(command, 0, request, 0, command.length);
      System.arraycopy(data, 0, request, 3, data.length);

      return sendSystemRequest(request);
   }

   /**
    * Closes an open file.
    *
    * @param byte
    *            handle - File handle number.
    * @return Error code 0 = success
    */
   private byte closeFile(byte handle) throws IOException {
      byte[] request = { SYSTEM_COMMAND_NOREPLY, CLOSE, handle };
      return sendSystemRequest(request);
   }

   /**
    * Helper code to append a string and null terminator at the end of a
    * command request. Should use String.concat if I could add a zero to end
    * somehow.
    *
    * @param command
    * @param str
    * @return
    */
   private byte[] appendString(byte[] command, String str) {
      byte[] buff = new byte[command.length + str.length() + 1];

      for (int i = 0; i < command.length; i++)
         buff[i] = command[i];

      for (int i = 0; i < str.length(); i++)
         buff[command.length + i] = (byte) str.charAt(i);

      buff[command.length + str.length()] = 0;
      return buff;
   }

   private byte[] appendBytes(byte[] array1, byte[] array2) {
      byte[] array = new byte[array1.length + array2.length];

      System.arraycopy(array1, 0, array, 0, array1.length);
      System.arraycopy(array2, 0, array, array1.length, array2.length);

      return array;
   }
}

astral
New User
 
Posts: 4
Joined: Mon Jan 26, 2009 1:35 pm

Re: [solved] Bluetooth: Sending File from NXT to NXT using L

Postby adam49 » Tue Dec 20, 2011 7:03 pm

Hi astral,
I'm new to NXT and i stumbled across your post, i want to try something new so I'm going to try out your class
I will let you know how i get on.

Thanks for showing us how to use your class. :wink:
adam campbell
adam49
New User
 
Posts: 1
Joined: Tue Dec 20, 2011 6:52 pm


Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff