// ComPortProcess.java
//
// Copyright (C) 1998 Henrik Bjorkman. This is free software you 
// may use, copy and distribute this program freely. But you
// must do it on your own risk. No responsibilities taken and
// all rights reserved.
//
// This class is an interface between a java program and a serial 
// port. It works on Windows NT and it should work on most unix
// systems but it does not work on Win95.
//
//
// Credits
//
// SUNs jdk 1.1.5 was used when developing this program.
//
// I had help by looking at how these things where done in a program 
// called "Elgaard Positioning System" when creating this program.
//
//
// History
//
// 1998-05-20 Created by Henrik Bjorkman
// 1998-06-02 Added package. /Henrik
// 1999-05-10 Setting baudrate dont work when running under Linux.
//            Writing a message about how it can be done manualy. 
// 1999-06-19 Renamed from ComPort to ComPortProcess and made
//            it a subclass to new abstract class ComPort.
// 1999-09-05 Added waitFor. Perhaps it will work better now. /Henrik
// 2000-06-13 Changed package name to chartplotter_package.

package chartplotter_package;

import java.io.*;


class ComPortProcess extends ComPort
{
  String portName;
  File file;  
  FileInputStream in;

  static void error(String s)
  {
    System.err.println("ComPortProcess: " + s);
    System.exit(0);
  }

  static void debug(String s)
  {
    //System.out.println("ComPortProcess: " + s);
  }

  static void println(String s)
  {
    System.out.println(s);
  }

  /* This function will only tell user how to set baudrate */
  public void setBaudRate()
  {
    debug("setBaudRate");
    try 
    { 
      String init=null;

      if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS")>=0) 
      {
        init = "MODE.COM " + portName + " 4800,N,8,1 ";
        println("Remember to set baudrate before starting this program.");
        println("It is done like this:");
        println(init);
      } 
      else 
      {
        String s="stty 4800 pass8 -parenb  -inlcr  ixany -ixon < ";
        init = "/bin/sh " + "-c " + s + portName;

        println("Remember to set baudrate before starting this program.");
        println("It is done like this:");
        println(s+portName);
        println("Probably it is enough with just:");
        println("stty 4800 < "+ portName);
        println("You need to be root to use the serial port.");
      }
      println("If it still don't work try to install the Java Communications API.");

/*
      if (init!=null)
      {
        Process pid=Runtime.getRuntime().exec(init);
        InputStream in = new DataInputStream(pid.getInputStream());
        while (true) 
        {
          int ch=1;
          ch = in.read();
          if (ch<0) break;
          System.out.print((char)ch);
        }
        pid.waitFor();
      }
*/
    } 
    /*catch(IOException e) 
    {
      error("IOException " + e);
    }*/
    catch(SecurityException e) 
    {
      error("SecurityException "+ e);
    }

  }

  public void defaultPortName()
  {
    try 
    { 
      if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS")>=0) 
      {
        portName="COM1";
        println("Windows "+portName);
      } 
      else 
      {
        portName="/dev/cua0";
        println("Unix"+portName);
      }
    } 
    catch(SecurityException e) 
    {
      error("SecurityException "+ e);
    }
  }


  public ComPortProcess(String port_name) 
  {
    portName=port_name;
    if (portName==null) {defaultPortName();}
    setBaudRate();

    try
    {
      file = new File(portName);
      in = new FileInputStream(file);
    } 
    catch(IOException e) 
    {
      error("" + e);
    }
  }


  public String readLine()
  {
    StringBuffer line=new StringBuffer();

    try 
    {
      while(true)
      {
        int ch;
        ch=in.read();
        if ((ch=='\r') || (ch=='\n'))
        {
          break;
        }
        else if (ch>0)
        {
          line.append((char)ch);
        }
        else
        {
          synchronized (this) {try {this.wait(50);}  catch (InterruptedException e) {;}}
        }
      }
    }
    catch( IOException e ) 
    {
      error("read " + e );
    }

    debug(line.toString());
    return(line.toString());
  }

  public void serialClose() 
  {
    debug("serialClose");
    try 
    {
      if (in != null)
      {
        debug("close");
        in.close();
        in=null;
      }
    }
    catch( IOException e ) 
    {
      error("close " + e );
    }  
  }
}

