Page 212 - The Ultimate Palm Robot
P. 212

Color profile: Generic CMYK printer profile
           Composite  Default screen
                                                          Bots /The Ultimate Palm Robot/ Mukhar & Johnson / 222880-6 / Chapter 7






                                                  Chapter 7 Essential Robot Programming Strategies   195



                                    command (BD1AD1) into the character array. The code then sets the last char-
                                    acter of the array to be \0. The puts( ) function call sends a string to an output
                                    screen managed by PocketC. You can see the output by tapping the Output
                                    button on the main PocketC screen. The sersenda( ) function outputs a charac-
                                    ter array to the serial port. The serial port must already be open for this com-
                                    mand to work; we will see the command to open the serial port in the main( )
                                    function. The arguments to the sersenda( ) function are the character array
                                    and the number of characters to send. The serdata( ) function returns the
                                    number of bytes waiting to be read on the serial port. If there is data on the se-
                                    rial port, the code reads it with a call to serrecva( ). The arguments for this
                                    function are the character array to store the data in, and the number of bytes to
                                    read. To ensure that the character array can be converted to a string, the code
                                    sets the array element following the last byte to 0. The code then calls the
                                    ctostr( ) function. This converts the character array to a string and assigns the
                                    result to our int variable x. Unlike standard C, PocketC allows you to assign a
                                    string to an int. If the string starts with a number, the number is assigned to the
                                    variable. Thus, you don’t need a special function to convert a string to an int.
                                    The value of x is then sent to the Output display and to the screen of the PDA.
                                      Following the readSensor( ) function is the doJig( ) function. This function
                                    uses the sersenda( ) function to send the command to the serial port. The se-
                                    rial port must already be open for sersenda( ) to work. The sleep( ) function
                                    stops the program for the given number of milliseconds. One thousand milli-
                                    seconds is one second.
                                      The main( ) function is what PocketC calls to start the program. The pro-
                                    gram starts by declaring some variables, calling the initCmds( ) function, and
                                    setting up the display. The next important function call is the call to open the
                                    serial port:


                                        seropen(9600, "8N1N", 10);

                                      As seen in the code snippet above, the function seropen( ) opens the serial
                                    port. The parameters set the serial port to 9600 baud, 8 data bits, no parity, 1
                                    stop bit, and no flow control. The final parameter (10) is a timeout value in
                                    hundredths of a second.
                                      After opening the serial port, the main( ) function draws two buttons on
                                    the screen for starting and stopping the program. When the user taps the Start
                                    button, the program starts a loop that calls the readSensor( ) function, and










           P:\010Comp\Bots\880-6\ch07.vp
           Monday, May 12, 2003 4:29:29 PM
   207   208   209   210   211   212   213   214   215   216   217