LCP and mailboxes to send and receive data

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

Moderators: 99jonathan, roger, imaqine

LCP and mailboxes to send and receive data

Postby esmetaman » Sun Jan 02, 2011 3:30 pm

Happy new year friends,

This week, I was testing LCP support for NXT and I observe that LCP has a method to send messages, but It doesn't have a method to read messages.

http://lejos.svn.sourceforge.net/viewvc ... iew=markup

In the class exist a generic method named emulateCommand and it includes a way to readMessages.

Anyone has tested a way to read data from a mailbox?

In the SVN exists 2 examples named MonitorTest and Startup which use messageWrite but I didn't see any example using emulateCommand

http://lejos.svn.sourceforge.net/viewvc ... iew=markup

The way to send a message to mailbox 1 is the following:

LCPBTResponder lcpThread = new LCPBTResponder();
lcpThread.setDaemon(true);
lcpThread.start();

System.out.println("Running");

int counter = 0;
String message = "";
String output = "";

message = counter + " O";
System.out.println(message);
LCP.messageWrite(0, message);

Cheers
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Receiving something

Postby esmetaman » Sun Jan 02, 2011 4:31 pm

Hi,

I notice that I am receiving some stuff:

The code which I am using is the following.

Code: Select all
import java.io.IOException;
import lejos.nxt.*;
import lejos.nxt.comm.*;

public class MonitorTest {

   public static void main(String[] args ) throws Exception {
        LCPBTResponder lcpThread = new LCPBTResponder();
        lcpThread.setDaemon(true);
        lcpThread.start();
   
      System.out.println("Running");
      
      int counter = 0;
      String message = "";
      String output = "";
      
      while(!Button.ESCAPE.isPressed()){

         //Send
         message  = counter + " @";
         System.out.println( "W: " + message);
         LCP.messageWrite(0, message);
         
         //Receive
         byte[] inMsg = new byte[64];
         byte[] reply = new byte[64];
         int len = 0;
         
         try{
            reply = messageRead((byte)0, (byte)0, false);
            //int value = ((0xFF & reply[5]) << 8);
            //int value = (0xFF & reply[5]);
            System.out.println("R: " + reply[0]);
            //System.out.println("" + replyLen);
         }catch(Exception e){
            System.out.println("Exception");
         }
         
          pause(500);
         
          counter++;
      }
      
      System.out.println("End");
      // Exit after any button is pressed
      Button.waitForPress();
   }
      
   public static void pause(int time){
      try{ Thread.sleep(time);}catch(InterruptedException e){}
   }
   
   /**
    * @param remoteInbox 0-9
    * @param localInbox 0-9
    * @param i True clears the message from the remote inbox.
    * @return the message as an array of bytes
    */
   public static byte[] messageRead(byte remoteInbox, byte localInbox, boolean i) throws IOException {
      byte [] request = {LCP.DIRECT_COMMAND_REPLY, LCP.MESSAGE_READ, remoteInbox, localInbox, (i ? (byte) 1 : (byte) 0)};
      byte[] reply = new byte[64];
      //byte [] reply = nxtComm.sendRequest(request, 64);
      int len = 0;
      int replyLen = LCP.emulateCommand(request, len,reply);
      byte[] message = new byte[reply[4]];
      System.arraycopy(reply, 5, message, 0, reply[4]);
      return message;
   }
}


For the moment, I send and receive, but when I receive, I should receive the number 1.

Anyone, has some clue?

Cheers
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain


Return to NXJ Software

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests

more stuff