The TINI Internet Interface
by Al Williams


Listing One
// TINI sender class
import java.io.*;
import java.net.*;
import javax.comm.*;

public class SerialSender
  {
  SerialPort rs232;
  Socket sock;
  OutputStreamWriter os;
  InputStream is;
  String msg;
  public static void main(String args[])
   {
   if (args.length!=2)   // check arguments and go
     {
     System.out.println("Usage: SerialSender host port");
     System.exit(9);
     }
   new SerialSender().go(args);
   }
  void procError(String s) // Note: Tini Exceptions don't return messages
   {
   System.out.println("Error");
   System.out.println(s);
   System.exit(1);
   }
// main routine
  public void go(String args[])
    {
// Open socket
    try 
      {
        System.out.println("Connecting to " + args[0] +":"+ args[1]);
        sock=new Socket(args[0],Integer.parseInt(args[1]));
      }
    catch (Exception e)
      {
      procError("Can't open Socket");
      }
// Connect socket to stream
    try
      {
      os=new OutputStreamWriter(sock.getOutputStream());
      os.write("Start\r\n",0,7);
      os.flush();
      }
    catch (Exception e)
      {
      procError("Can't open stream");
      }
// Open RS232
     try
        {
// Use this code to open the RS232 port
        CommPortIdentifier cpi;
        cpi=CommPortIdentifier.getPortIdentifier("serial0");
        if (cpi==null) procError("Can't find SERIAL0");
        rs232 = (SerialPort)cpi.open("SSend",1000);
        rs232.setSerialPortParams(9600,SerialPort.DATABITS_8,
           SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
        is=rs232.getInputStream();
// Or... Just use stdin
//          is=System.in;
        }
        catch (Exception e)
         {
         procError("Can't open RS232 port " +msg);
         }
// main loop
    while (true) 
      {
      int c;
      try
        {
        c=is.read();
        }
      catch (Exception e) 
        {
        continue; // ignore rs232 exceptions
        }
      try
        {
        if (c==13) c=10;  // cr to LF
        os.write(c);
        if (c==10) os.flush();
        }
      catch (Exception e)
        {
        procError("Write error");
        }
      }
     }
    }


Listing Two
// Host program
import java.io.*;
import java.net.*;
public class SerialRcv
  {
  ServerSocket ssock;
  BufferedReader br;
  Socket sock;
  public static void main(String args[])
    {
    if (args.length!=1)  // check arguments and go
      {
      System.out.println("Usage: SerialRcv port");
      System.exit(1);
      }
    new SerialRcv().go(args);
    }
// main routine
  public void go(String args[])
    {
// get a connection
    try
      {
      ssock=new ServerSocket(Integer.parseInt(args[0]));
      System.out.println("Listening on port " + args[0]);
      sock=ssock.accept();
      ssock.close(); // quit listening
      System.out.println("Status: Connection established");
      br=new BufferedReader(
         new InputStreamReader(
          sock.getInputStream()));
      }
    catch (Exception e)
      {
      System.out.println("Can't open or connect server socket");
      System.exit(1);
      }
// main loop
    while (true)
      {
      try
        {
        String s=br.readLine();
        if (s==null) break;
        if (s.length()==0) continue;  // no blank lines
        System.out.println(s);
        }
       catch (Exception e)
        {
        System.out.println("Read error");
        System.exit(2);
        }
      }
    }
  } 


    
    
    



3

