NXC - main method params?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
brothman01
Posts: 20
Joined: 16 Feb 2012, 22:59

NXC - main method params?

Post by brothman01 »

hey,
I am new to NXC, so I have a pretty basic question... the 'main' method does not have the parameters argv[] or anything like that, and I see that the robot can be sent 'messages', if the main function has no parameters, how can these messages be accessed from the code? as in:

Code: Select all

if message equals msg1 {
// do something 1
else if message equals msg2 {
// do something else
}
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC - main method params?

Post by mattallen37 »

task main is where the program starts. If you want to have a conditional, you do it exactly like you showed.

Code: Select all

task main(){
  if(message == msg1){

  }
  else if(message == msg2){

  }
}
That won't compile though, because you didn't tell the program what message, msg1, and msg2 are.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
brothman01
Posts: 20
Joined: 16 Feb 2012, 22:59

Re: NXC - main method params?

Post by brothman01 »

oh ok, but then
a) what kind of objects are 'msg1' and 'msg2'?
b) since there are no string parameters argv[] in task main(), how do I access the messages that are sent?
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC - main method params?

Post by mattallen37 »

That's up to the rest of the program. Values don't magically appear where convenient. What are you trying to do?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: NXC - main method params?

Post by mcsummation »

brothman01 wrote:hey,
I am new to NXC, so I have a pretty basic question... the 'main' method does not have the parameters argv[] or anything like that, and I see that the robot can be sent 'messages', if the main function has no parameters, how can these messages be accessed from the code?
The "messages" may be Bluetooth messages.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC - main method params?

Post by HaWe »

yes, I'm also quite sure about that, but the messages still need to be declared as variables globally, or in the main program locally (most likely as strings).
And then the variable (e.g., string ) values have to be polled somehow by appropriate functions.
spiked33
Posts: 14
Joined: 16 Feb 2012, 17:20

Re: NXC - main method params?

Post by spiked33 »

Ummm, 2 different things, one is called parameters. Since the program is running in the NXT, you really do not have a way of passing parameters. It is a 'main' task, not 'main' in the traditional C sense.

Messages are sent via Bluetooth or USB mailboxes. They aren't variables per se, but a byte array of received and sent characters from a serial connection. And if you go down this route, be sure and get your mind wrapped around the master slave concept. And watch out for API calls like SendRemoteString, which is different from SendResponseString (which inst necessarily a response, from very recent also newb experience).
brothman01
Posts: 20
Joined: 16 Feb 2012, 22:59

Re: NXC - main method params?

Post by brothman01 »

Code: Select all

Ummm, 2 different things, one is called parameters. Since the program is running in the NXT, you really do not have a way of passing parameters. It is a 'main' task, not 'main' in the traditional C sense.
right, that was my bad, I explained it kinda poorly... I am thinking of the NXT as a terminal command, that could be run by sending it arguments and parameters... Since one of the functions in the 'NeXT Tools' program I am using is 'send message', I was thinking of controlling the NXT from there. I think my question is much better explained by showing my code.

My code: (most taken from the NXC language guide at http://bricxcc.sourceforge.net/nbc/nxcd ... index.html

Code: Select all

task main()
{
  MessageReadType args;
  args.QueueID = MAILBOX1; // 0
  // args.Remove = true; // ???
  SysMessageRead(args);

  if (args.Result == NO_ERR) {
    TextOut(0, LCD_LINE1, args.Message);
    Wait(SEC_1);
  } else {
   TextOut(0, LCD_LINE1, "no message found.");
   Wait(SEC_1);
  }
}
my understanding is that my creating a variable of type 'MessageReadType', an array of every message is created called 'args'.
'args.QueueID = MAILBOX1;' tell args to start reading at and stop reading after mailbox 1
'SysMessageRead(args);' tells the brick to read args (as instructed)
then the if statement says to print out the message if one is found, and to print out "no message found" if there is no message to read..

I keep getting the "no message found.", what am I doing wrong to allow the brick to read and print a message?
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: NXC - main method params?

Post by mcsummation »

SysMessageRead says to go read one (1) message from Mailbox1. However, the message must have been put into the mailbox since the program started.

args is a structure of type MessageReadType, it is not an array of messages.
brothman01
Posts: 20
Joined: 16 Feb 2012, 22:59

Re: NXC - main method params?

Post by brothman01 »

SysMessageRead says to go read one (1) message from Mailbox1. However, the message must have been put into the mailbox since the program started.

args is a structure of type MessageReadType, it is not an array of messages.
oh, but how does it know to use mailbox1 only from the 'SysMessageRead' command?
Locked

Who is online

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