2009-06-08 9 views
2

Port série Ne se ferme pas. Je veux libérer le port COM ...Port série Ne se ferme pas. Je veux libérer le port COM

ci-dessous est mon code ....

import java.io.*; 
import java.util.*; 
import gnu.io.*; 

public class ReadCommPort implements SerialPortEventListener { 

    static CommPortIdentifier portId; 
    static Enumeration   portList; 
    InputStream     inputStream; 
    OutputStream    outputStream; 
    public SerialPort   serialPort; 
    List byteList    = new ArrayList(); 
    public static Message message = null; 

    public void readData() { 
     boolean portFound = false; 
     String defaultPort = "COM1"; 
     portList   = CommPortIdentifier.getPortIdentifiers(); 

     while (portList.hasMoreElements()) { 
      portId = (CommPortIdentifier)portList.nextElement(); 
      if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { 
       if (portId.getName().equals(defaultPort)) { 
        System.out.println("Found port: " + defaultPort); 
        portFound = true; 
        buildSerialPort(); 
       } 
      } 
     } 
     if (! portFound) { 
      System.out.println("port " + defaultPort + " not found."); 
     } 
    } 

    public void buildSerialPort() { 
     try { 
      serialPort = (SerialPort) portId.open("ReadCommPort", 1); 
      inputStream = serialPort.getInputStream(); 
      outputStream = serialPort.getOutputStream(); 
      serialPort.addEventListener(this); 
      serialPort.notifyOnDataAvailable(true); 
      serialPort.setSerialPortParams(2400, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, 

        SerialPort.PARITY_NONE); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @SuppressWarnings("unchecked") 
    public void serialEvent(SerialPortEvent event) { 

     switch (event.getEventType()) { 
     case SerialPortEvent.BI: 
      System.out.println("BI"); 
      break; 

     case SerialPortEvent.OE: 
      System.out.println("OE"); 
      break; 

     case SerialPortEvent.FE: 
      System.out.println("FE"); 
      break; 

     case SerialPortEvent.PE: 
      System.out.println("PE"); 
      break; 

     case SerialPortEvent.CD: 
      System.out.println("CD"); 
      break; 

     case SerialPortEvent.CTS: 
      System.out.println("CTS"); 
      break; 

     case SerialPortEvent.DSR: 
      System.out.println("DSR"); 
      break; 

     case SerialPortEvent.RI: 
      System.out.println("RI"); 
      break; 

     case SerialPortEvent.OUTPUT_BUFFER_EMPTY: 
      System.out.println("OUTPUT_BUFFER_EMPTY"); 
      break; 

     case SerialPortEvent.DATA_AVAILABLE : 
      try { 
       int len = inputStream.available(); 
       byte[] readBuffer = new byte[ len ]; 
       // processing data code.. 
       // close the port 
       // release all resources... 
       serialPort.removeEventListener(); 
       try 
       { 
        serialPort.addEventListener(null); 
       } 
       catch (TooManyListenersException e) 
       { 
        e.printStackTrace(); 
       } 
       inputStream.close(); 
       outputStream.close(); 
       serialPort.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      break; 
     } 
    } 

    public static void main(String[] args) { 
     new ReadCommPort().readData(); 
    } 
} 

Répondre

2

Si vous utilisez une version de RXTX-2.1, puis en appelant removeEventListener(); programme devrait se bloquer. Ceci est dû au code natif dans la bibliothèque. Cela peut être évité en exécutant un tel code, mais pour moi personnellement de cette façon n'aime vraiment pas:

new Thread(){ 
    @Override 
    public void run(){ 
     serialPort.removeEventListener(); 
     serialPort.close(); 
    } 
}.start(); 

Si vous utilisez essentiellement RXTX, puis utilisez la version 2.2. Aussi bien que des conseils pour essayer le composant jSSC - http://code.google.com/p/java-simple-serial-connector/

+0

J'avais un problème semblable avec RXTX-2.1-7 sur Mac OS 10.5.8 PPC. Dans mon cas, il n'y avait pas de crash, mais pas de sortie seulement. Faire apparaître un thread à fermer lorsque le gestionnaire d'événements libère le verrou sur le port est évident - après l'avoir vu! – Max

+0

@ scream3r, Ceci provoque un 'EXCEPTION_ACCESS_VIOLATION'. – mre