Java + Uno + RFID : 在 java 中调用读取 rfid 的方法
Java + Uno + RFID : call method read rfid in java
我有一个问题,我可以从其他 class 调用方法到我的 JFrame
这是我的方法class,我从这个论坛的其他人那里得到的
/*
* 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)" 中获取结果,当我尝试将其调用到另一个函数时,它没有显示出来。
我犯了什么错误?
有人可以帮我吗?
方法public void serialEvent(SerialPortEvent oEvent)
是在串口接收到东西时执行的(不打算自己调用),所以里面需要对接收到的东西进行处理,把它画出来(基于你的例子,注意里面的评论,你应该对收到的数据做些什么):
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());
}
}
}
我有一个问题,我可以从其他 class 调用方法到我的 JFrame
这是我的方法class,我从这个论坛的其他人那里得到的
/*
* 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)" 中获取结果,当我尝试将其调用到另一个函数时,它没有显示出来。
我犯了什么错误? 有人可以帮我吗?
方法public void serialEvent(SerialPortEvent oEvent)
是在串口接收到东西时执行的(不打算自己调用),所以里面需要对接收到的东西进行处理,把它画出来(基于你的例子,注意里面的评论,你应该对收到的数据做些什么):
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());
}
}
}