本文介绍了Java:在JFrame类中获取RFID Serial Rs232数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**从rfid扫描程序获取数据作为JFrame **中的串行rs232端口,在这种情况下,我已经制作了一个JFrame表单,并且我有一个JTextField用于将数据rfid存储为String。实际上,我发现一个来自互联网的类将读取器读取为字节,现在如何将我的RFID类中的数据作为字符串从我的主类中的JTextfield Jframe获取?



这里我的RFID课程:



**Getting data on from rfid scanner as Serial rs232 port in JFrame**, in this case I have made a JFrame form and I have a JTextField for storing data rfid as a String. Actually, I found a class from internet that read the RFID reader as byte, now How To get data from my RFID Class to my JTextfield Jframe in my main class as String?

Here my RFID class :

package view;

import java.util.*;
import java.io.*;
import javax.comm.*;

public class ListPorts implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration portList;
    
    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;
    
    public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();
    
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM30")) {
            //                if (portId.getName().equals("/dev/term/a")) {
                    ListPorts reader = new ListPorts();
                }
            }
        }
    }
    
    public ListPorts() {
        try {
            serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
        } catch (PortInUseException e) {System.out.println(e);}
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {System.out.println(e);}
    try {
            serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {System.out.println(e);}
        serialPort.notifyOnDataAvailable(true);
        try {
            serialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {System.out.println(e);}
        readThread = new Thread(this);
        readThread.start();
    }
    
    public void run() {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {System.out.println(e);}
    }
    
    public void serialEvent(SerialPortEvent event) {
        switch(event.getEventType()) {
        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
        case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[20];
    
            try {
                while (inputStream.available() > 0) {
                    int numBytes = inputStream.read(readBuffer);
                }
                System.out.print(new String(readBuffer)); // I Have to get this data to my Jtextfield
            } catch (IOException e) {System.out.println(e);}
            break;
        }
    }
}

推荐答案

这篇关于Java:在JFrame类中获取RFID Serial Rs232数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 09:14