使用java通过usb串行电缆进行连接
我正在使用java库rxtxx向微控制器发送数据 该应用程序似乎正常工作,但出现了一个警告:rxtxx2.1.7版本不匹配 它会影响发送的数据吗?? 我已对其进行了测试,但未发送任何数据我的代码是:使用java通过usb串行电缆进行连接,java,serial-port,rxtx,usbserial,Java,Serial Port,Rxtx,Usbserial,我正在使用java库rxtxx向微控制器发送数据 该应用程序似乎正常工作,但出现了一个警告:rxtxx2.1.7版本不匹配 它会影响发送的数据吗?? 我已对其进行了测试,但未发送任何数据我的代码是: import java.util.*; import gnu.io.*; import java.io.*; public class portwrit{ static Enumeration portList; static CommPortIdenti
import java.util.*;
import gnu.io.*;
import java.io.*;
public class portwrit{
static Enumeration portList;
static CommPortIdentifier portId;
static String msgstr="100";
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static Thread readThread;
static boolean outputBufferEmptyFlag = false;
public static void main(String[] args ) throws NoSuchPortException, PortInUseException {
boolean portFound = false;
String defaultPort = "COM6";
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port " + defaultPort);
portFound = true;
try {
serialPort =
(SerialPort) portId.open("SimpleWrite", 2000);
} catch (PortInUseException e) {
System.out.println("Port in use.");
continue;
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}
System.out.println(
"Writing "+msgstr+"\" to "+serialPort.getName());
try {
outputStream.write(Byte.parseByte(msgstr));
} catch (IOException e) {}
try {
Thread.sleep(2000); // Be sure data is xferred before closing
} catch (Exception e) {}
serialPort.close();
System.exit(1);
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}
}
}
根据您的要求,您可能安装了RXTX软件的多个副本。它建议在路径/类路径上查找comm.jar或RXTXcomm.jar或类似文件。我已经尝试过这个软件包:我不知道它是否有效,但没有出现警告,谢谢