Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/04/2007, 12:01
Avatar de derkenuke
derkenuke
Colaborador
 
Fecha de Ingreso: octubre-2003
Ubicación: self.location.href
Mensajes: 2.665
Antigüedad: 20 años, 6 meses
Puntos: 45
coger identificadores de puertos -- getPortIdentifiers()

Hola. Tengo este código, con error en la línea señalada:
Código:
public void abrirPuertos()
{
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM1")) {
                    try {
                        serialPort = (SerialPort)portId.open("SimpleWriteApp", 2000);
                    } catch (PortInUseException e) {}
                    try {
                        outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
                    } catch (IOException e) {}
                    try {
                        serialPort.setSerialPortParams(4800,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                             SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    
                }
            }
        }
    }
pero me salta una excepción en la línea en negrita:
Cita:
Exception in thread "Thread-2" java.lang.NullPointerException
at Coche.SimpleWriteRead.enviar(SimpleWriteRead.java: 44)
at Coche.MacCoche_Casa.estadodos(MacCoche_Casa.java:1 56)
at Coche.MacCoche_Casa.run (MacCoche_Casa.java:66)
La variable portList me da null y me salta la excepcion. Si los puertos estan abiertos, ¿no debería coger el identificador sólo? ¿Cómo podría inicializar esa variable de otra manera?

Utilizo el "Advanced Serial Port Terminal" para simular puertos un ordenador portatil.


Muchas Gracias!
__________________
- Haz preguntas inteligentes, y obtendrás más y mejores respuestas.
- Antes de postearlo Inténtalo y Búscalo.
- Escribe correctamente tus mensajes.