【问题标题】:read data from a com port and write it to a text box in java从 com 端口读取数据并将其写入 java 中的文本框
【发布时间】:2013-12-16 14:48:51
【问题描述】:

我正在尝试从 com 端口(条形码扫描仪)读取数据并将其写入文本框。我对 java 比较陌生,所以我相信我缺少一些基本的东西,但是关于串行通信的文档是有限的。我正在尝试从条形码阅读器获取输入以写入我的 fxml 文档中内置的名为“txtONE”的文本框。这是我的代码:

    package javafxapplication7;

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    import gnu.io.CommPort;
    import gnu.io.CommPortIdentifier;
    import gnu.io.SerialPort;
    import gnu.io.SerialPortEvent;
    import gnu.io.SerialPortEventListener;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    /**
     *

     */
    public class JavaFXApplication7 extends Application {

        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

            Scene scene = new Scene(root);

            stage.setScene(scene);
            stage.show();
        }

        public JavaFXApplication7()
        {
            super();
        }

        void connect ( String portName ) throws Exception
        {
            CommPortIdentifier portIdentifier =         CommPortIdentifier.getPortIdentifier(portName);
            if ( portIdentifier.isCurrentlyOwned() )
            {
                System.out.println("Error: Port is currently in use");
            }
    else
    {
        CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

        if ( commPort instanceof SerialPort )
        {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

            InputStream in = serialPort.getInputStream();
            OutputStream out = serialPort.getOutputStream();

            (new Thread(new SerialWriter(out))).start();

            serialPort.addEventListener(new SerialReader(in));
            serialPort.notifyOnDataAvailable(true);

        }
        else
        {
            System.out.println("Error: Only serial ports are handled by this program.");
        }
    }     
}

/**
 * Handles the input coming from the serial port. A new line character
 * is treated as the end of a block in this example. 
 */
public static class SerialReader implements SerialPortEventListener 
{
    private InputStream in;
    private byte[] buffer = new byte[1024];

    public SerialReader ( InputStream in )
    {
        this.in = in;
    }

    @Override
    public void serialEvent(SerialPortEvent arg0) {
        int data;

        try
        {
            int len = 0;
            while ( ( data = in.read()) > -1 )
            {
                if ( data == '\n' ) {
                    break;
                }
                buffer[len++] = (byte) data;
            }
            System.out.print(new String(buffer,0,len));
            txtOne.text = new String(buffer,0,len);
        }
        catch ( IOException e )
        {
            e.printStackTrace();
            System.exit(-1);
        }             
    }

}

【问题讨论】:

  • 你期望的输出是什么,你得到什么输出?
  • 我正在尝试将数据写入我的 jframe 名称“txtOne”中的文本框,我所能做的就是让它写入系统。它无法识别 txtONE。

标签: java serial-port


【解决方案1】:

在java中读取com端口的简单方法,使用JNI(java本地接口) 读取存储数据的条形码的其他方式,当数据读取和保存(数据库/文件)使用java读取时。但是我在Java中使用串行端口和JNI,你读取存储的数据必须是第二种方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    相关资源
    最近更新 更多