i am trying to call lightOn and LightOff method from NewSerialWriter's run() method but not able to make the correct logic. please help me with this. here is my code of different classes.
package newRelay_3088;
import gnu.io.CommPort; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import java.io.InputStream; import java.io.OutputStream; import java.util.*;
public class LightController {
public LightController() throws Exception { super(); CommPortIdentifier commPortIdentifier = CommPortIdentifier.getPortIdentifier("COM12"); if(commPortIdentifier.isCurrentlyOwned()) { System.out.println("Error: Port is currently in use"); } else { CommPort commPort = commPortIdentifier.open(this.getClass().getName(), 2000); if(commPort instanceof SerialPort) { SerialPort serialPort = (SerialPort) commPort; serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); InputStream in = serialPort.getInputStream(); OutputStream out = serialPort.getOutputStream(); (new Thread(new NewSerialReader(in))).start(); (new Thread(new NewSerialWriter(out))).start(); } else { System.out.println("Error: Only serial ports are handled by this example."); } } } public static void main(String[] args) { try { new LightController(); } catch (Exception e) { e.printStackTrace(); } Lights light_1 = new Lights("00000001"); Lights light_2 = new Lights("00000002"); Lights light_3 = new Lights("00000004"); Lights light_4 = new Lights("00000008"); Lights light_5 = new Lights("00000016"); Lights light_6 = new Lights("00000032"); Lights light_7 = new Lights("00000064"); Lights light_8 = new Lights("00000128"); HashMap<Integer, Lights> lightList = new HashMap<Integer, Lights>(); lightList.put(1, light_1); lightList.put(2, light_2); lightList.put(3, light_3); lightList.put(4, light_4); lightList.put(5, light_5); lightList.put(6, light_6); lightList.put(7, light_7); lightList.put(8, light_8); lightList.get(1).lightOn(); }
}
package newRelay_3088;
import java.io.IOException; import java.io.OutputStream;
public class Lights{ OutputStream out; String str;
public Lights(String str) { this.str = str; } public void lightOn() { // code for light on goes here System.out.println(str); try { String hexString = String.format("%02X", Integer.parseInt(str)); System.out.println("HexSting : " + hexString); System.out.println(hexString.length()); String command = "!012" + hexString + "\r"; System.out.println(command); System.out.println(command.getBytes()); out.write(command.getBytes()); System.out.println("-----------------------1-------------------------"); int c = 0; while((c = System.in.read()) > -1) { System.out.println("-----------------------2-------------------------"); this.out.write(c); } } catch ( IOException e ) { e.printStackTrace(); } } public void lightOff(int sequenceNo) { // code for light off goes here try { String hexString = String.format("%02X", Integer.parseInt(str)); System.out.println("HexSting : " + hexString); System.out.println(hexString.length()); String command = "!012" + hexString + "\r"; System.out.println(command); out.write(command.getBytes()); System.out.println("-----------------------1-------------------------"); int c = 0; while((c = System.in.read()) > -1) { System.out.println("-----------------------2-------------------------"); this.out.write(c); } } catch ( IOException e ) { e.printStackTrace(); } } public void getLightStatus() { // method for getting the current status of all the lights // code goes here }
}
package newRelay_3088;
import java.io.IOException; import java.io.InputStream;
public class NewSerialReader implements Runnable{
InputStream in; public NewSerialReader(InputStream in) { this.in = in; } public void run() { byte[] buffer = new byte[1024]; int length = -1; try { while((length =this.in.read(buffer)) > -1 ) { System.out.print(new String(buffer, 0, length)); } } catch ( IOException e ) { e.printStackTrace(); } }
}
package newRelay_3088;
import java.io.IOException; import java.io.OutputStream;
public class NewSerialWriter implements Runnable{ public OutputStream out;
public NewSerialWriter(OutputStream out) { this.out = out; } public void run() { }
} please help ,e with this. also tell me where else my logic is not right. i sm trying to send 8 different commands to a relay to switch on and off the lights and at the same time continuously checking the status... thanks a ton in advance.