Eventos para leer de puerto serie
Publicado por zoidberg_pinzas (3 intervenciones) el 15/02/2011 23:06:16
Hola, encontré este código en el buscador, creo que es sobre como activar eventos para un puerto serie:
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
// if (portId.getName().equals("COM1")) {
if (portId.getName().equals("/dev/term/a"))
{
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead()
{
try
{
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try
{
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try
{
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.s
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
// if (portId.getName().equals("COM1")) {
if (portId.getName().equals("/dev/term/a"))
{
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead()
{
try
{
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try
{
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try
{
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.s
Valora esta pregunta


0