The return value of SendRemote*

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
felix2ch
Posts: 33
Joined: 11 Jun 2013, 16:46

The return value of SendRemote*

Post by felix2ch »

I'm trying to send messages between two nxts.
I found the return value always 0x20 after SendRemoteString called.But the message be sended successfully.

After searching this forum, I found John said:
What does SendRemoteString return? You should always check function return values in case of errors or other important status values. It probably returns STAT_COMM_PENDING.
But in the NXTDefs.h:

Code: Select all

  __DCMessageWritePacket byte[]     {0x80, 0x09, 0xFF, 0xFF}
The 0x80 means no response needed.

So, What is the SendRemote* really returned?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: The return value of SendRemote*

Post by afanofosc »

The SendRemoteString function ultimately results in a call to the CommBTWrite system call function on the NXT running your program. This system call is defined as shown below:

Code: Select all

//cCmdWrapCommBTWrite
//ArgV[0]: (return) Status byte, SBYTE
//ArgV[1]: Connection index, 0-3
//ArgV[2]: Buffer
//
NXT_STATUS cCmdWrapCommBTWrite(UBYTE * ArgV[])
{
  SBYTE * pReturnVal =  (SBYTE*)(ArgV[0]);
  UBYTE Connection   = *(ArgV[1]);
  UBYTE * pBuf;
  UWORD BufLength;
  DV_INDEX DVIndex;

  //Resolve array arguments
  DVIndex = *(DV_INDEX *)(ArgV[2]);
  pBuf = cCmdDVPtr(DVIndex);

  BufLength = DV_ARRAY[DVIndex].Count;

  //If there's an old error code hanging around, clear it before proceeding.
  if (VarsCmd.CommStat < 0)
    VarsCmd.CommStat = SUCCESS;

  //!!! Only first 256 bytes could possibly make it through! Should return error on longer input?
  //!!! Not requesting a wait-for-response because only known use doesn't read responses.
  pMapComm->pFunc(SENDDATA, (UBYTE)BufLength, Connection, FALSE, pBuf, (UWORD*)&(VarsCmd.CommStat));

  //!!! Reasonable to wrap below code in cCmdCommBTCheckStatus?
  //INPROGRESS means our request was accepted by His Funkiness of pFunc
  if (VarsCmd.CommStat == (SWORD)INPROGRESS)
  {
    *pReturnVal = STAT_COMM_PENDING;

    //Set DirtyComm flag so stream is reset after program ends
    VarsCmd.DirtyComm = TRUE;
  }
  //Translate BTBUSY to ERR_COMM_CHAN_NOT_READY
  else if (VarsCmd.CommStat == (SWORD)BTBUSY)
  {
    *pReturnVal = ERR_COMM_CHAN_NOT_READY;
  }
  else
  {
    *pReturnVal = UNPACK_STATUS(VarsCmd.CommStat);
  }

  return (NO_ERR);
}
So the result of this function call is whatever gets written to ArgV[0] of the CommBTWrite system call structure, i.e.,

Code: Select all

TCommBTWrite	struct
 Result		sbyte
 Connection	byte
 Buffer		byte[]
TCommBTWrite	ends
The return value is stored in Result. This value has nothing whatsoever to do with the remote NXT. The message has not even been sent yet. It is sent by the NXT firmware asynchronously, i.e., the CommBTWrite call returns immediately with a status (Result) value and some time later on the firmware will send the message to the remote NXT on the specified Connection. You can see above where pReturnVal gets set to different values depending on what happens during this function call.

Code: Select all

#define STAT_COMM_PENDING 32      /*!< Pending setup operation in progress */
The most likely result is STAT_COMM_PENDING (aka 0x20 aka 32). That means: "okay, I will send that message ASAP. Please check the bluetooth communication layer status using BluetoothStatus(byte conn) in a loop until it says that the comm status is idle again before trying to send another BT message"

The help for SendRemoteString says "Use RemoteConnectionIdle to determine when this write request is completed." RemoteConnectionIdle just calls BluetoothStatus and returns true when it returns 0.

If you want to get information from the remote NXT, i.e., something that it tries to send the master in response to the message the master sent it then the master will need to read the reponse message from a remote NXT response mailbox using something like ReceiveRemoteString.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
felix2ch
Posts: 33
Joined: 11 Jun 2013, 16:46

Re: The return value of SendRemote*

Post by felix2ch »

Thank you, John!
So detailed and clearly answer!
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest