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

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



                 34     CHAPTER 2  ■  SERIAL COMMUNICATION



                        than the standard serial port, and two, all of our implementing classes would not use this port—
                        they would instead need to use the connector to the WebSerialPort: the WebSerialClient.
                            The class WebSerialClient implements the interface JSerialPort, so now anything I write
                        can just use this interface and I can interchange the WebSerialClient, the StandardSerialPort,
                        or my own implementation and I will not have to modify any of the code that uses these. Pretty
                        slick, huh?
                            The first field in this class is a formatter object. I use this for debugging the milliseconds of
                        an operation. You will need to debug and experiment with different timeout values for items
                        connected to your microcontroller. Too long and you’ll be wasting performance, too little and
                        you’ll get nothing back or only junk. I’ll discuss more of this in Chapter 4, but it’s probably best
                        to make a note of it for now.
                            The string URL and the URL object will be used to connect to the webcom.jsp on my robot.
                        The dtr variable is set to false if I am connecting to a BASIC Stamp carrier board. The timeout
                        and MAX_DELAY are ints that will determine whether the client should wait for a return from
                        webcom.jsp, or whether it should just make two calls: one write and one read. I’ve found that
                        depending on your WiFi connection, you might want to increase or decrease this value.
                            Next, the construction of this client will take the string server that represents the server
                        name or IP address of the web server hosting the webcom.jsp. The string, tcpPort, represents
                        the port where the web server hosting the webcom.jsp is listening. (See Example 2-8.)


                        Example 2-8. WebSerialClient.java
                        package com.scottpreston.javarobot.chapter2;

                        import java.io.BufferedReader;
                        import java.io.InputStreamReader;
                        import java.net.URL;
                        import java.text.SimpleDateFormat;
                        import java.util.Date;

                        public class WebSerialClient implements JSerialPort {

                            private SimpleDateFormat formatter = new SimpleDateFormat(
                                    "MM/dd/yy - HH:mm:ss.SSS");
                            private String url;
                            private URL myUrl;
                            private boolean dtr = false;
                            private int timeout = 0;
                            public static final int MAX_DELAY = 500;

                            public WebSerialClient(String server, String tcpPort, String portId) {
                                this.url = "http://" + server + ":" + tcpPort
                                + "/" + WebSerialPort.COMM_JSP + "?portid=" + portId;
                            }
   48   49   50   51   52   53   54   55   56   57   58