/* * filename: Wireless.java * by Sonny Cruz * */ import javax.comm.*; import java.io.*; public class Wireless extends Thread{ static final int AVAILABLE_CYBIKO = 3; static InputStream in = null; static OutputStream out = null; static SerialPort sp = null; static int packetCount = 0; static int count; static int counter = 0; static byte[] receivedBuffer = new byte[8]; static byte[] sendBuffer = new byte[8]; boolean flop = true; private static Wireless instance = new Wireless(); public static Wireless getInstance() { return instance; } private Wireless() { try { CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier("COM2"); sp = (SerialPort)portID.open("Wireless", 1000); sp.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); sp.setFlowControlMode(SerialPort. FLOWCONTROL_NONE); sp.enableReceiveTimeout(500); out = sp.getOutputStream(); in = sp.getInputStream(); //initialize sendBuffer sendBuffer[0] = 0x30; sendBuffer[1] = 0x31; sendBuffer[2] = 0x32; sendBuffer[3] = 0x33; sendBuffer[4] = 0x34; sendBuffer[5] = 0x35; sendBuffer[6] = 0x36; sendBuffer[7] = 0x38; } catch (IOException ioe) {} catch (NoSuchPortException nspe) {} catch (UnsupportedCommOperationException ucoe) {} catch (PortInUseException piue) {} } public byte[] getBuffer() { return receivedBuffer; } public int getPacketCount() { return packetCount; } public void setFifthByte(byte b) { sendBuffer[4] = b; } public void resetFifthByte() { sendBuffer[4] = 0x35; } public void run() { while (true) { if (counter++ >= AVAILABLE_CYBIKO) // increment cybiko address counter = 0; switch(counter) { // assign cybiko address case 0 : sendBuffer[1] = 0x31; break; case 1 : sendBuffer[1] = 0x32; break; case 2 : sendBuffer[1] = 0x33; break; } try { out.write(0x1b); // synch out.write(sendBuffer, 0, sendBuffer.length); try { Thread.sleep(50); } catch (InterruptedException e) {} count = in.read(receivedBuffer, 0, receivedBuffer.length); if (count == 8) packetCount++; try { Thread.sleep(250); } catch (InterruptedException e) {} } catch (IOException e) {} } } }