[NXC] Sending Numbers from Slave to Master

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
horst94
Posts: 3
Joined: 31 Jul 2011, 08:52

[NXC] Sending Numbers from Slave to Master

Post by horst94 »

Hi,

I have used NXC nearly for a year, but have a question concerning the bluetooth communication between two NXTs with NXC(3.3.8.10):
In the direction master -> slave following commands work:
MASTER:

Code: Select all

 SendRemoteNumber(1,MAILBOX1,temp);
SLAVE:

Code: Select all

ReceiveRemoteNumber(MAILBOX1,false,temp);
But now to the problem, sending numbers from slave to master:
SLAVE:

Code: Select all

SendRemoteNumber(0,MAILBOX1,temp);
MASTER:

Code: Select all

ReceiveRemoteNumber(MAILBOX1,false,temp);
The first run on the NXTs with this commands works, but then the bluetooth link interrupts and only after restarting the devices a new connection is possible...
Without sending numbers form slave to master this behaviour doesn't occur.
I can't find the mistake :cry:
Thanks for your help :)
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: [NXC] Sending Numbers from Slave to Master

Post by spillerrec »

I have no experience with bluetooth communication, however a random guess would be that you should use char SendResponseNumber (byte queue, long val) on the slave.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC] Sending Numbers from Slave to Master

Post by HaWe »

yes, that's right,
and the corresponding master cmd is

Code: Select all

char ReceiveRemoteNumber  ( byte  queue,   bool  clear,  long &  val  )   [inline] 
Read a numeric value from a queue/mailbox.

Read a numeric value from a mailbox and optionally remove it. If the local mailbox is empty and this NXT is the master then it attempts to poll one of its slave NXTs for a message from the response mailbox that corresponds to the specified local mailbox number.

Parameters:
queue The mailbox number. See Mailbox constants.
clear A flag indicating whether to remove the message from the mailbox after it has been read.
val The numeric value that is read from the mailbox.

Returns:
A char value indicating whether the function call succeeded or not.
horst94
Posts: 3
Joined: 31 Jul 2011, 08:52

Re: [NXC] Sending Numbers from Slave to Master

Post by horst94 »

Thanks for your advices :)
I have tried the command "SendResponseNumber", but it fails. In contrast to "SendResponseNumber" the MASTER could not receive the number. On the other hand the bluetooth link remains stable and doesn't interrupt after stopping the program :roll:
My test code:
MASTER:

Code: Select all

task main(){
 int temp=0;
 while(true){
  ReceiveRemoteNumber(MAILBOX1,true,temp);
  NumOut(0,LCD_LINE1,temp,true);            [b]//The 95 does not appear ![/b]
  Wait(500);
  }
 }
SLAVE:

Code: Select all

task main(){
 int i,temp=95;
 while(true){
  SendResponseNumber(MAILBOX1,temp);
  NumOut(0,LCD_LINE1,temp,true);
  Wait(500);
  }
 }
Well, what does the command "SendResponseNumber" do ? How can I read the "local response mailbox" (see char SendResponseNumber (byte queue, long val)) ?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC] Sending Numbers from Slave to Master

Post by afanofosc »

You should always check the return value of functions like these. Save it to a variable and write it to the screen with NumOut. The value will tell you whether it succeeded or failed and if it failed what the problem was. SendResponseNumber will write the number to a reponse mailbox on your slave NXT and then when your master NXT calls ReceiveRemoteNumber it should cause the firmware to poll the slave devices for a message in response mailbox 1 and store it in your master NXT's mailbox 1 and then the next time you call ReceiveRemoteNumber it should retrieve that message and show it to the screen via your NumOut call. If you check the return value you can tell whether or not you should show the value or not based on whether the mailbox was empty or not. Only when this function returns NO_ERR (0) is it okay to use the value. If it returns STAT_COMM_PENDING (32) then repeat the call in a loop until it returns a negative error code or NO_ERR. And you should always pass "true" in for the "clear" parameter of ReceiveRemote* functions so that you remove the message you just read from the mailbox. Otherwise you will keep on reading the same message over and over again since it stays in the mailbox.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
horst94
Posts: 3
Joined: 31 Jul 2011, 08:52

Re: [NXC] Sending Numbers from Slave to Master

Post by horst94 »

afanofosc wrote:You should always check the return value of functions like these.[...]
John Hansen
Thanks for your reply. Now I really understand this Function (SendResponseNumber) :)
With the Code

Code: Select all

char status=64;
 while(status != NO_ERR)
  status = ReceiveRemoteNumber(MAILBOX1,true,temp);
the Number from the Slave NXT, sent with SendResponseNumber, can be received.
So my problem is solved. :)

A last question, am I right in thinking, that ReceiveNumber* have to run twice to receive one number (on master) ?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC] Sending Numbers from Slave to Master

Post by afanofosc »

In this situation, yes, it will always be called at least twice since the only way a message will arrive in the master's local mailbox1 is by first checking for a message and finding an empty mailbox and then repeating the call until the polling code successfully retrieves a message from the slave's response mailbox1. It should be possible for a slave to write directly to the master's mailbox1 but that requires paying close attention on both ends to whether your bluetooth connection's stream is inbound or outbound and checking return values to make sure you do not stomp on previous message send requests by trying to send another message before the first one is completely sent. You said you crashed your connection or something like that when you tried to have the slave write to the master directly. If I could see your full source code I might be able to diagnose the cause and recommend a fix that would allow you to use the slave writes to master approach. No promises but I can have a go. Preferrably not huge programs.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 5 guests