How to name my brick?

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

How to name my brick?

Postby esmetaman » Sun Sep 09, 2007 5:23 pm

Hi,

I am testing new NXJ release, I would like to create a general Class to manage communications between 2 nxt brick.

My first doubt is:

how to name my brick? In javadoc, I saw the method: setFriendlyName(byte[] name) but how to convert a string into byte.

For example:

I would like to change the name of my brick, for example: "NXTBOT1" how to convert this string into a byte[] format?

Thanks
User avatar
esmetaman
Advanced Member
 
Posts: 296
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby bbagnall » Sun Sep 09, 2007 6:24 pm

There's a tool with NXJ that lets you change the name of your brick. It's called NXJBrowse and it's located in the directory lejos_nxj/bin. I think it will only change the name under USB for now. Under Bluetooth it might look like it changed the name but it actually doesn't, if I'm not mistaken.
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Postby esmetaman » Sun Sep 09, 2007 6:34 pm

Hi brian,

I will test with nxjbrowse, but with the method setFriendlyName, is it possible?

I have found a way to make a class to convert string to byte[]

Thanks.
User avatar
esmetaman
Advanced Member
 
Posts: 296
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby esmetaman » Sun Sep 09, 2007 7:15 pm

Hi Brian,

I tested nxjbrowse, but i received a exception in my MSDOS console:

C:\DATOS\ROBOTICS\NXJ\samples\jab\1>nxjbrowse
found 5 busses
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d07baf4, pid=3536, ti
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_09-b01 mixed mode, sharing)
# Problematic frame:
# C [awt.dll+0xbaf4]
#
# An error report file with more information is saved as hs_err_pid3536.l
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#

C:\DATOS\ROBOTICS\NXJ\samples\jab\1>

Can you tell me the cause?
User avatar
esmetaman
Advanced Member
 
Posts: 296
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby lawrie » Mon Sep 10, 2007 6:59 pm

You should be able to call Bluetooth.setFriendlyName directly, e.g:

String nameString = "NXTBOT1";

byte[] nameBytes = new byte[16];

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

nameBytes[nameString.length()] = 0;

Bluetooth.setFriendlyName(nameBytes);

I don't know why nxjbrowse caused a JVM failure. Does this happen immediately, or when you change the name? Did it happen with the 0.3 release also?
lawrie
leJOS Team Member
 
Posts: 929
Joined: Mon Feb 05, 2007 1:27 pm

Postby esmetaman » Mon Sep 10, 2007 9:32 pm

Hi Lawrie, I tried to test NXJBROWSE command using MSDOS console and a typed NXJBROWSE, and I received this exception.

I will try your code.

Why do you defined a byte array with 16 elements?

Thanks
User avatar
esmetaman
Advanced Member
 
Posts: 296
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby esmetaman » Mon Sep 10, 2007 9:54 pm

Thanks Lawrie, I understood your code.

My idea is run a class and it used new features using LCUID.

import lejos.nxt.*;
import javax.microedition.lcdui.*;
import lejos.nxt.comm.*;

public class BotNamer{

public static void main(String[] args)throws Exception {

/*
LCDUI obj = new LCDUI();
String texto = obj.show();
*/
String NXTName = "SOLDIER1";
Graphics gObj = new Graphics();
gObj.drawString(NXTName, 0, 0);
gObj.refresh();
setNXTName(NXTName);
}

static void setNXTName(String NXTName){
int size = (NXTName.length()+1)*2;

byte[] nameBytes = new byte[size];

for(int i=0;i<NXTName.length();i++){
nameBytes[i] = (byte) NXTName.charAt(i);
}
nameBytes[NXTName.length()] = 0;

Bluetooth.setFriendlyName(nameBytes);
}
}

I can receive data from TextBox Object. Anybody can I help me?

import java.util.Random;

import javax.microedition.lcdui.*;
import lejos.nxt.*;
import java.util.*;

public class LCDUI implements CommandListener{

private TextBox input;
private Display display;

private static final int CMDID_BACK_TO_MAIN = 1;
private static final int CMDID_EXIT_APP = 2;

private static final Command BACK_COMMAND = new Command(CMDID_BACK_TO_MAIN, Command.BACK, 0);
private static final Command EXIT_COMMAND = new Command(CMDID_EXIT_APP, Command.STOP, 2);

public void LCDUI(){

}

public String show(){
TextBox input = new TextBox("Enter NXT Name:", "", 10, TextField.ANY);
input.addCommand(BACK_COMMAND);
input.setCommandListener(this);

Display display = Display.getDisplay();
display.setCurrent(input);
display.show(true);

return input.getText();
}

public void commandAction(Command c, Displayable d) {
if (c.getCommandId() == CMDID_BACK_TO_MAIN) {
Display display = Display.getDisplay();
display.quit();
}
}

}

I tried to modify original sample, But I dont know how to transfer a String from the execution of LCDUI class

My original idea is deliver a NXJ tool to define the NXT BRICK using new Textbox Class
User avatar
esmetaman
Advanced Member
 
Posts: 296
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby lawrie » Tue Sep 11, 2007 8:33 pm

The protocol between the ARM chip and the BC4 chip sends the name as 16-bytes padded out with zeroes. The code in the LCP class that calls setFriendlyName passes a 16-byte array. I think setFriendlyname currently only works if it passed the full 16 bytes. I will fix this for the next release.

I will look at your code for setting the name from a TextBox if I get the chance. I have not looked at the LCDUI code much yet. It was written by Andre Nijholt.
lawrie
leJOS Team Member
 
Posts: 929
Joined: Mon Feb 05, 2007 1:27 pm

Postby esmetaman » Tue Sep 11, 2007 8:58 pm

Write to my personal email to send Andre Nijhol's email to contact with him.

Thanks.
User avatar
esmetaman
Advanced Member
 
Posts: 296
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby lawrie » Sun Sep 16, 2007 5:11 pm

I do not have Andre Nijholt's email address - I think Brian has it.

The program below works for me for changing the name of the NXT.

I am not sure if what I have done is correct, but it seems to work, apart from, for some reason, you have to press ENTER twice to select the SetName text box.


import javax.microedition.lcdui.*;
import lejos.nxt.comm.*;
/**
*
* @author Lawrie Griffiths
*/
public class SetName implements CommandListener {
private static final int CMDID_BACK_TO_MAIN = 1;
private static final int CMDID_EXIT_APP = 2;

private static final Command BACK_COMMAND = new Command(CMDID_BACK_TO_MAIN, Command.BACK, 0);
private static final Command EXIT_COMMAND = new Command(CMDID_EXIT_APP, Command.STOP, 2);

private List menu = new List("Set Name", Choice.IMPLICIT);
private Ticker ticker = new Ticker("Tools");
private TextBox input = new TextBox("Enter Name:", "", 16, TextField.ANY);
private Alert exitAlert = new Alert("Exit");
private Display display;

/**
* Start application
*
* @param polling Select method for button handling
*/
public void startApp(boolean polling) {
// Create main menu
menu = new List("Tools", Choice.IMPLICIT);
menu.append("Set Name", null);

menu.setSelectedIndex(0, true);
menu.addCommand(EXIT_COMMAND);
menu.setCommandListener(this);
menu.setTicker(ticker);

// Set textbox properties
input.addCommand(BACK_COMMAND);
input.setCommandListener(this);

// Start displaying main menu and handling buttons
display = Display.getDisplay();
display.setCurrent(menu);
display.show(polling);
}

/**
* Handle events.
*/
public void commandAction(Command c, Displayable d) {
if (c.getCommandId() == CMDID_BACK_TO_MAIN) {

if (d == input) {
// Name has changed
String nameString = input.getText();
byte [] nameBytes = new byte[16];
for(int i=0;i<nameString.length();i++) {
nameBytes[i] = (byte) nameString.charAt(i);
}
Bluetooth.setFriendlyName(nameBytes);
}
// Display main menu again
display.setCurrent(menu);
} else if (c.getCommandId() == CMDID_EXIT_APP) {
// Request to exit application
exitAlert.setType(Alert.ALERT_TYPE_CONFIRMATION);
exitAlert.setString("Exit Lejos?");
exitAlert.setCommandListener(this);
display.setCurrent(exitAlert);
} else {
// Handle system commands
if (d == exitAlert) {
if (exitAlert.getConfirmation()) {
display.quit();
} else {
display.setCurrent(menu);
}
} else if (d == menu) {
List list = (List) display.getCurrent();
if (list.getSelectedIndex() == 0) {
display.setCurrent(input);
}
}
}
}

public static void main(String[] args) {
new SetName().startApp(true);
}
}
lawrie
leJOS Team Member
 
Posts: 929
Joined: Mon Feb 05, 2007 1:27 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 4 guests

more stuff