package com.example.sycapp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.util.Vector; import android.content.Context; public class SerialIO{ private OutputStream sout; private InputStream sin; private Vector parsedData; // Serial Data private final short HEADER = SerialData.HEADER; private final byte HEADER_FST = (byte)(HEADER>>8&0xFF); private final byte HEADER_SCD = (byte)(HEADER&0xFF); private final int int16Cnt = SerialData.evt_int16Cnt; private final int int32Cnt = SerialData.evt_int32Cnt; // Serial Buffer private int int16Idx; private int int32Idx; private byte[] buffer = new byte[4]; private int int16_; private int int32_; private int parseStatus = 0; private short int16Buffer[] = new short[int16Cnt]; private int int32Buffer[] = new int [int32Cnt]; private int portId; // Constructors SerialIO(String portName, int portId){ try { this.sout = new BufferedOutputStream(new FileOutputStream(new File(portName))); this.sin = new FileInputStream(new File(portName)); this.parsedData = new Vector(); this.portId = portId; } catch (Exception e) { Util.debug("[S#0]"+e.toString()); } } SerialIO(FileOutputStream fo){ try { this.sout = new BufferedOutputStream(new FileOutputStream(new File("/dev/ttyO1"))); this.sin = new FileInputStream(new File("/dev/ttyO1")); this.parsedData = new Vector(); } catch (Exception e) { Util.debug("[S#0]"+e.toString()); } } // Write & Read Routines public void send(byte[] content){ try{ sout.write(content); sout.flush(); }catch(Exception e){ Util.debug("[S#5]"+e.toString()); } } public void send(String content){ try{ sout.write(content.getBytes()); sout.flush(); }catch(Exception e){ Util.debug("[S#4]"+e.toString()); } } public Vector checkPort(){ parsedData.clear(); try { if( sin.available()>0){ onDataReceived(); } } catch (Exception e) { Util.debug("[S#2]" +e.toString()); } return parsedData; } private void onDataReceived(){ //Util.debug("[Serial] OnDataReceived!"); SerialData sData = new SerialData(); try { byte buffer[] = new byte[sin.available()]; sin.read(buffer,0, sin.available()); parseByteFlow(buffer); } catch (Exception e){Util.debug("[S#1]"+e.toString());} } // TODO: need to be changed back public void parseByteFlow(byte[] byteFlow){ for (int i=0; i