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
