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

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



                 42     CHAPTER 2  ■  SERIAL COMMUNICATION


                        Code Objective

                        The objective for this example is to simplify communication for my robot by modeling a Stamp
                        program in Java.


                        Code Discussion
                        The first interface I’ll create will be called JController. This interface will have three execute()
                        methods and one close() method.
                            The only difference between the two execute methods will be the return type. In execute(),
                        the return type will be a string. This will call the readString() method from the JSerialPort. In
                        execute2(), the return type will be a byte[]. This will call the read() method from the JSerialPort.
                        (See Example 2-11.)

                        Example 2-11. JController.java Interface
                        package com.scottpreston.javarobot.chapter2;

                        public interface JController {

                            public String execute(byte[] cmd, int delay) throws Exception;
                            public byte[] execute2(byte[] cmd, int delay) throws Exception;
                            public void close();
                        }
                            Next, I’ll write the implementation for the interface defined in the preceding example.
                        As I began writing implementation classes for the Javelin Stamp, and six versions of the BASIC
                        Stamp, I found myself repeating a lot of the same calls to the execute methods. So, rather than
                        writing them seven times, I decided to create a master controller that would implement the
                        functionality of all of them. It will also be able to handle any new controllers that might come
                        up in later chapters. I’ll call this class Controller, and so that the actual implementation classes
                        are created, I will make it generic.
                            This class has a single field, serialPort of type JSerialPort. The constructor of this class will
                        take the JSerialPort interface so that it can use either the WebSerialClient or the StandardSeri-
                        alPort. I also set the DTR to false since none of the controllers I use will use this, and for the
                        Stamp Carrier Boards having this set to true will put the Stamp into program mode.
                            The first method, execute(), will return a string. The first thing that is checked for is the
                        instance of the JSerialPort. If it’s of type WebSerialClient, then I want to put the delay in the
                        serial port on the web server. This is due to a timing lag between the executing machine and the
                        web server connection. I set the maximum delay in the WebSerialClient class and if the delay is
                        less than or equal to this number, the delay will be set in the WebSerialClient; otherwise, I will
                        call Utils.pause() and make two calls to the client. Also, in both cases I need to check to see if
                        the delay is zero. If that’s the case, I don’t want to ready anything; I just want to write. (See
                        Example 2-12.)
   56   57   58   59   60   61   62   63   64   65   66