1
마이크로 컨트롤러에 데이터를 보내려면 java 라이브러리 rxtxx를 사용합니다. 응용 프로그램이 작동하지만 경고가 나타납니다. rxtxx2.1.7 버전 불일치 보낸 데이터에 영향을 줍니까 ?? 나는 그것을 테스트했지만 데이터가 내 코드를 전송하지 않았다입니다 :java를 사용하여 usb- 직렬 케이블을 통해 연결
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.");
}
}
}
정말 고맙습니다. 시도해 보겠습니다. –
이 패키지를 사용해 보았습니다 : http://koros.ece.utexas.edu/wiki/images/7/7e/Ch-rxtx-2.2-20081207-win- x64.zip 작동하지 않을지 모르지만 경고가 나타나지 않습니다. 감사합니다. –