// ComPortAPI.java
//
// Copyright (C) 1998 Henrik Bjorkman. No responsibilities taken.
// All rights reserved.
//
// This class uses the Java Communications API to initialize serial
// ports.
//
// Credits
//
// SUNs Java Communications API and jdk 1.1.5 was used when 
// developing this class.
//
//
// History
//
// 1998-05-20 Created by Henrik Bjorkman
// 1998-06-01 Added package. /Henrik
// 1998-10-27 Added BufferedWriter out. /Henrik
// 1999-06-19 Renamed from ComPort to ComPortAPI and made 
//            it a subclass to new abstract class ComPort.
// 1999-07-10 Adapted for Java Communications API 2.0. /Henrik
// 2000-06-13 Changed package name to chartplotter_package.
//

package chartplotter_package;

import java.io.*; 
import java.util.*;
import javax.comm.*;

public class ComPortAPI extends ComPort
{
  SerialPort serialPort;
  public BufferedReader in;
  public BufferedWriter out;

  static public void error(String s)
  {
    System.err.println("ComPortAPI: " + s);
    System.exit(0);
  }

  static public void debug(String s)
  {
    //System.out.println("ComPortAPI: " + s);
  }

  public ComPortAPI(String port_name) 
  {
    CommPortIdentifier portId=null;

    debug("serialOpen");
    debug("system "+System.getProperty("os.name"));

    /* First find a comm port */

    Enumeration portList = CommPortIdentifier.getPortIdentifiers();

    if (port_name!=null) {debug("looking for "+port_name);}

    while (portList.hasMoreElements()) 
    {
      CommPortIdentifier pi = (CommPortIdentifier) portList.nextElement();
      if (pi.getPortType() == CommPortIdentifier.PORT_SERIAL) 
      {
        debug("found port "+pi.getName());
        if ((port_name==null) || (pi.getName().equals(port_name)))
        {
          /* Found a port */
          portId=pi;
          break;
        }
      }
    }

    if (portId==null) {error("Port not found (Port name is case sensitive)");}

    /* A port has been found, now its time to open it. */
    try {
      serialPort = (SerialPort) portId.open("ChartPlotter", 2000);
    } catch (PortInUseException e) {error("PortInUseException " +e);}

    /* Create streams for reading and writing to the ports. */
    try {
      in=new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
      out=new BufferedWriter(new OutputStreamWriter(serialPort.getOutputStream()));
    } catch (IOException e) {error("IOException " +e);}

    /* Set port mode to 4800 baud, 8 data, 1 stop bit and no parity. */
    try {
      serialPort.setSerialPortParams(4800,
       SerialPort.DATABITS_8,
       SerialPort.STOPBITS_1,
       SerialPort.PARITY_NONE);
    } catch (UnsupportedCommOperationException e) {error("UnsupportedCommOperationException " +e);}

    System.out.println("using port "+portId.getName()+" at 4800 baud, 8 data, 1 stop and no parity");
  }

  public String readLine()
  {
    String line=null;
    try 
    {
      line=in.readLine();
    }
    catch( IOException e ) 
    {
      error("readLine " + e );
    }  

    return(line);
  }

  public void serialClose() 
  {
    debug("serialClose");
    try 
    {
      if (in != null)
      {
        debug("close");
  	  in.close();
	  in=null;
      }
    }
    catch( IOException e ) 
    {
      error("close " + e );
    }  
  }
}

