2012-03-09 80 views
6

我正在将一个带有librxtx-java的设备连接到Ubuntu。该代码以前工作在10.04,但在12.04,它无法发现连接到计算机的USB串行。Ubuntu RXTX不识别usb串行设备

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

的这部分代码从不进入while循环中,尽管被安装适当librxtx-Java库,并且被识别的设备(dmesg的|尾示出了在一行USB“检测到串行设备转换器”)。

更新:

看来Ubuntu的12.04的64位不与任何USB串行设备的工作(尽管它们会显示在dmesg和显示为一个的/ dev/ttyUSB,这似乎是不仅仅是Java的一个问题

回答

13

我有Ubuntu 11.10内核3.0.0-12-generic-pae和librxtx-java version 2.2pre2-8已经安装了,下面的代码正确的列出了我的串口,现在你已经正确安装了串口转换器?您需要检查转换器使用的端口。使用下面的示例应用程序,您可以尝试类似java -cp /usr/share/java/RXTXcomm.jar:. GetCommPorts 2

请确保您在/ dev /中的ttySXX或ttyUSBXX文件中拥有正确的权限。

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 

这些串口显示在我的系统中,希望运行该应用程序的用户应该在组拨出。要添加自己,请使用:

sudo usermod -a -G dialout username 

您现在应该处于“拨出”组。从该应用程序

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(); 
     } 

    } 


} 

输出:

$ 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

什么。 GetCommPorts“/ dev/usb-serial-converter-port”呢? Eclipse中是否有相同的设置?在升级之前,它没有任何选项就能正常工作,我不知道这是否是非sun jdk的问题? – NoBugs 2012-03-09 15:54:20

+0

“/ dev/usb-serial-converter-port”是GetCommPorts的参数,用于检查串行通信端口是否存在。那么,我没有编译过非Sun的JDK,为什么不尝试它,检查输出?另外,你的通讯端口是什么? – Jasonw 2012-03-09 16:10:35

+0

它显示为/ dev/ttyUSB0,有没有办法测试我是否在系统级从设备接收到任何东西? – NoBugs 2012-03-09 18:49:03