2011-07-20 211 views
1

喜读书,我有财产以后这样RXTX如何从COM端口

package compot; 

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


public class core { 

    private static SerialPort p; 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
     Enumeration ports = CommPortIdentifier.getPortIdentifiers(); 
     System.out.println("start"); 
     while(ports.hasMoreElements()) 
     { 
      CommPortIdentifier port = (CommPortIdentifier) ports.nextElement(); 
      System.out.print(port.getName() + " -> " + port.getCurrentOwner() + " -> "); 
      switch(port.getPortType()) 
      { 
       case CommPortIdentifier.PORT_PARALLEL: 
        System.out.println("parell"); 
       break; 
       case CommPortIdentifier.PORT_SERIAL: 
        //System.out.println("serial"); 
       try { 
        p = (SerialPort) port.open("core", 1000); 
        int baudRate = 57600; // 57600bps 
        p.setSerialPortParams(
          baudRate, 
          SerialPort.DATABITS_8, 
          SerialPort.STOPBITS_1, 
          SerialPort.PARITY_NONE); 
       } catch (PortInUseException e) { 
        System.out.println(e.getMessage()); 
       } catch (UnsupportedCommOperationException e) { 
        System.out.println(e.getMessage()); 
       } 
       break; 
      } 
     } 
     System.out.println("stop"); 
    } 
} 

但我不知道如何从端口读取?我已阅读this tutorial,但我不知道他们是什么“演示应用程序”?

编辑

OutputStream outStream = p.getOutputStream(); 
        InputStream inStream = p.getInputStream(); 

        BufferedReader in = new BufferedReader(new InputStreamReader(inStream)); 
        String inputLine; 

        while ((inputLine = in.readLine()) != null) 
         System.out.println(inputLine); 
        in.close(); 

我添加此代码,但我recive

稳定图书馆 =================== ====================== Native lib版本= RXTX-2.1-7 Java lib版本= RXTX-2.1-7开始/ dev/ttyUSB3 - > null - >底层输入流返回零字节停止

+0

任何后续行动?根本没有反应? :p – chzbrgla

+0

我编辑我的问题 –

+0

这可能是流量控制的问题。你可以用port.setFlowControl()方法在端口上设置它。但是,我不可能知道应该使用哪种流量控制。也许开始阅读有关串行端口的一般信息:http://tldp.org/HOWTO/Serial-HOWTO-4.html我建议阅读输入流字节明智不行。但这取决于您实际尝试与之通信的设备。 – chzbrgla

回答

0

像这样的事情在你的try块:

OutputStream outStream = p.getOutputStream(); 
InputStream inStream = p.getInputStream(); 
+0

那么什么是'p =(SerialPort)port.open(“核心”,1000);'这对于这第一部分“核心”需要? –

+0

串行端口连接被命名为“核心”,并在1000毫秒后超时,当前所有者在该时间内没有释放连接..您可能应该没有进程使用此端口 – chzbrgla

+0

此名称将标识端口的所有者。只需使用您的应用程序名称更多信息:[link](http://download.oracle.com/docs/cd/E17802_01/products/products/javacomm/reference/api/javax/comm/CommPortIdentifier.html) – Patrik

6

这是你的代码?你究竟想在那里做什么? :P

为了从的SerialPort阅读,你需要声明此端口:

CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/tty/USB0"); //on unix based system 

然后打开这个端口上的连接:

SerialPort serialPort = (SerialPort) portIdentifier.open("NameOfConnection-whatever", 0); 

下一步将会设置这个端口的参数(如果需要):

serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 

这是我的配置 - 你的可能会有所不同:)

现在您已准备好读取该端口上的一些数据! 获取数据,你需要从获得serialPorts InputStream和阅读:

InputStream inputStream = serialPort.getInputStream(); 
while (active) { 
     try { 
      byte[] buffer = new byte[22]; 
      while ((buffer[0] = (byte) inputStream.read()) != 'R') { 
      } 
      int i = 1; 
      while (i < 22) { 
       if (!active) { 
        break; 
       } 
       buffer[i++] = (byte) inputStream.read(); 
      } 
      //do with the buffer whatever you want! 
     } catch (IOException ex) { 
      logger.error(ex.getMessage(), ex); 
     } 
} 

什么我其实这里做的是从InputStream使用它的方法read()阅读。这将阻塞,直到数据可用或返回-1,如果到达尾流。在这个例子中,我一直等到我得到一个'R'字符,然后将下一个22字节读入缓冲区。这就是你读取数据的方式。

  1. 获取serialPorts的InputStream
  2. 使用.read()方法
  3. 有全部取消当循环和退出循环内(在我的情况下,active可以通过其他方法设置为false,因此结束读取过程。

希望这有助于

2

尝试使用

if (socketReader.ready()) { 
} 

,以便套接字仅在缓冲流 中有某些内容需要读取时作出响应,以避免发生异常。