Just Another Blog

Are you thinking what I'm thinking?

Wednesday, November 05, 2003

Helping Hands

I've just receive an email from Michael Ash, a guy who wrote the How-to for legOS. (I sent him an email, asking him about how to use LNP... ^^::) I feel so happy. He is really very kind. And the email that he replied is quite long too... o..o||

> Hi,
> I'd like to have inter-RCXs communication and PC-RCX communication
> using
> BrickOS (formerly legOS). I found several useful pages like
> http://legos.sourceforge.net/HOWTO/x405.html and
> http://news.lugnet.com/robotics/rcx/legos/?n=788. But I can't find any
> example
> code. By just reading the manual, I don't quite get how it works,
> probably
> because I don't have network programming background.
> I'd like to know if there is any example programs. It don't need to be
> very
> complicated, a very simple one is good enough. For example, RCX A
> (sender)
> send something to RCX B (receiver), then RCX B response by beeping.

I've attached my lnpmsg.h and .c files in case they help. Most of what they contain is already in the HOWTO, but maybe the extra code will be useful to you.

I haven't programmed for legOS for a couple of years, so I'm a bit rusty, but hopefully I haven't forgotten everything. (I keep meaning to get back into it, but life continues to intervene.)

The best approach is to take the packet_handler function as-is. As I wrote in the HOWTO, you don't want to do anything complicated or lengthy in the packet handler, just copy the message someplace safe and mark a flag that it has arrived. You can then have a thread which waits for the gNewData variable to be set, and then acts. (Code for this thread is in the HOWTO section you pointed out, near the end.) Sending the message is fairly easy, you just use lnp_addressing_write, or the my_addressing_write function in lnpmsg.c which makes sure everything is thread-safe and also makes sure the message is actually sent (although it can't make sure that it's been recieved). So what you can do is something like this on the sending end:

char msg = kBeepMsg; // define kBeepMsg somewhere as a number or
// character to mark the 'beep' message
my_addressing_write(&msg, sizeof(char), other_rcx_address, my_source_port);

And on the recieving end, in your PacketWatcher function, see if the first char of the incoming message is kBeepMsg and act accordingly.

The only thing left is addressing, which is a bit complicated as I never did RCX-to-RCX communication. But the basic idea is you set the address in your legOS kernel somewhere. (I forget where exactly....) Then you have two copies of the kernel, each with a different LNP address. Upload one to one RCX and the other to your other RCX. Then in your program, you can #define the address of the other RCX as you can see in the lnpmsg.c file. Once they both have unique addresses and know the address of the other one, they should be able to communicate.

Hopefully this will get you started. Network programming can be either simple or complicated, depending on what you want to do. Once you get the basic messaging working and you work on more complicated things, keep in mind that in LNP your messages are guaranteed to be uncorrupted, but that they are not all guaranteed to actually arrive. Write your code with the idea that some of your messages will not make it through.

If you have other questions, please feel free to write back.

I'm so sorry... /(-__-)\