arduino Java+Uno+RFID:调用方法读取Java中的RFID
我有一个问题,我可以调用其他类的方法到我的JFrame中
这是我从这个论坛的其他人那里学到的方法课
/*
* 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 transaksi_satu;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
public class UnoConnect implements SerialPortEventListener{
SerialPort serialPort;
private static final String PORT_NAMES[] = {"COM3"};
private BufferedReader input;
private OutputStream output1;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;
public UnoConnect(){
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//First, Find an instance of serial port as set in PORT_NAMES.
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output1 = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
public void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
public void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine=null;
if (input.ready()) {
inputLine = input.readLine();
System.out.println(inputLine);
//Lbl_ID.setText(inputLine);
}
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
}
我想从源代码底部的函数“public void serialEvent(SerialPortEvent oEvent)”中得到结果,当我试图从中调用另一个函数时,它不会出现
我犯了什么错误? 有人能帮我吗
共 (0) 个答案