I am using a java thread while serial communication with the device. It works fine absolutely perfect in debug mode but when I run the program then the thread does not execute instructions.Am using the RXTX library for Serial Communication and the rest of my program is in JavaFX (Application).
This is the class am using for Serial Communication (SerialClass.java) :
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
public class SerialClass implements SerialPortEventListener {
public SerialPort serialPort;
/**
* The port we're normally going to use.
*/
private static final String PORT_NAMES[] = {
"/dev/tty.usbserial-A9007UX1", // Mac OS
"/dev/ttyACM0", // Linux
"COM21", // Windows
};
public static BufferedReader input;
public static OutputStream output;
/**
* Milliseconds to block while waiting for port open
*/
public static final int TIME_OUT = 2000;
/**
* Default bits per second for COM port.
*/
public static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//First, Find an instance of serial port as set in PORT_NAMES.
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
char ch = 1;
output.write(ch);
// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine = input.readLine();
System.out.println(inputLine);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
public static synchronized void writeData(String data) {
System.out.println("Sent: " + data);
try {
output.write(data.getBytes());
} catch (Exception e) {
System.out.println("could not write to port");
}
}
}
Am using an another class ImageInterface, which uses this class :
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.sanselan.ImageReadException;
import finImage.FinImage;
import img.ImageAccessor;
import img.ImageBean;
public class ImageInterface {
String image;
public static BufferedReader input;
public static OutputStream output;
public static synchronized void writeData(String data) {
System.out.println("Sent: " + data);
try {
output.write(data.getBytes());
} catch (Exception e) {
System.out.println("could not write to port");
}
}
public void openWithImage(File ImgFile) {
try {
// TODO code application logic here
ImageAccessor sa = new ImageAccessor();
ImageBean s = new ImageBean();
String ImgId = FinImage.getImageFromFin(ImgFile);
System.out.print(ImgId);
String sm = ImgId.toString();
System.out.print(sm);
String imageid = FinImage.getImageFromFin(ImgFile);
String sme;
Boolean flag = false;
if (imageid.startsWith("'")) {
sme = imageid.substring(1, imageid.length() - 1);
} else {
sme = imageid;
}
s = sa.getAllImageInfoById(sme);
System.out.print(s);
image = s.getFinPin();
//System.out.print(FinPin);
//System.out.print(FinPin);S
// image = ImgPin.toString();
System.out.print(image);
try {
SerialClass obj = new SerialClass();
int c = 0;
obj.initialize();
input = SerialClass.input;
output = SerialClass.output;
InputStreamReader Ir = new InputStreamReader(System.in);
BufferedReader Br = new BufferedReader(Ir);
Thread t = new Thread() {
public void run() {
obj.writeData(image);
}
//catch (InterruptedException ie){}
};
t.start();
System.out.println("Started");
obj.close();
} catch (Exception e) {
}
} catch (ImageReadException ex) {
Logger.getLogger(ImageInterface.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ImageInterface.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
In the above code I am accessing an id in the Image and then writing it through serial communication. In the above program, the thread doesn't write when the program is run, but it does write to Serial Port when the program is debug. Am not able to find out the exact reason why such a problem is occurring.
Am using the Arduino for output. Also I have been working with 2 Operation Systems simultaneously, Ubuntu 12.04LTS and Windows 7. In Windows 7 the thread executes in debug while in Ubuntu 12.04 the thread does not start the run method when started.
Please point out where am making a mistake.