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

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



                 24     CHAPTER 2  ■  SERIAL COMMUNICATION



                                                // set config parms
                                                serialPort.setSerialPortParams(baud,
                                                        SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                                                        SerialPort.PARITY_NONE);
                                                serialPort
                                                        .setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
                                                Utils.pause(50);
                                                // config output stream
                                                outputStream = serialPort.getOutputStream();
                                                // config input stream
                                                inputStream = serialPort.getInputStream();
                                                // add events listener
                                                serialPort.addEventListener(this);
                                                serialPort.notifyOnDataAvailable(true);
                                                Thread.sleep(50); // waits till ports change state.
                                            }
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }


                            public byte[] read() {
                                while (!dataIn) {
                                    try {
                                        Thread.sleep(1);
                                    } catch (Exception e) {
                                    }
                                }
                                dataIn = false;
                                return readBuffer;
                            }

                            public String readString() {
                                byte[] b = read();
                                StringBuffer s = new StringBuffer();
                                for (int i = 0; i < b.length; i++) {
                                    if (b[i] != 0) {
                                        int in = (int) b[i];
                                        if (in < 0) {
                                            in = in + 256;
                                        }
                                        s.append(in);
                                        s.append("~");
                                    }
                                }


                   97022d2480fe4a63cfdfa123a6e70098
   38   39   40   41   42   43   44   45   46   47   48