Serin and serout

Ok when I got in to this I wanted to know how everything works and so now I have a hard question: I think. How does the serin and serout statement work, I know it’s a serial port command, but how do you decide what argument goes where? Like if I place something on the third argument, why does it go to the engines and not the relays? Where do I find out this stuff? Thanks.

The order of arguments for the Serin and Serout commands in the Robot Controller’s User CPU are determined by the internal structure of the RC, and the requested data from the Master CPU in your “DEFINE CONSTANTS FOR INITIALIZATION” section. Whatever constants you define as 1 in that section will be returned to you in your SERIN command IN THE ORDER THEY ARE LISTED IN THE CONSTANTS FOR INITIALIZATION SECTION OF THE DEFAULT CODE. (VERY IMPORTANT!) The SEROUT order is FIXED, and the comment above the command in the default code shows the order that the Output CPU is expecting the data. ANY change from this order will have unexpected results.

Take a look at the default code, that will show you the necessary order.

There are parts of the code marked " (DO NOT CHANGE)" Don’t change these parts of the code.

As to your questions, serin can be modified somewhat by the initializations you choose. You do this by choosing which variables the master CPU should send the STAMP2. Once you choose them (by putting 1’s in the appropriate place to make the c_variablename constant equal to 1), they are fixed until you download a new program to the STAMP2.

After choosing the varialbes you want to get sent, you have to actually have your serin command match the variables requested. The most common cause of the “basic init error” LED going red is that the STAMP2 program asks for a different number of variables with the serin command than it requested via the c_variablename constants.

As to the serout command, things are much more stable. Generally speaking you have nothing to change. The only change that is allowed is that if you are dying to save a few milliseconds, you can truncate the output series. I don’t recommend it because it usually means you are too close to the edge. If your code is that sensitive that this saves your program, you are probably going to go over the edge for some other reason very soon.

In the interest of full disclosure, I will show you how to truncate the serout command, even though I don’t recommend it. Suppose you only needed to output p1_y, relayA, p2_y, and relayB. In this case your serout command could be shortened to:

Serout USERCPU, OUTBAUD, [255,255,p1_y,relayA,p2_y,relayB]

Finally, note that there is no changing the pins that the STAMP2 uses to communicate with the Master CPU – for either the Serin command or the Serout command. This only makes sense since the pins are etched in copper and are difficult to move via software control :wink:

Joe J.