currently my code looks like that:
PC
- Code: Select all
DataInputStream dis;
DataOutputStream dos;
NXTComm nxtComm;
NXTInfo nxtInfo;
static int state = 0;
String address = "00:16:53:0A:5C:22";
/**
* Sets up connection
*/
public RobotController() {
try {
// If there is a problem with this, we can change 'NXTCommFactory.BLUETOOTH' to '2'.
nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
} catch (NXTCommException e) {
System.out.println("constructor");
System.out.println(e);
}
// If there is a problem with this, we can change 'NXTCommFactory.BLUETOOTH' to '2'.
nxtInfo = new NXTInfo(NXTCommFactory.BLUETOOTH, name, address);
try {
nxtComm.open(nxtInfo);
} catch (NXTCommException e) {
System.out.println("constructor");
System.out.println(e);
}
InputStream is = nxtComm.getInputStream();
OutputStream os = nxtComm.getOutputStream();
dis = new DataInputStream(is);
dos = new DataOutputStream(os);
}
//*** BT Listener - THREADED
public void run() {
System.out.println("Scanner On ");
while (true) {
//read
try {
state = dis.readInt();
} catch (IOException error) {
continue;
}
System.out.println("robot state : " + state);
}
}
/**
* Sends command to Robot (i.e. Robot.nxj)
*
* @param number, the opcode to be sent to the NXT brick
*/
public void sendCommand(int number) {
try {
dos.writeInt(number);
dos.flush();
} catch (IOException error) {
System.out.println("Error in sending command to robot: " + error);
}
}
and Brick
- Code: Select all
class BTSender
{
private static DataOutputStream dataOutStr;
static public void create()
{
dataOutStr = DataExchange.conn.openDataOutputStream();
}
static public void send(int state)
{
try{
dataOutStr.writeInt(state);
} catch (Exception e)
{
LCD.drawString("error in sending", 0, 5);
}
}
class BTReader extends Thread
{
public DataInputStream dataInStr;
public void run() {
int command=0;
dataInStr = DataExchange.conn.openDataInputStream();
if (dataInStr == null) {
LCD.clear();
LCD.drawString("NoDataStream", 0, 0);
}
while (true)
{
try {
command = dataInStr.readInt();
LCD.drawInt(command, 0, 2);
} catch (Exception ioe)
{
LCD.clear();
LCD.drawString("CantReadFromStream", 0, 0);
}
PilotWrapper.commands.add(command);
}
}
}
sending commands to brick works, but i can't send things back, or probably i can't read them
because sender on brick doesn't return any exception so probably it's works
but listener on pc looks like it stuck and it not doing anything
can anyone tells me what i;m doing wrong?
regards
