package jp.gr.javacons.industry.seminar.tempmonitor; import javax.comm.*; import java.io.*; import java.util.*; import java.text.NumberFormat; public class DarwinDataCollector implements DataCollector { protected CommPortIdentifier portID; protected SerialPort port; protected BufferedReader reader; protected PrintWriter writer; // 本来なら設定ファイルなどに記述するべき情報 protected String portName = "COM1"; 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 int min = -200; // 最小値 protected int max = 1370; // 最大値 // 数値整形用フォーマッタ protected NumberFormat formatter; public DarwinDataCollector(){ // PortIdentifier の生成 portID = getCommPortIdentifier(portName); } protected CommPortIdentifier getCommPortIdentifier(String portname){ CommPortIdentifier portID = null; try{ // CommPortIdentifier を取得 portID = CommPortIdentifier.getPortIdentifier(portname); }catch(NoSuchPortException ex){ System.out.println(portname + " can't be found."); System.exit(1); } return portID; } public void open(){ try{ // ポートのオープン port = (SerialPort)portID.open("DarwinDataCollector", 5000); }catch(PortInUseException ex){ // タイムアウトを過ぎた場合 System.out.println("Other application is using " + portName + "."); System.exit(1); } // 通信条件の設定 setSerialPortParameters(port); // Reader/Writer の抽出 writer = getWriter(port); reader = getReader(port); // DARWIN の初期化 darwinInitialize(); // DARWIN のレンジ設定 for(int i = 0 ; i < channelSize ; i++){ setRange(i+1, min, max); } } 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; } protected void initFormatter(){ // 数値出力の整形用クラス formatter = NumberFormat.getInstance(); // 出力桁数を 3桁にする formatter.setMinimumIntegerDigits(3); } // DARWIN の初期化 protected void darwinInitialize(){ // Darwin Reset sendCommand("RS0"); // Darwin Initialize sendCommand("RC0"); } // DARWIN のレンジ設定 protected void setRange(int ch, int minimum, int maximum){ initFormatter(); // レンジの設定には SR コマンドを使用 // 書式 SRp1,p2,p3,p4,p5 // p1: チャネル番号 p2: 入力の種類 p3: 測定のタイプ // p4: 最小値 p5: 最大値 StringBuffer command = new StringBuffer(); command.append("SR"); command.append(formatter.format(ch)); command.append(","); command.append(type); command.append(String.valueOf(minimum)); command.append(","); command.append(String.valueOf(maximum)); sendCommand(command.toString()); } public int getChannelSize(){ return channelSize; } public int getMinimumValue(){ return min; } public int getMaximumValue(){ return max; } public Vector getData(){ // TS コマンドを使用して、取得するデータの選択を行う // 書式 TSp1 // p1: 0 測定データ // 1 設定データ sendCommand("TS0"); // データ出力前にはトリガを送る必要がある // トリガ書式 ESC T sendCommand("\u001bT"); // データの取得 return requestData(); } // コマンドの送信 protected String sendCommand(String command) { writer.println(command); writer.flush(); // コマンドには必ず戻り値がある String result = null; try{ result = reader.readLine(); }catch(IOException ex){ ex.printStackTrace(); } return result; } protected Vector requestData(){ // データの取得 // 書式 FMp1,p2,p3 // p1: 測定データフォーマット指定 0=ASCII, 1=バイナリ // p2: 出力先頭チャネル p3: 出力終了チャネル writer.println("FM0,001," + formatter.format(channelSize)); writer.flush(); String buffer; StringTokenizer tokenizer; String value; Vector results = new Vector(); // データの受信 // 書式 1行目 DATEYYMMDD YY: 年 MM: 月 DD: 日 // 2行目 TIMEhhmmss hh: 時間 mm: 分 ss: 秒 // 3行目 S1S2A1A1A2A2A3A3A4A4UUUUUCCC,±DDDDDE-e // S1: データステータス N: ノーマル E: 異常 // S2: データ順序 スペース: 中間データ E: 最終データ // A1,A2,A3,A4: アラームステータス // U: 単位 // C: チャネル番号 // ±: データの正負 // DDDDD: データの仮数部 // E-e: データの指数部 (eに数値が入る) // 以下、チャネル数分、3行目と同じフォーマットでデータを受信できる try{ buffer = reader.readLine(); // 時間情報は使用しない buffer = reader.readLine(); }catch(IOException ex){ ex.printStackTrace(); System.exit(1); } for(int i = 0 ; i < channelSize ; i++){ try{ buffer = reader.readLine(); tokenizer = new StringTokenizer(buffer, ","); tokenizer.nextToken(); value = tokenizer.nextToken(); // データ部分の切りだし results.add(new Double(value)); }catch(IOException ex){ ex.printStackTrace(); System.exit(1); } } return results; } }