 1Content-type: application/x-Unknown; charset=UTF8package nmc.sidekick;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

import javax.comm.CommPort;
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;

import com.ajile.jem.rawJEM;

public class xr16c2850 {

    public static int bkpts = 0;
    public static void BkptHere() {
        bkpts++;
    }

    /**
     * Starts the application.
     * @param args an array of command-line arguments
     */
    public static void main(java.lang.String[] args) {
        DebugMessage.println("Hello, this is a test for the xr16c2850");
        try {
            CommPortIdentifier commPort3 = CommPortIdentifier.getPortIdentifier("com3");
            SerialPort serialPort3 = (SerialPort)commPort3.open("xy3", 0);
            OutputStream ostream3 = serialPort3.getOutputStream();
            InputStream istream3 = serialPort3.getInputStream();

            CommPortIdentifier commPort4 = CommPortIdentifier.getPortIdentifier("com4");
            SerialPort serialPort4 = (SerialPort)commPort4.open("xy4", 0);
            OutputStream ostream4 = serialPort4.getOutputStream();
            InputStream istream4 = serialPort4.getInputStream();

            DebugMessage.println("opened com3 and com4");
            DebugMessage.println("setting XR16C2850 port into internal loopback");
            BkptHere();
            rawJEM.setField(1, 0x00900010, 4, 1);
            rawJEM.setField(1, 0x00a00010, 4, 1);
            BkptHere();

            DebugMessage.println("now sleeping 2 seconds");
            Thread.sleep(2000);


            String s = "howdy, partner";
            String t = "It's a lovely day in the neighborhood.";
            DebugMessage.println("Sending string: " + s);
            int totLen3 = s.length();
            int totLen4 = t.length();
            ostream3.write(s.getBytes());
            ostream4.write(t.getBytes());
            int a = 0;
            int lasta = 0;
            int b = 0;
            int lastb = 0;
            while ((lasta < totLen3) || (lastb < totLen4)) {
                a += istream3.available();
                b += istream4.available();
                if (a != lasta) {
                    DebugMessage.println("Just got " + (a - lasta) + " new bytes on com3");
                }
                if (a != lasta) {
                    DebugMessage.println("Just got " + (b - lastb) + " new bytes on com4");
                }
                lasta = a;
                lastb = b;
            }
            byte[] rcvd3bytes = new byte[totLen3];
            byte[] rcvd4bytes = new byte[totLen4];
            int r3 = istream3.read(rcvd3bytes);
            int r4 = istream4.read(rcvd4bytes);
            String rcvd3str = new String(rcvd3bytes);
            String rcvd4str = new String(rcvd4bytes);
            DebugMessage.println("Received string: " + rcvd3str + " on com3");
            DebugMessage.println("Received string: " + rcvd4str + " on com4");

        } catch ( NoSuchPortException e) {
            DebugMessage.println("got NoSuchPortException exception: " + e);
        } catch ( PortInUseException e ) {
            DebugMessage.println("got PortInUseException exception: " + e);
        } catch ( IOException e ) {
            DebugMessage.println("got IOException exception: " + e);
        } catch ( InterruptedException e) {
            DebugMessage.println("got InterruptedException exception: " + e);
        } catch ( Exception e) {
            DebugMessage.println("got exception: " + e);
        }
    }

}
