Java+;Uno+;RFID:java中读取RFID的调用方法
我有一个问题,我可以调用从其他类到我的JFrame的方法 这是我从这个论坛的其他人那里学到的方法课Java+;Uno+;RFID:java中读取RFID的调用方法,java,arduino,arduino-uno,rfid,Java,Arduino,Arduino Uno,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 tr
/*
* 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());
}
}
}
}
我想从源代码底部的函数“publicsvoidserialevent(SerialPortEvent-oEvent)”中得到结果,但当我试图从中调用另一个函数时,它没有显示出来
我犯了什么错误?
有人能帮我吗?当从串行端口接收到某个内容时(您不打算自己调用),就会执行方法
public void serialEvent(SerialPortEvent oEvent)
,因此在该方法中,您需要处理接收到的内容,并对其进行描述(根据您的示例,请注意其中的注释,您应该对收到的数据进行处理):
您调试过吗?可能是因为出现了异常,所以
串行事件从未出现过called@Beloo是的,我有,但结果是“构建成功”这不是调试,而是构建。调试是一个过程,当您将调试器连接到正在运行的应用程序并逐步执行程序操作以检查每一行的输入和输出结果时。请阅读和,然后根据这些标准改进您的问题。您可以帮助我,如何将结果传递给其他服务方法d如果它在方法中???我在这里有一个问题,这几天,它给我一个头痛添加一个对该服务的引用到侦听器中(最好在构造期间),然后只调用该服务的适当方法。请记住,您需要将该侦听器注册到适当的串行端口(先前构造)以便从中接收事件。请查看用于接收事件的库的文档。
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);
// DO SOMETHING WITH THE inputLine RECEIVED HERE
// EG. PASS IT TO SOME OTHER SERVICE METHOD
}
} catch (Exception e) {
System.err.println(e.toString());
}
}
}