2016-11-23 71 views
1

我想先寫入串口。爲此,我使用USB串行ftdi電纜。電纜連接到COM4。運行Windows 7 64位我需要幫助寫/讀/從串口使用ftdi usb串口電纜

a)使用RXTX項目。 http://rxtx.qbang.org/wiki/index.php/Main_Page

要通過以下說明

  1. 下載rxtx-2.1-7-bins-r2.zip利用RXTX 我試圖做
  2. 將它解壓縮
  3. 複製rxtxSerial.dll成c:\ program files \ java \ jre-version \ bin目錄
  4. 將RXTXcomm.jar複製到c:\ program files \ java \ jre-version \ lib \ ext目錄
  5. change all re從 'javax.comm' 到 'gnu.io'
  6. 重新編譯ferences

這裏是我的代碼:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package twowayserialcomm; 

/** 
* 
* @author HP 
*/ 

import java.io.InputStream; 
import java.io.OutputStream; 
import gnu.io.SerialPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.CommPort; 

public class TwoWaySerialComm { 

    /** 
    * @param args the command line arguments 
    * 
    */ 
    void connect(String portName) throws Exception { 
     CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); 

     if(portIdentifier.isCurrentlyOwned()) { 
      System.out.println("Error: Port is currently in use"); 
     } else { 
      int timeout = 10000; 
      CommPort commPort = portIdentifier.open(this.getClass().getName(), timeout); 

      if(commPort instanceof SerialPort) { 
       SerialPort serialPort = (SerialPort)commPort; 
       serialPort.setSerialPortParams(9600, 
            SerialPort.DATABITS_8, 
            SerialPort.STOPBITS_1, 
            SerialPort.PARITY_NONE); 

       //InputStream in = serialPort.getInputStream(); 
       OutputStream outputStream = serialPort.getOutputStream(); 
       outputStream.write(53); 
       //outputStream.write(1); 
       //outputStream.write(20); 
       //outputStream.write(0); 
       //outputStream.write(83); 

       //CommPort port = serialPort; 
       System.out.println("Write done"); 
       //(new Thread(new SerialReader(in,port ))).start(); 
      } else { 
       System.out.println("Error: Only serial ports are handled by this example."); 
      } 
     } 
    } 
    public static void main(String[] args) { 
     try { 
      TwoWaySerialComm alex = new TwoWaySerialComm(); 
      //(new TwoWaySerialComm()).connect("COM4"); 
      alex.connect("COM4"); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

當運行:

run: 
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver 
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path 
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) 
    at java.lang.Runtime.loadLibrary0(Runtime.java:870) 
    at java.lang.System.loadLibrary(System.java:1122) 
    at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83) 
    at twowayserialcomm.TwoWaySerialComm.connect(TwoWaySerialComm.java:26) 
    at twowayserialcomm.TwoWaySerialComm.main(TwoWaySerialComm.java:61) 
C:\Users\HP\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 
BUILD FAILED (total time: 1 second) 

b)使用的javax.comm。庫 在做,我得到了以下錯誤回報

run: 
javax.comm.NoSuchPortException 
    at javax.comm.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:105) 
    at twowayserialcomm.TwoWaySerialComm.connect(TwoWaySerialComm.java:26) 
    at twowayserialcomm.TwoWaySerialComm.main(TwoWaySerialComm.java:61) 

,這裏是從NetBeans中的項目窗口

串行端口的Java

enter image description here

+0

,你得到的錯誤意味着該DLL不能在'java.library.path'找到,所以你需要將系統屬性'java.library.path'設置爲與包含lib的文件夾對應的路徑,選中[this](http://stackoverflow.com/questions/10714785/giving-java-library -path-in-netbeans-for-dll-so-files) –

+0

謝謝尼古拉斯,工作。如何將您的評論標記爲答案? – Alexandros81

+0

好消息,不,你不能標記評論作爲答案,你只能投票或標記評論。 –

回答

0

您獲得意味着錯誤的dll可能找不到java.library.path,所以你需要設置系統屬性java.library.path t Ø對應於包含庫的文件夾,路徑加入-Djava.library.path="C:\path\to\your\dll"爲VM選項

更多細節約how to set the path to a dll in NetBeans