I have this RS232 class that need to read by the main class.
Whenever i use this readData method. it throws nullPointerException message.
but if i implement it d RS232() main method, it works well with output interger value -1 or hex value 255.
why izzit like that?
/**
* @(#)RS232.java
*
* Serial Port Object
*/
import javax.comm.*;
import java.io.*;
import java.net.*;
public class RS232 {
static InputStream in = null;
static OutputStream out = null;
private static RS232 instance = new RS232();
public static RS232 getInstance() { return instance; }
private RS232() {
try {
System.out.println("Connecting to serial0 at 115200bps");
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("serial0");
SerialPort sp = (SerialPort)portId.open("DeviceController", 0);
sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
sp.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
sp.enableReceiveThreshold(1024);
sp.enableReceiveTimeout(1000);
// Get output and input stream for the serial port
InputStream in = sp.getInputStream();
OutputStream out = sp.getOutputStream();
/*
int data;
data = 0;
try {
data = in.read();
System.out.println("data");
}
catch (IOException ioe) { System.err.println("readData FAIL"); }
*/
}
catch (IOException ioe) {
System.err.println("Unable to open an InputStream " + "for the serial port!");
System.exit(1);
}
catch (NoSuchPortException nspe) {
System.err.println("No Such Port");
System.exit(2);
}
catch (UnsupportedCommOperationException ucoe) {
System.err.println("Unable to set the paramets of the serial port!");
System.exit(3);
}
catch (PortInUseException piue) {
System.err.println("The serial port is in use!");
System.exit(4);
}
public int readData() {
int data;
data = 0;
try {
data = in.read();
System.out.println("data");
}
catch (IOException ioe) { System.err.println("readData FAIL"); }
return data;
}
public void sendData(int b) {
try {
out.write((byte)b);
}
catch (IOException ioe) {}
}
}