package jp.gr.javacons.industry.seminar.tempmonitor; import javax.comm.*; import java.io.*; import java.util.*; import java.text.DecimalFormat; public class DarwinSimulator implements SerialPortEventListener { protected SerialPort port; protected BufferedReader reader; protected PrintWriter writer; // 本来なら設定ファイルなどに記述するべき情報 protected String portName = "COM2"; protected int baudrate = 9600; protected int databits = SerialPort.DATABITS_8; protected int stopbits = SerialPort.STOPBITS_1; protected int parity = SerialPort.PARITY_EVEN; protected int flowcontrol = SerialPort.FLOWCONTROL_NONE; protected int channelSize = 4; // チャネル数 protected String type = "TC,K,"; // 入力のタイプ ここではK型熱電対を使用 protected Random random; protected DecimalFormat formatter; public DarwinSimulator(){ CommPortIdentifier portID = getCommPortIdentifier(portName); port = open(portID); setSerialPortParameters(port); try { // SerialPortEvent を受け取るためのリスナの登録 port.addEventListener(this); } catch(TooManyListenersException ex){ ex.printStackTrace(); System.exit(1); } // Data Available イベントを受け取るようにする port.notifyOnDataAvailable(true); writer = getWriter(port); reader = getReader(port); random = new Random(); formatter = new DecimalFormat("00000E0"); } protected CommPortIdentifier getCommPortIdentifier(String portname){ CommPortIdentifier portID = null; try{ // CommPortIdentifier を取得 portID = CommPortIdentifier.getPortIdentifier(portname); }catch(NoSuchPortException ex){ System.out.println("DarwinDataCollector cant't find Comport: " + portname + "."); System.exit(1); } return portID; } protected SerialPort open(CommPortIdentifier portID){ SerialPort serialport = null; try{ // ポートのオープン serialport = (SerialPort)portID.open("DarwinSimulator", 5000); }catch(PortInUseException ex){ // タイムアウトを過ぎた場合 System.out.println("Other application is using " + portName + "."); System.exit(1); } return serialport; } public void close(){ writer.close(); try{ reader.close(); }catch(IOException ex){ ex.printStackTrace(); } port.close(); } protected void setSerialPortParameters(SerialPort port){ try { // 通信条件の設定 port.setSerialPortParams(baudrate, databits, stopbits, parity); port.setFlowControlMode(flowcontrol); } catch (UnsupportedCommOperationException ex){ ex.printStackTrace(); System.exit(1); } } protected PrintWriter getWriter(SerialPort port){ PrintWriter writer = null; try { // 出力用の Writer を生成 writer = new PrintWriter(new BufferedWriter( new OutputStreamWriter(port.getOutputStream()))); } catch (IOException ex){ ex.printStackTrace(); System.exit(1); } return writer; } protected BufferedReader getReader(SerialPort port){ BufferedReader reader = null; try { // 入力用の Reader を生成 reader = new BufferedReader(new InputStreamReader(port.getInputStream())); } catch (IOException ex){ ex.printStackTrace(); System.exit(1); } return reader; } // SerialPortEvent 処理ルーチン public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: // Data Available 以外のイベントは処理しない break; case SerialPortEvent.DATA_AVAILABLE: // Data Available の処理 String buffer = null; String sub = null; try { while (reader.ready()) { // データの読み込み buffer = reader.readLine(); sub = buffer.substring(0, 2); if(sub.equalsIgnoreCase("FM")){ sendData(); }else{ ack(); } } } catch (IOException e){} break; } } public void ack(){ writer.println("E0"); writer.flush(); } public void sendData(){ int i; writer.println("DATA000710"); writer.println("TIME000000"); for(i = 1 ; i <= 3 ; i++){ sendData(i, false); } sendData(i, true); } public void sendData(int ch, boolean last){ StringBuffer buffer = new StringBuffer(); buffer.append("N"); if(last){ buffer.append("E C "); }else{ buffer.append(" C "); } buffer.append("00"); buffer.append(String.valueOf(ch)); buffer.append(","); double value = random.nextDouble()*100.0; if(value > 0){ buffer.append("+"); } buffer.append(formatter.format(value)); System.out.println("V=" + value + " SendData : " + buffer.toString()); writer.println(buffer.toString()); writer.flush(); } public static void main(String[] args){ new DarwinSimulator(); } }