在JavaGUI中显示com端口数据
下面是读取com端口数据五次并在GUI中显示的代码。它工作起来就像在JavaGUI中显示com端口数据,java,swing,user-interface,serial-port,Java,Swing,User Interface,Serial Port,下面是读取com端口数据五次并在GUI中显示的代码。它工作起来就像 读取com端口中可用的数据五次,最后启动gui,一次显示所有五个数据。 我的目标是首先启动GUi,然后 读取第一个数据并在GUI中显示 附加第二个数据并在GUI中显示 .. 附加第5个数据并在GUI中显示 GUi是使用Nebeans创建的 /* * To change this license header, choose License Headers in Project Properties. *
/*
* 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
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装学习;
导入jssc.SerialPort;
导入jssc.SerialPortException;
/**
*
*@author Mica
*/
公共类gui扩展了javax.swing.JFrame{
/**
*创建新表单gui
*/
公共图形用户界面(){
初始化组件();
comport();
}
私有void comport(){
SerialPort SerialPort=新的SerialPort(“COM3”);
整数计数=1;
而(计数<11){
试一试{
serialPort.openPort();//打开串行端口
serialPort.setParams(9600,8,1,0);//设置参数。
byte[]buffer=serialPort.readBytes(32);//从串行端口读取10个字节
读取的最终字符串=新字符串(缓冲区);
System.out.println(“««从COM读取“+”:“+Readed”);
jTextArea1.append(readed+“\n”);
serialPort.closePort();//关闭串行端口
}
捕获(SerialPortException例外){
系统输出打印项次(ex);
}
计数++;
}
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
}//
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新建gui().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JScrollPane-jScrollPane1;
私有javax.swing.JTextArea jTextArea1;
//变量结束声明
}
扩展。您可以在doInBackground()
的实现中读取串行端口、publish()
临时结果,以及append()
在process()
的实现中将结果添加到JTextArea
,如图所示 是的,我可以运行你给我的例子。当按下开始按钮时,显示数据。当com端口上有可用数据时,不使用按钮是否可以在textarea上获取数据?任何建议不需要该按钮;您可以根据需要在doInBackground()
中循环和/或睡眠。