00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 package ikayaki.util;
00024
00025 import ikayaki.squid.*;
00026
00027 import java.io.IOException;
00028 import java.io.PrintStream;
00029 import java.text.DateFormat;
00030 import java.text.SimpleDateFormat;
00031 import java.util.Date;
00032
00038 public class SerialProxy {
00039
00040 private static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
00041
00042 public static void main(String[] args) {
00043 if (args.length != 2) {
00044 System.out.println("Forwards commands sent between two serial ports and logs them.");
00045 System.out.println("Usage: serialproxy <COM#> <COM#>");
00046 System.out.println("Example: serialproxy COM2 COM5");
00047 return;
00048 }
00049 SerialIO portOne;
00050 SerialIO portTwo;
00051
00052 try {
00053 portOne = SerialIO.openPort(new SerialParameters(args[0]));
00054 portTwo = SerialIO.openPort(new SerialParameters(args[1]));
00055
00056 System.out.println("Timestamp\tSender\tMessage");
00057 new Forwarder(portOne, portTwo, System.out);
00058 new Forwarder(portTwo, portOne, System.out);
00059
00060
00061 System.in.read();
00062
00063 } catch (SerialIOException e) {
00064 e.printStackTrace();
00065 } catch (IOException e) {
00066 e.printStackTrace();
00067 }
00068 }
00069
00070 private static class Forwarder implements SerialIOListener {
00071
00072 private SerialIO in;
00073 private SerialIO out;
00074 private PrintStream log;
00075
00076 public Forwarder(SerialIO in, SerialIO out) {
00077 this(in, out, null);
00078 }
00079
00080 public Forwarder(SerialIO in, SerialIO out, PrintStream log) {
00081 in.addSerialIOListener(this);
00082 this.in = in;
00083 this.out = out;
00084 this.log = log;
00085 }
00086
00087 public void serialIOEvent(SerialIOEvent event) {
00088 try {
00089 if (log != null) {
00090 log.println(dateFormat.format(new Date()) + "\t" + in.getPortName() + "\t" + event.getLogMessage());
00091 }
00092 out.writeMessage(event.getMessage());
00093 } catch (SerialIOException ex) {
00094 ex.printStackTrace();
00095 }
00096 }
00097 }
00098 }