File source
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
/** read and decode serial data */
public class SerialReader implements SerialPortEventListener {
InputStream in;
SorterGUI th;
private byte[] buffer = new byte[1024];
public SerialReader ( InputStream in, SorterGUI th ) {
this.in = in;
this.th = th;
}
@Override
public void serialEvent(SerialPortEvent arg0) {
int data;
try{
int len = 0;
while ( ( data = in.read()) > -1 ){
if ( data == '\n' ) {
break;
}
buffer[len++] = (byte) data;
}
String sin = new String(buffer,0,len);
//System.out.println("in "+sin);
Scanner ins = new Scanner(sin);
if(ins.hasNextLine()){
String s = ins.nextLine();
System.out.println(s);
if(s.equals("STOP")){
th.motorStop();
}
}
}catch ( IOException e ){
e.printStackTrace();
System.exit(-1);
}
}
}