SerialReaderListener.java //Un interface
Código:
import javax.comm.*;
interface SerialReaderListener
{ void newData(byte[] data);
}
SerialReaderThread.java
Código:
import java.io.*;
import javax.comm.*;
public class SerialReaderThread extends Thread
{
private SerialReaderListener listener;
public SerialPort puerto;
//Constructor
public SerialReaderThread(SerialReaderListener listener, SerialPort puerto)
{ this.listener = listener;
this.puerto = puerto;
}
public void run()
{ DataInputStream p = null;
try
{ p = new DataInputStream(this.puerto.getInputStream());
int input;
byte[] b=new byte[20];
while((input=p.read(b))!=-1)
{ listener.newData(b);
b=new byte[20];
}
}
catch(IOException e)
{
}
finally
{ try
{ p.close();
System.out.println("Cierro");
}
catch(IOException e)
{}
}
}
}