package serial;

/*-----------------------------------------------------------------------------
 * File: TermTest.java
 * Function: Simple console terminal program to demonstrate serial port
 *           interface for Java using the SerialPort class.
 *
 * Copyright (c) 1996,1997,1998 Solutions Consulting, All Rights Reserved.
 *---------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
 * Solutions Consulting makes no representations or warranties about the
 * suitability of the software, either express or implied, including but
 * not limited to the implied warranties of merchantability, fitness for
 * a particular purpose, or non-infringement. Solutions Consulting shall
 * not be liable for any damages suffered by licensee as a result of
 * using, modifying or distributing this software or its derivatives.
 *---------------------------------------------------------------------------*/
import java.io.*;
import Serialio.*;

public class TermTest {
	static String banner = "Copyright (c) 1996,1997,1998 Solutions Consulting, All Rights Reserved.";
	static String ver = "TermTest Ver 1.2";

	public static void main(String args[]) {
	SerialPortLocal sp;
	String devName = "";

	if (args.length == 0){
		PrintUsage();
		System.exit(1);
	}
	else {
		devName = args[0];
	}

	try {
    	SerialConfig serCfg = new SerialConfig(devName);
		sp = new SerialPortLocal(serCfg);
		// settings
		//sp.setBitRate(SerialConfig.BR_1200); 
		sp.setBitRate(SerialConfig.BR_9600); 
        sp.setDataBits(SerialConfig.LN_8BITS); 
        sp.setStopBits(SerialConfig.ST_1BITS); 
        sp.setParity(SerialConfig.PY_NONE); 
        //sp.setHandshake(SerialConfig.HS_CTSRTS); 
        sp.setHandshake(SerialConfig.HS_NONE); 
        // 
		
		sp.setTimeoutRx(1000);
		sp.setDTR(true); //some modems will not respond if DTR is low
		int verLib = sp.getLibVer();
		short vMajor = (short)(verLib >> 16);
		short vMinor = (short)(verLib & 0xffff);
		System.out.println("DLL version: "+vMajor+"."+vMinor);
		TermRcvTask rcv = new TermRcvTask(sp);
		TermSndTask snd = new TermSndTask(sp);
		rcv.start();
		snd.start();
		System.out.println("\nUse ^C to exit...");
		do {
			try { Thread.sleep(10000); } catch (InterruptedException se) {}
			} while(true);
		}
	catch (Exception ioe) {
		System.out.println(ioe);
		System.exit(1);
		}
  	}

	static int PrintUsage() {
		System.out.println(ver);
		System.out.println(banner);
		System.out.println("\nThis is a very simple serial port terminal program written in Java.");
		System.out.println("It uses native methods to communicate with the serial port.");
		System.out.println("The parameters are 9600 bps, No Parity, 8 data bits, 1 stop bit.");
		System.out.println("\nEnter the serial device name as the first parameter.");
		System.out.println("To use COM2, enter this command: java TermTest COM2");
		System.out.println("\nNote: If you use this program with a modem you may");
		System.out.println("want to issue the ATE0 command to cancel modem echo");
		return 0;
	}
}

/*-----------------------------------------------------------------------------
 * This thread reads the Recieve buffer and displays the values on the
 * console as characters
 *---------------------------------------------------------------------------*/
class TermRcvTask extends Thread {
	TermRcvTask(SerialPortLocal sp) throws IOException
		{
		if (sp.getPortNum() == -1)
			throw new IOException("TermSndTask: serial port not initialized");
		p = sp;
		}

	public void run() {
		int b;

		for (;;) {
			try {
				while((b = p.getByte()) != -1)
					System.out.print((char)b);
				}
			catch (Exception e) {
				System.out.println("Error in TermRcvTask "+e);
		 		}
			}
		}

	SerialPortLocal p;
}

/*-----------------------------------------------------------------------------
 * This thread reads the keyboard for input and sends the keystrokes to
 * the serial port transmit buffer
 *---------------------------------------------------------------------------*/
class TermSndTask extends Thread {
	TermSndTask(SerialPortLocal sp) throws IOException
		{
		if (sp.getPortNum() == -1)
			throw new IOException("TermRcvTask: serial port not initialized");
		p = sp;
		}

	public void run() {
		int b;
		for (;;) {
			try {
//				System.out.println("Input...");
				b = System.in.read(); // blocks if input not available
//				System.out.println("PutByte("+b+")");
				p.putByte((byte)b);
				}
			catch (Exception e) {
				System.out.println("Error in TermSndTask "+e);
				}
			}
		}

	SerialPort p;
}