/* Bese : Sun SimpleRead SimpleWrite For FSW(Kitchen of the Future "F"oot "Sw"itch) IO. H.Miyazawa */ import java.io.*; import java.util.*; import javax.comm.*; import java.net.*; //public class fsw { public class fswtcp implements Runnable, SerialPortEventListener{ static Enumeration portList; static CommPortIdentifier portId; static String messageString = "}I"; static SerialPort serialPort; static OutputStream outputStream; static InputStream inputStream; //Read Thread readThread; static int fsw_i, fsw_j; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM1")) { //if (portId.getName().equals("/dev/term/a")) { try { serialPort = (SerialPort) portId.open("SimpleWriteApp", 1000); } catch (PortInUseException e) {} try { outputStream = serialPort.getOutputStream(); } catch (IOException e) {} //Read try { inputStream = serialPort.getInputStream(); } catch (IOException e) {} /*try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {} */ serialPort.notifyOnDataAvailable(true); //Read try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {} fswtcp reader = new fswtcp(); /*try { outputStream.write(messageString.getBytes()); fsw reader = new fsw(); } catch (IOException e) {} */ } } } } public fswtcp() { try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {} readThread = new Thread(this); readThread.start(); System.out.println("ComPort Open."); try { outputStream.write(13); messageString = "}LCjava"; outputStream.write(messageString.getBytes()); } catch (IOException e) {} CamSlector(1); //return 1; /*try { outputStream.write(13); messageString = "}LCFSW READER java"; outputStream.write(messageString.getBytes()); } catch (IOException e) {} */ /*for( int i=1; i<5; i++ ) { System.out.println( "FOR:" + (i) ); try { outputStream.write(messageString.getBytes()); outputStream.write(i+49); System.out.println(messageString); } catch (IOException e) {} //Thread.sleep(200); // o sec. stop. }*/ } public void CamSlector(int camsel){ String camsels; camsels = ""; switch(camsel){ case 1: camsels = "}D013"; break; case 2: camsels = "}D014"; break; case 3: camsels = "}D007"; break; case 4: camsels = "}D008"; break; default: break; } System.out.println(camsel + camsels); try { outputStream.write(13); outputStream.write(camsels.getBytes()); } catch (IOException e) {} } public int fsw_now (){ messageString = "}I"; int fsw_now2; fsw_now2 = 0; for( int i=1; i<5; i++ ) { fsw_i = i; fsw_j = 1; //System.out.println( "FOR:" + (i) ); try { outputStream.write(13); outputStream.write(messageString.getBytes()); outputStream.write(i+48); //System.out.print(messageString); //System.out.println(i+48); } catch (IOException e) { return 0; } try{ Thread.sleep(50); // o sec. stop. } catch (InterruptedException e) {} //fsw_now2 = fsw_j<<(i-1); fsw_now2 = fsw_now2 + (fsw_j<<(i-1)); //System.out.println("fsw_j:" + fsw_j +"i"+i + fsw_now2); } //for (i) return fsw_now2; } //Read public void run() { try { Thread.sleep(500); // o sec. stop. } catch (InterruptedException e) {} //tcp try{ System.out.println("TCP Starting..."); ServerSocket ss = new ServerSocket(10000); System.out.println("Running......."); while (true) { // クライアントからの要求があった場合 // Socketオブジェクトを取得 Socket s = ss.accept(); // ソケットのストリーム読み込み BufferedReader br = new BufferedReader( new InputStreamReader(s.getInputStream())); // ブラウザ用の出力ストリームを取得 PrintWriter pw = new PrintWriter(s.getOutputStream(), true); // プロンプトに出力 String line1,line2; int str_BEGIN,line3,re_fsw; while(!(line1 = br.readLine()).equals("")){ //ブラウザーのヘッダ出力。 //System.out.println(line1); str_BEGIN = line1.indexOf("GET"); //System.out.println("*" + str_BEGIN); if(str_BEGIN == 0){ str_BEGIN = line1.indexOf("/"); try{ line2 = line1.substring(str_BEGIN+1,str_BEGIN+2); line3 = Integer.parseInt(line2); }catch (NumberFormatException ex){ //数字以外の時 line3 = -1; } System.out.println("SerialOut" + line3); if((line3 < 5)&&(line3 > 0)){ //System.out.println("SerialOut" + line3); CamSlector(line3); } } if(line1 == "\r\n"){break;} if(line1 == "\r"){break;} if(line1 == "\n"){break;} }//while re_fsw = fsw_now(); //System.out.println("fsw:" + re_fsw); pw.println("HTTP/1.1 200 OK"); pw.println("Server: FSW"); pw.println("Content-Type: text/plain; charset=none"); pw.println("Connection: close"); pw.print("\r\n"); for(int i=1; i<5; i++ ){ System.out.print("fsw"+i+":"); if(0 !=(re_fsw & (1<<(i-1)))){ //OFF System.out.print("1 "); pw.print("fsw" + i + "=1&"); }else{ //ON System.out.print("0 "); pw.print("fsw" + i + "=0&"); } } System.out.println(" "); /*pw.print("sfw1=0&"); pw.print("sfw2=0&"); pw.print("sfw3=0&"); pw.print("sfw4=0&");*/ pw.println("dummy=0&"); //pw.print("\r\n\r\n"); s.close(); }//while }catch (IOException e) { System.out.println("TCP Error"); }//try //tcp /* try { messageString = "}I"; while(true){ for( int i=1; i<5; i++ ) { fsw_i = i; //System.out.println( "FOR:" + (i) ); try { outputStream.write(13); outputStream.write(messageString.getBytes()); outputStream.write(i+48); //System.out.print(messageString); //System.out.println(i+48); } catch (IOException e) {} Thread.sleep(50); // o sec. stop. } //for (i) } //while } catch (InterruptedException e) {} */ } public void serialEvent(SerialPortEvent event) { //System.out.println("SerialEvent"); 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: break; case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer = new byte[20]; try { while (inputStream.available() > 0) { int numBytes = inputStream.read(readBuffer); } //System.out.print("*"); //System.out.print(readBuffer[0]); //System.out.print(new String(readBuffer)); //System.out.print("*"); // for( int i=0; i<19; i++ ) { //System.out.print(readBuffer[i]); if(readBuffer[i] == 49){ //1 fsw_j = 1; //System.out.println((fsw_i) + ":OFF"); } if(readBuffer[i] == 48){ //0 fsw_j = 0; //System.out.println((fsw_i) + ":ON"); //CamSlector(fsw_i); //fsw_out = "*"; //fsw_out[1] = 0; } if(readBuffer[i] == 69){ System.out.println("Error"); } readBuffer[i] = 0; //バッファーのクリア }//for(i) } catch (IOException e) {} break; } } }