【发布时间】:2016-11-23 11:39:24
【问题描述】:
我想先写入串口。为此,我使用的是 USB 转串行 ftdi 电缆。电缆连接到 COM4。运行 Windows 7 64 位
a) 使用 RXTX 项目。 http://rxtx.qbang.org/wiki/index.php/Main_Page
使用 RXTX 我尝试按照这些说明进行操作
- 下载rxtx-2.1-7-bins-r2.zip
- 解压
- 将 rxtxSerial.dll 复制到 c:\program files\java\jre-version\bin 目录
- 将 RXTXcomm.jar 复制到 c:\program files\java\jre-version\lib\ext 目录
- 将所有引用从“javax.comm”更改为“gnu.io”
- 重新编译
这是我的代码:
/*
* 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,所以需要将系统属性java.library.path设置为包含该lib的文件夹对应的路径,检查this -
感谢 Nicolas,这很有效。如何将您的评论标记为答案?
-
好消息,不,您不能将评论标记为答案,您只能投票或标记评论。
-
把 rxtxSerial.dll 放到 C:\Windows\System32
标签: java serial-port