Hello下面是读取COM端口数据五次并在GUI中显示的代码。它像
GUi是使用Nebeans创建的
/*
* 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
}
扩展SwingWorker
。您可以阅读你的实现串口doInBackground()
,publish()
中期业绩和append()
结果,以一个JTextArea
在你的实现process()
,如图所示这里。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句