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

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



                 20     CHAPTER 2  ■  SERIAL COMMUNICATION



                            • Methods that work well for sending and receiving data packets to and from the
                              microcontroller.

                        Code Discussion

                        The first item to create is a serial port interface. The Java interface is a means of hiding different
                        implementations with the same behavior. For now I’ll create serial port access to a local port,
                        but later I’ll want to provide the same behavior over a network.
                            The first thing you notice from the interface is that it contains no logic, just method stubs.
                        This is because an interface is only there to define behavior, not to implement any of the behavior.
                        The actual work will be implemented by another class that implements this interface.
                            The second thing you notice are the names of the methods. For instance, the read() method
                        will return a byte array. The readString() method will return a String. The write method will
                        return nothing and will take a byte array since it’s only an input parameter. There is also a
                        close() method for freeing resources and resending ownership so that other classes or programs
                        can access the implementing object.
                            I have added two serial port accessor methods: setDTR for use with the Parallax Stamp
                        carrier boards, and setTimeout as a means to assist in the data packet synchronization with the
                        connected microcontroller.
                            The three static strings are for use with the WebSerialClient defined later in this chapter.
                        The read() and readString() have methods with input parameters because (depending on the
                        timeout value) sometimes it’s better to wait for the response within the serial port class rather
                        than calling Thread.sleep() externally and then calling two separate write() and read()
                        methods. (See Example 2-3.)

                        Example 2-3. JSerialPort.java
                        package com.scottpreston.javarobot.chapter2;

                        public interface JSerialPort{

                            public byte[] read();
                            public String readString();
                            public void write(byte[] b) throws Exception;
                            public void close();
                            public void setDTR(boolean dtr);
                            public void setTimeout(int tOut);
                            public static final String READ_COMMAND = "r";
                            public static final String WRITE_COMMAND = "w";
                            public static final String WRITE_READ_COMMAND = "wr";
                            public byte[] read(byte[] b) throws Exception;
                            public String readString(byte[] b) throws Exception;

                        }

                            Now that I have created the generic interface for all the serial ports I am going to use in this
                        book, it’s time to write our first implementation class, StandardSerialPort. Figure 2-3 shows the
                        class diagram for JSerialPort and StandardSerialPort.
   34   35   36   37   38   39   40   41   42   43   44