首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Java中显示com端口数据

在Java中显示com端口数据
EN

Stack Overflow用户
提问于 2014-12-21 21:21:54
回答 1查看 2.1K关注 0票数 2

下面是读取com端口数据五次并在GUI中显示的代码。它的作用就像

  1. 读取com端口中的数据五次,最后启动gui,并一次显示所有五个数据。我的目标是先启动GUi
  2. 读取第一次数据并在GUI中显示
  3. 附加第二个数据并在GUI中显示。在GUI中追加第5个数据和显示

GUi是使用Nebean创建的

代码语言:javascript
复制
   /*
     * 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 learn;

import jssc.SerialPort;
import jssc.SerialPortException;

/**
 *
 * @author Mica
 */
public class gui extends javax.swing.JFrame {

    /**
     * Creates new form gui
     */
        public gui() {
        initComponents();
         comport();

    }

    private void comport(){

    SerialPort serialPort = new SerialPort("COM3");

        int count = 1;


         while (count < 11) {
         try {

            serialPort.openPort();//Open serial port
            serialPort.setParams(9600, 8, 1, 0);//Set params.
            byte[] buffer = serialPort.readBytes(32);//Read 10 bytes from serial port
            final String readed = new String(buffer);
            System.out.println("««Readed from COM"  + ": " + readed);

            jTextArea1.append(readed+ "\n" );
            serialPort.closePort();//Close serial port

        }
        catch (SerialPortException ex) {
           System.out.println(ex);
        }
        count++;
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Create and display the form */

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {


                new gui().setVisible(true);

            }

        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   
}
EN

Stack Overflow用户

回答已采纳

发布于 2014-12-22 01:00:06

扩展SwingWorker。您可以在实现doInBackground()publish()临时结果和将结果读取到process()实现中的JTextArea中读取串口,如here所示。

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27593964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档