Recipe 12.5 Reading and Writing: Lock-Step


Problem

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

Solution

Just use read and write calls.

Discussion

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 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 12-5.

Example 12-5. CommPortModem.java
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 12-6 extends our CommPortModem program to initialize the modem and dial a telephone number.

Example 12-6. CommPortDial.java
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( );     } }



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net