简体   繁体   English

Java + Uno + RFID:在java中调用方法读取rfid

[英]Java + Uno + RFID : call method read rfid in java

i have a problem that i can call the method from other class into my JFrame我有一个问题,我可以将其他类的方法调用到我的 JFrame 中

this my method class which i got from other people in this forum这是我在这个论坛上从其他人那里得到的我的方法类

    /*
 * 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());
        }
    }

}
}

i want get the result from function "public void serialEvent(SerialPortEvent oEvent)" at the bottom of source code, when i try to call it into another from it doesn't show up.我想从源代码底部的函数“public void serialEvent(SerialPortEvent oEvent)”中获取结果,当我尝试将它调用到另一个时,它没有出现。

what mistake i make?我犯了什么错误? can someone help me?有人能帮我吗?

The method public void serialEvent(SerialPortEvent oEvent) is executed when something is received from serial port (it is not intended to be called by yourself), so inside it you need to process what is received, to picture it (based on your example, pay attention to the comments inside, there you should do something with the received data):方法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());
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM