ceritanya gini nih gan..
ane lagi bikin aplikasi yang dimana ngehubungin java sama arduino
setelah di RUN aplikasi ane bisa data yang diambil dari sensor arduino
setelah berjalan beberapa saat kadang keluar "IO ERROR: Underlying input stream returned zero bytes"
ane bingung ini sebabnya apa
ada yang bisa bantu ane ga?
ini code ane gan
Spoiler for code :
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import processing.app.Preferences;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author GIGIH
*/
public class ArduinoJava extends Thread {
public static String a,b;
public ArduinoJava(){
}
static InputStream input;
static OutputStream output;
static CommPortIdentifier portId;
static SerialPort port;
@Override
public void run(){
try {
ArduinoJava();
} catch (IOException ex) {
Logger.getLogger(ArduinoJava.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void ArduinoJava() throws IOException{
String inputan ="";
SendtoServer s = new SendtoServer();
String tempa ="";
String tempb ="";
double F,S,fuzzy,fuzzy_detik;
int fuzzy_sleep;
Preferences.init();
System.out.println("Using port: " + Preferences.get("serial.port"));
try {
portId = CommPortIdentifier.getPortIdentifier(Preferences.get("serial.port"));
//port = (SerialPort)portId.open("serial talk", 4000);
port = (SerialPort)portId.open("", 4500);
input = port.getInputStream();
output = port.getOutputStream();
port.setSerialPortParams(Preferences.getInteger("serial.debug_rate"),
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
while(true){
while(input.available() > 0) {
BufferedReader br = new BufferedReader(new InputStreamReader(input));
while (( inputan = br.readLine()) != null){
String a = inputan.split(",")[0];
//System.out.println(a);
String b = inputan.split(",")[1];
//System.out.println(b);
// if(tempa.equals(a) && tempb.equals(b) ){
// System.out.println("sama");
// tempa=a;
// tempb=b;
// }
// else {
System.out.println("outnya");
System.out.println(a);
System.out.println(b);
fuzzy = Double.parseDouble(a);
//s.SendtoServer(a, b);
if(fuzzy >= 0 && fuzzy <=5){
fuzzy_detik = 1;
//System.out.println("cepat");
}
else if(fuzzy >= 25 && fuzzy <=30){
fuzzy_detik = 5;
//System.out.println("lambat");
}
else{
F = (25 - fuzzy)/20;
S = (fuzzy - 5)/20;
if(F>S){
fuzzy_detik = 5 -(4*F);
}
else{
fuzzy_detik = (4*S) + 1;
}
System.out.println("fuzzy");
System.out.println(F);
System.out.println(S);
}
System.out.println("sleep");
System.out.println(fuzzy_detik);
tempa = a;
tempb = b;
fuzzy_sleep = (int) (fuzzy_detik*1000);
System.out.println(fuzzy_sleep);
Thread.sleep(fuzzy_sleep);
}
br.close();
//System.out.print((char)(input.read()));
//inputan = br.readLine();
//System.out.print(inputan);
}
}
}
catch(gnu.io.NoSuchPortException nsp) {
System.err.println("ERROR: " + nsp.getMessage());
}
catch(gnu.io.UnsupportedCommOperationException usp) {
System.err.println("ERROR: " + usp.getMessage());
}
catch(gnu.io.PortInUseException pie) {
System.err.println("ERROR: Port " + port + " is already in use\nCLose the port and restart.");
}
catch(java.io.IOException ioe) {
System.err.println("IO ERROR: " + ioe.getMessage() );
}
catch(Exception exe) {
System.err.println("ERROR: Unexpected error occured \n" + exe.getMessage() );
}
}
}