78 captures
20 Feb 2003 - 03 Oct 2016
May JUN Jul
07
2003 2004 2005
success
fail

About this capture

COLLECTED BY

Organization: Alexa Crawls

Starting in 1996, Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the Wayback Machine after an embargo period.

Collection: Alexa Crawl DX

Crawl DX from Alexa Internet. This data is currently not publicly accessible.
TIMESTAMPS

The Wayback Machine - http://web.archive.org/web/20040607051120/http://www.onjava.com:80/onjava/javacook/solution.csp?day=1
 
ONJava.com -- The Independent Source for Enterprise Java
O'Reilly Network.oreilly.comSafari Bookshelf.Conferences. Sign In/My Account | View Cart   
Articles Weblogs Books Learning Lab  

Join us at the O'Reilly Open Source Convention, July 26-30, in Portland, OR.

Traveling to
a tech show?

Las Vegas Hotels
Discount Hotels
Hotel Discounts
Chicago Hotels
California Hotels
Canada Hotels
New York City Hotels




ONJava.com
supported by:

Student Loan
   Consolidation
Auto Parts Online



   Print.Print
Email.Email article link
The Java Cookbook (cover)

Java Recipe of the Day

The following recipe is from The Java Cookbook, by Ian Darwin. All links in this recipe point to the online version of the book on the Safari Bookshelf.

Buy it now, or read it online on the Safari Bookshelf.


You want to read and write on a port, and your communications needs are simple.

Just use read and write calls.

Suppose you need to send a command to a device and get a response back, and then send another, and get another. This has been called a "lock-step" protocol, since both ends of the communication are locked into step with one another, like soldiers on parade. There is no requirement that both ends be able to write at the same time (see Recipes 10.7 and 10.8 for this), since you know what the response to your command should be and don't proceed until you have received that response. A well-known example is using a standard Hayes-command-set modem to just dial a phone number. In its simplest form, you send the command string ATZ and expect the response OK, then send ATD with the number, and expect CONNECT. To implement this, we first subclass from CommPortOpen to add two functions, send and expect, which perform reasonably obvious functions for dealing with such devices. See Example 11-5.

import java.awt.*;
import java.io.*;
import javax.comm.*;
import java.util.*;

/**
 * Subclasses CommPortOpen and adds send/expect handling for dealing
 * with Hayes-type modems.
 *
 */
public class CommPortModem extends CommPortOpen {
    /** The last line read from the serial port. */
    protected String response;
    /** A flag to control debugging output. */
    protected boolean debug = true;

    public CommPortModem(Frame f)
        throws IOException, NoSuchPortException,PortInUseException,
            UnsupportedCommOperationException {
        super(f);
    }

    /** Send a line to a PC-style modem. Send \r\n, regardless of
     * what platform we're on, instead of using println(  ).
     */
    protected void send(String s) throws IOException {
        if (debug) {
            System.out.print(">>> ");
            System.out.print(s);
            System.out.println(  );
        }
        os.print(s);
        os.print("\r\n");

        // Expect the modem to echo the command.
        if (!expect(s)) {
            System.err.println("WARNING: Modem did not echo command.");
        }

        // The modem sends an extra blank line by way of a prompt.
        // Here we read and discard it.
        String junk = is.readLine(  );
        if (junk.length(  ) != 0) {
            System.err.print("Warning unexpected response: ");
            System.err.println(junk);
        }
    }

    /** Read a line, saving it in "response". 
     * @return true if the expected String is contained in the response, false if not.
     */
    protected boolean expect(String exp) throws IOException {
        response = is.readLine(  );
        if (debug) {
            System.out.print("<<< ");
            System.out.print(response);
            System.out.println(  );
        }
        return response.indexOf(exp) >= 0;
    }
}

Finally, Example 11-6 extends our CommPortModem program to initialize the modem and dial a telephone number.

import java.io.*;
import javax.comm.*;
import java.util.*;

/**
 * Dial a phone using the Java Communications Package.
 *
 */
public class CommPortDial extends CommPortModem {

    protected static String number = "000-0000";

    public static void main(String[] ap)
        throws IOException, NoSuchPortException,PortInUseException,
            UnsupportedCommOperationException {
        if (ap.length == 1)
            number = ap[0];
        new CommPortDial().converse(  );
        System.exit(0);
    }

    public CommPortDial(  ) 
        throws IOException, NoSuchPortException, PortInUseException,
            UnsupportedCommOperationException {
        super(null);
    }

    protected void converse(  ) throws IOException {

        String resp;        // the modem response.

        // Send the reset command
        send("ATZ");

        expect("OK");

        send("ATDT" + number);

        expect("OK");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // nothing to do
        }
        is.close(  );
        os.close(  );
    }
}

View the past week's recipes: Today | Yesterday | 3 days ago | 4 days ago | 5 days ago | 6 days ago | A week ago



Sponsored by:



Contact Us | Advertise with Us | Privacy Policy | Press Center | Jobs

Copyright © 2000-2004 O坦eilly Media, Inc. All Rights Reserved.
All trademarks and registered trademarks appearing on the O'Reilly Network are the property of their respective owners.

For problems or assistance with this site, email