NXT with Android Smartphone

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
freggel10
Posts: 6
Joined: 21 Nov 2010, 10:29

NXT with Android Smartphone

Post by freggel10 »

Hello,

When I work on the NXT Direct Commands (to control eg an engine without an external program) it works.
Now I want to send messages to the mailbox of the NXT. The messages would be processed by an external program. That does not work. What is wrong?
(Smartphone app sends message to NXT, LabVIEW VI reads mailbox from NXT)

Code: Select all

public void writeMessage(byte message) {
       commandBuffer = ByteBuffer.allocate(14); // maximum 64 bytes
       commandBuffer.order(ByteOrder.LITTLE_ENDIAN); // has to be little endian by definition
       commandBuffer.putShort((short) 7); // announce that the message is 7 bytes long
       commandBuffer.put((byte) 0x80); // no respond
       commandBuffer.put((byte) 0x09); // Message Write
       commandBuffer.put((byte) 0x01); // Inbox (0-9)
       commandBuffer.put((byte) 0x02); // Message Size
       commandBuffer.put((byte) 0); // Data
       commandBuffer.put((byte) 0x00); // Null termination
       commandBuffer.put(5, (byte) message);
       try{
         myOutputStream.write(commandBuffer.array());             
      } catch (IOException writeException){
         cancel();
      }
       }

Code: Select all

commandBuffer.put(5, (byte) message);
Is 5 right?

Code: Select all

 public void writeMessageButtonClick(View v) {
          if (writeMessageButton.isChecked()) {
             connection.writeMessage((byte) 0x33);
          }else {
             connection.writeMessage((byte) 0x34);
             }
      }
Thanks
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: NXT with Android Smartphone

Post by hassenplug »

You may want to expand a bit when you say "it does not work".

One suggestion: on the NXT, you need to make sure the "Message Received" flag is set to true, or you may get invalid data from the mailbox.

Steve
---> Link to lots of MINDSTORMS stuff under my picture --->
robotman2
Posts: 7
Joined: 28 Aug 2012, 04:52

Re: NXT with Android Smartphone

Post by robotman2 »

Hi Freggel,

Are you using / have you used the NI software to communicate to the NXT? I think I am also a bit unclear exactly what is not working properly. I guess my biggest question is what type of software package are you using to communicate to the NXT?

I haven't looked too much into NI's software extension for the NXT, but in general NI software tends to use modules (VI's) that are custom written for their software and don't translate over to text-based programming too easily. An example of this might be the "mailbox" you mention. To my knowledge, the NXT does not have a mailbox and indeed may be NI lingo for handling received messages.

One point above that Steve mentioned above was that its always good to turn on your message response, or received messages, as they might be able to tell you what's going on.

If you could maybe expand upon your issues a bit more - I might be able to offer a bit more insight. Thanks!

Hope this helps,
Robotman!
(http://www.robotappstore.com)
freggel10
Posts: 6
Joined: 21 Nov 2010, 10:29

Re: NXT with Android Smartphone

Post by freggel10 »

Hello,
Are you using / have you used the NI software to communicate to the NXT? I think I am also a bit unclear exactly what is not working properly. I guess my biggest question is what type of software package are you using to communicate to the NXT?
- I use LabVIEW2009 student edition with the NXT modul (Toolkit) 2009.

Now I have a wait of 100ms after reading the mailbox in my VI.
Image
When I use the following code I get now the String "2". It works now. But if I use the above code, I get now the string "... ³ HTC One SX (and still 3 strange characters)" and not the String "2". Why I get another String? Is anything wrong?
This works:

Code: Select all

public void writeMessage() {
    	commandBuffer = ByteBuffer.allocate(8); // maximum 64 bytes //8
    	commandBuffer.order(ByteOrder.LITTLE_ENDIAN); // has to be little endian by definition
    	commandBuffer.putShort((short) 6); // announce that the message is 6 bytes long 
    	commandBuffer.put((byte) 0x80); // no respond
    	commandBuffer.put((byte) 0x09); // MessageWrite
    	commandBuffer.put((byte) 0x00); // Inbox (0-9) 
    	commandBuffer.put((byte) 0x02); // MessageSize
    	commandBuffer.put((byte) 0x32); // Data //String 2
    	commandBuffer.put((byte) 0x00); // Null termination (/0)
    	try{
			myOutputStream.write(commandBuffer.array());    			
		} catch (IOException writeException){
			cancel();
		}
    	}
    

Code: Select all

public void writeMessageButtonClick(View v) {
    	if (writeMessageButton.isChecked()) {
    		connection.writeMessage();
    	}else {    		}
   }
Here I get the string "... ³ HTC One SX (and still 3 strange characters)" and not the String "2":

Code: Select all

public void writeMessage(byte message) {
       commandBuffer = ByteBuffer.allocate(14); // maximum 64 bytes
       commandBuffer.order(ByteOrder.LITTLE_ENDIAN); // has to be little endian by definition
       commandBuffer.putShort((short) 7); // announce that the message is 7 bytes long
       commandBuffer.put((byte) 0x80); // no respond
       commandBuffer.put((byte) 0x09); // Message Write
       commandBuffer.put((byte) 0x01); // Inbox (0-9)
       commandBuffer.put((byte) 0x02); // Message Size
       commandBuffer.put((byte) 0); // Data
       commandBuffer.put((byte) 0x00); // Null termination
       commandBuffer.put(5, (byte) message);
       try{
         myOutputStream.write(commandBuffer.array());             
      } catch (IOException writeException){
         cancel();
      }
       }

Code: Select all

public void writeMessageButtonClick(View v) {
          if (writeMessageButton.isChecked()) {
             connection.writeMessage((byte) 0x32);
          }else {
             }
      }
Thanks,
freggel10
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: NXT with Android Smartphone

Post by hassenplug »

robotman2 wrote:I haven't looked too much into NI's software extension for the NXT, but in general NI software tends to use modules (VI's) that are custom written for their software and don't translate over to text-based programming too easily. An example of this might be the "mailbox" you mention. To my knowledge, the NXT does not have a mailbox and indeed may be NI lingo for handling received messages.
robotman2, You may want to research this a bit more before answering these types of question. The mailbox is a concept used in LEGO's firmware, NXT-G, NI's Labview, and even NXC.

freggel10, in your code, you seem to be sending the message AFTER the null termination. It seems like you'd want to send it as the message/data byte

Code: Select all

      commandBuffer.putShort((short) 7); // announce that the message is 7 bytes long
       commandBuffer.put((byte) 0x80); // no respond
       commandBuffer.put((byte) 0x09); // Message Write
       commandBuffer.put((byte) 0x01); // Inbox (0-9)
       commandBuffer.put((byte) 0x02); // Message Size
       commandBuffer.put((byte) message); // actual message (data)
       commandBuffer.put((byte) 0x00); // Null termination
Steve
---> Link to lots of MINDSTORMS stuff under my picture --->
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests