簡體   English   中英

我需要幫助使用ftdi usb到串行電纜寫入/讀取串行端口

[英]I need help to write/read to/from serial port using ftdi usb to serial cable

我想先寫入串口。 因為我正在使用USB連接ftdi電纜。 電纜連接到COM4。 運行Windows 7 64位

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

為了使用RXTX,我嘗試按照這些說明進行操作

  1. 下載rxtx-2.1-7-bins-r2.zip
  2. 解壓縮它
  3. 將rxtxSerial.dll復制到c:\\ program files \\ java \\ jre-version \\ bin目錄中
  4. 將RXTXcomm.jar復制到c:\\ program files \\ java \\ jre-version \\ lib \\ ext目錄中
  5. 將所有引用從'javax.comm'更改為'gnu.io'
  6. 重新編譯

這是我的代碼:

/*
 * 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

在此輸入圖像描述

您獲得的錯誤意味着無法在java.library.path找到dll ,因此您需要通過添加-Djava.library.path="C:\\path\\to\\your\\dll"系統屬性 java.library.path設置為與包含庫的文件夾對應的路徑-Djava.library.path="C:\\path\\to\\your\\dll"作為VM選項

有關如何在NetBeans中設置dll路徑的更多詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM