Page 57 - The Definitive Guide to Building Java Robots
P. 57

Preston_5564C02.fm  Page 38  Wednesday, September 14, 2005  5:42 AM



                 38     CHAPTER 2  ■  SERIAL COMMUNICATION




                        ■Note  If you’re just starting out with robotics, it’s great to have this bread board for adding sensors or
                        creating circuits.



                            Once your board is hooked up and you can see the microcontroller you’ve connected,
                        you’re ready to program. Your first goal will be to just send some data to the microcontroller
                        and get some data in return.

                        Code Objective

                        The objectives for this example are to:
                            • Send 2 bytes of data to a microcontroller.
                            • Receive a specific byte back that confirms the byte sent to it.


                        Code Discussion
                        The Stamp program has a byte array of size 3 called serialIn. This byte array will be populated
                        from the SERIN command when it’s either full or when it receives a termination command of
                        “!”. Then, based on the first byte received from the PC, the program will branch to SEROUT
                        with an “a” or “b”. (See Example 2-9.)



                        ■Note  Adjust the serial port timeout or the time between the write and the delay. Then adjust the pause
                        time in the Stamp code and see how the results vary.




                        Example 2-9. SerialEcho.bs2
                        '{$STAMP BS2}
                        '{$PORT COM1}

                        serialIn     VAR   Byte(3)
                        foo          VAR   Byte

                        main:
                          foo = 255
                          serialIn(0) = 0
                          serialIn(1) = 0
                          serialIn(2) = 0
                          SERIN 16,16468,1000,main,[STR serialIn\3\"!"]
                          PAUSE 100
                          LOOKDOWN serialIn(0),[100,101,102],foo
                          BRANCH foo,[test1, test2, getCompass]
                          PAUSE 5
                          GOTO main


                   97022d2480fe4a63cfdfa123a6e70098
   52   53   54   55   56   57   58   59   60   61   62