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

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



                 40     CHAPTER 2  ■  SERIAL COMMUNICATION



                            public static void main(String[] args) {
                                try {
                                    StampSerialTest sst = new StampSerialTest();
                                    System.out.println("From Stamp:" + sst.test((byte)101));
                                    System.out.println("From Stamp:" + sst.test((byte)102));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    System.exit(1);
                                }
                                System.out.println("Done.");
                            }


                        }

                        Section Summary
                        The Stamp program and Java class created in this section were

                            • SerialEcho.bs2: This program resides in the microcontroller and will echo an “a” and “b”
                              depending on the commands sent to the controller.

                            • StampSerialTest.java: This class is designed to send and receive specific information to
                              the microcontroller on coordination with SerialEcho.bs2.
                            Once you have tested basic access to your microcontroller, now it’s time to extend on this
                        concept a bit and model your microcontroller, first by creating a generic one that can be used
                        for any type of microcontroller, and then by creating a version that corresponds precisely to
                        your Stamp program.


                        2.5 Modeling a Microcontroller with Java

                        While I could use the StandardSerialPort to send and receive data to and from a robot, this
                        makes it more difficult down the road as the robot’s capability increases. So, to begin you will
                        need a microcontroller. I choose the Parallax BASIC Stamp because it’s easy to program and
                        there are lots of examples and support available for this model.
                            The first Parallax BASIC Stamp comes in nine different versions, not including the OEM
                        versions. They come with 8, 16, and 32 I/O pins that operate from 4MHz to 50MHz and can be
                        programmed with up to 12,000 instructions. They are quite versatile and there are plenty of
                        examples of software on the Internet, as well as those provided from the manufacturer’s web
                        site at www.parallax.com.
                            The two pictured in Figure 2-6 and Figure 2-7 are the BASIC Stamp 2 and the Javelin Stamp.
                        The Javelin Stamp is the Java version. You can find all the program examples in this book for
                        the Javelin Stamp in Appendix 2. All other examples will be for the BASIC Stamp 2 since it’s the
                        most popular and, as I said earlier, there are more examples for this microcontroller at present.
   54   55   56   57   58   59   60   61   62   63   64