/* * filename: rs485.java * by Sonny Cruz * */ import javax.comm.*; import java.io.*; public class rs485 implements Constant { static InputStream in = null; static OutputStream out = null; static SerialPort sp = null; private static rs485 instance = new rs485(); public static rs485 getInstance() { return instance; } private rs485() { try { com.dalsemi.system.TINIOS.enableSerialPort1(); CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier("serial1"); sp = (SerialPort)portID.open("tini", 5000); sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); sp.setFlowControlMode(SerialPort. FLOWCONTROL_NONE); out = sp.getOutputStream(); in = sp.getInputStream(); } catch (IOException ioe) {} catch (NoSuchPortException nspe) {} catch (UnsupportedCommOperationException ucoe) {} catch (PortInUseException piue) {} } public int readData() { int data; data = 0; try { data = in.read(); } catch (IOException ioe) {} return data; } public void sendData(int b) { try { out.write((byte)b); } catch (IOException ioe) {} } public void requestSensorStatus() { try { out.write(SYNCH); // synch out.write(HEADER); // header out.write(DUMMY_BYTE); // address out.write(SENSOR_STATUS); // command out.write(DUMMY_BYTE); // data out.write(DUMMY_BYTE); // data out.write(DUMMY_BYTE); // data out.write(TRAILER); // trailer } catch (IOException ioe) {} } public void sendX10(int choice) { try { out.write(SYNCH); out.write(HEADER); // header out.write(DUMMY_BYTE); // address out.write(X_10); // command out.write(HOUSE_CODE); // data field switch (choice) { case 2 : out.write(A2_ON); break; case 20 : out.write(A2_OFF); break; case 3 : out.write(A3_ON); break; case 30 : out.write(A3_OFF); break; case 4 : out.write(A4_ON); break; case 40 : out.write(A4_OFF); break; case 5 : out.write(A5_ON); break; case 50 : out.write(A5_OFF); break; case 6 : out.write(A6_ON); break; case 60 : out.write(A6_OFF); break; case 7 : out.write(A7_ON); break; case 70 : out.write(A7_OFF); break; default: break; } out.write(DUMMY_BYTE); // data field out.write(TRAILER); } catch (IOException ioe) {} } }