2012-03-09 14 views
6

Sto collegando un dispositivo con librxtx-java a Ubuntu. Il codice precedentemente funzionava in 10.04, ma in 12.04 non è possibile scoprire la seriale USB collegata al computer.Ubuntu RXTX non riconosce il dispositivo seriale USB

java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers(); 
while (portEnum.hasMoreElements()) 
{ 
    CommPortIdentifier portIdentifier = portEnum.nextElement(); 
    System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType())); 
} 

Questa sezione del codice non va nel ciclo while, nonostante le librerie librxtx-java appropriati installati e dispositivo essendo riconosciuti (dmesg | tail mostra USB "convertitore dispositivo di serie rilevato" in una riga).

Aggiornamento:

Risulta Ubuntu 12.04 a 64 bit non funziona con tutti i dispositivi usb-seriale (anche se visualizzati in dmesg e appaiono come/dev/ttyUSB, sembra essere più di un semplice problema con Java

risposta

13

Ho il kernel 01.3.0.0-12-generic-pae e librxtx-java version 2.2pre2-8 installato Con il codice seguente, elenca correttamente le porte seriali Ora hai un convertitore da USB a seriale installato correttamente ? È necessario verificare che porta utilizza il convertitore. Utilizzando l'app di esempio di seguito, è possibile provare qualcosa come java -cp /usr/share/java/RXTXcomm.jar:. GetCommPorts 2

Assicurarsi di disporre dell'autorizzazione corretta nel file ttySXX o ttyUSBXX, in/dev /.

crw-rw---- 1 root dialout 4, 65 2012-02-29 01:08 /dev/ttyS1 
crw-rw---- 1 root dialout 4, 66 2012-02-29 01:08 /dev/ttyS2 

Queste porte seriali sono mostrati nel mio sistema e l'utente che desidera eseguire l'applicazione dovrebbe essere nel gruppo dialout. Per aggiungerti, usa:

sudo usermod -a -G dialout username 

Ora dovresti essere nel gruppo "dialout".

import gnu.io.CommPortIdentifier; 
import gnu.io.NoSuchPortException; 
import gnu.io.PortInUseException; 
import gnu.io.SerialPort; 
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import gnu.io.UnsupportedCommOperationException; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Enumeration; 
import java.util.TooManyListenersException; 

public class GetCommPorts 
{ 
    static Enumeration<CommPortIdentifier>   portList; 
    static CommPortIdentifier portId; 
    static SerialPort     serialPort; 
    static OutputStream   outputStream; 
    static boolean     outputBufferEmptyFlag = false;  


    public static class SerialReader implements SerialPortEventListener 
    { 
     private InputStream in; 
     private byte[] buffer = new byte[1024]; 

     public SerialReader(InputStream in) 
     {   
      this.in = in;   
     } 

     @Override 
     /** 
     * treat \n as end of block. 
     */ 
     public void serialEvent(SerialPortEvent ev) 
     { 
      int data; 

      try 
      { 
       int len = 0; 
       while ((data = in.read()) > -1) 
       { 
        if (data == '\n') 
        { 
         break; 
        } 
        buffer[len++] = (byte) data; 
       } 
       System.out.println(new String(buffer, 0, len)); 
      } 
      catch (IOException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       System.exit(-1); 
      }   
     }  
    } 

    public static class SerialWriter implements Runnable 
    { 
     OutputStream out; 

     public SerialWriter(OutputStream out) 
     { 
      this.out = out; 
     } 

     @Override 
     public void run() 
     {   
      try 
      { 
       int c = 0; 
       while ((c = System.in.read()) > -1) 
       { 
        this.out.write(c); 
       } 
      } 
      catch (IOException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       System.exit(-1); 
      } 

     } 

    } 

    private static String getPortTypeName (int portType) 
    { 
     switch (portType) 
     { 
     case CommPortIdentifier.PORT_I2C: 
      return "I2C"; 
     case CommPortIdentifier.PORT_PARALLEL: 
      return "Parallel"; 
     case CommPortIdentifier.PORT_RAW: 
      return "Raw"; 
     case CommPortIdentifier.PORT_RS485: 
      return "RS485"; 
     case CommPortIdentifier.PORT_SERIAL: 
      return "Serial"; 
     default: 
      return "unknown type"; 
     } 
    } 

    private static void listPorts() 
    { 
     @SuppressWarnings("unchecked") 
     java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers(); 

     while (portEnum.hasMoreElements()) 
     { 
      CommPortIdentifier portIdentifier = portEnum.nextElement(); 
      System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()));   

      if (portIdentifier.getPortType() == 1) 
      { 
       try 
       { 
        serialPort = (SerialPort) portIdentifier.open(portIdentifier.getName(), 3000); 
       } 
       catch (PortInUseException e) 
       { 
        System.err.print("port in use"); 
        continue; 
       } 

       System.out.println("Baud is " + serialPort.getBaudRate());  
       System.out.println("Bits is " + serialPort.getDataBits());  
       System.out.println("Stop is " + serialPort.getStopBits());  
       System.out.println("Par is " + serialPort.getParity()); 
       serialPort.close(); 
      } 
     } 
    } 

    private static int doReadWrite(String portName) 
    { 
     CommPortIdentifier portIdentifier; 

     try 
     { 
      portIdentifier = CommPortIdentifier.getPortIdentifier(portName); 

      if (portIdentifier.isCurrentlyOwned()) 
      { 
       System.err.println("error: port is currently in use"); 
       return -1; 
      } 

      SerialPort sport = (SerialPort) portIdentifier.open(portName, 3000); 
      sport.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 

      InputStream in = sport.getInputStream(); 
      OutputStream out = sport.getOutputStream();   

      (new Thread(new SerialWriter(out))).start(); 

      sport.addEventListener(new SerialReader(in)); 
      sport.notifyOnDataAvailable(true); 
     } 
     catch (NoSuchPortException e) 
     { 
      e.printStackTrace(); 
      return -1; 
     } 
     catch (PortInUseException e) 
     { 
      e.printStackTrace(); 
      return -1; 
     } 
     catch (UnsupportedCommOperationException e) 
     { 
      e.printStackTrace(); 
      return -1; 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
      return -1; 
     } 
     catch (TooManyListenersException e) 
     { 
      e.printStackTrace(); 
      return -1; 
     } 

     return 0;  
    } 

    static void showHelp() 
    { 
     System.out.println("Usage " + GetCommPorts.class.getName() + "N"); 
     System.out.println("1 read and write from the serial port"); 
     System.out.println("2 list all serial ports in the system"); 
     System.out.println("default show this help "); 
    } 


    public static void main(String[] args) 
    { 
     int operation = 0; 

     try 
     { 
      if (args.length != 1) 
      { 
       showHelp(); 
       return; 
      } 
      operation = Integer.parseInt(args[0]); 
     } 
     catch (NumberFormatException e) 
     { 

     }  

     switch (operation) 
     { 
     case 1: 
      doReadWrite("/dev/ttyUSB0"); 
      break; 
     case 2: 
      listPorts(); 
      break; 
     default: 
      showHelp(); 
     } 

    } 


} 

uscita da questa applicazione:

$ java -cp /usr/share/java/RXTXcomm.jar:. GetCommPorts 2 
/dev/ttyS1 - Serial 
Baud is 9600 
Bits is 8 
Stop is 1 
Par is 0 
/dev/ttyS0 - Serial 
Baud is 9600 
Bits is 8 
Stop is 1 
Par is 0 
+0

Cosa fa:. GetCommPorts "/ dev/usb-serial-converter-port" fare? Esiste un'impostazione equivalente in Eclipse? Prima dell'aggiornamento aveva funzionato bene senza quella opzione, mi chiedo se è un problema con il jdk non-sun? – NoBugs

+0

"/ dev/usb-serial-converter-port" è il parametro per GetCommPorts per verificare se la porta di comunicazione seriale è presente. Bene, non ho compilato jdk non-sun e perché non lo provi, esamina l'output? Inoltre, qual è il tuo porto di comunicazione? – Jasonw

+0

Si presenta come/dev/ttyUSB0, c'è un modo per verificare se sto ricevendo qualcosa dal dispositivo a livello di sistema? – NoBugs