[英]I need help to write/read to/from serial port using ftdi usb to serial cable
I want to first write to serial port. 我想先写入串口。 for that i am using a usb to serial ftdi cable.
因为我正在使用USB连接ftdi电缆。 The cable is connected to COM4.
电缆连接到COM4。 Running Windows 7 64-bit
运行Windows 7 64位
a) Using the RXTX project. a)使用RXTX项目。 http://rxtx.qbang.org/wiki/index.php/Main_Page
http://rxtx.qbang.org/wiki/index.php/Main_Page
To utilize the RXTX I tried doing that by following these instructions 为了使用RXTX,我尝试按照这些说明进行操作
Here is my code: 这是我的代码:
/*
* 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();
}
}
}
When running: 运行时:
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) by using the javax.comm. b)使用javax.comm。 libraries When doing that i got following erros
库当我这样做时,我得到了跟随错误
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)
and here is the project window from netbeans 这是netbeans的项目窗口
Serial Port Java 串口Java
The error that you get means that the dll
could not be found in java.library.path
, so you need to set the system property java.library.path
to the path corresponding to the folder that contains the library, by adding -Djava.library.path="C:\\path\\to\\your\\dll"
as VM Options 您获得的错误意味着无法在
java.library.path
找到dll
,因此您需要通过添加-Djava.library.path="C:\\path\\to\\your\\dll"
将系统属性 java.library.path
设置为与包含库的文件夹对应的路径-Djava.library.path="C:\\path\\to\\your\\dll"
作为VM选项
More details about how to set the path to a dll in NetBeans 有关如何在NetBeans中设置dll路径的更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.