Это код, который захватывает данные последовательного порта и передает их во фрейм GUI в JTextField. Я использовал консоль терминала, чтобы проверить, все ли данные были захвачены, он захватил все данные. Когда я добавил в сканер str0.setText (ячейка [0]), поток данных останавливается на ячейке 0, и в кадре отображаются только две ячейки (данные ячейки 0 и ячейки 1), а затем он останавливается.
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.fazecast.jSerialComm.SerialPort;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.*;
import java.awt.Cursor;
import java.awt.Rectangle;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextPane;
public class DisplayTest1 extends JFrame implements TextListener {
public JTextField txtTextfield_31;//57600
public JTextField str0;
public JTextField str1;
public JTextField str2;
public JTextField str3;
public JTextField str4;
public JTextField str5;
public JTextField str6;
public JTextField str7;
public JTextField str8;
public JTextField str9;
public JTextField txtTextfield;//COM3
public int cd;
public String textField;
public String cell0;
public String cell1;
public String cell2;
public String cell3;
public String cell4;
public String cell5;
public String cell6;
public String cell7;
public String cell8;
public String cell9;
private javax.swing.JComboBox bd;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DroneDisplayTest1 frame = new DroneDisplayTest1();
frame.setResizable(false);
frame.setVisible(true);
new JTextField().setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
str0 = new JTextField(40);
str0.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
str0.getText();
str0.setText(cell0);
}
});
str0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
str0.getText();
str0.setText(cell0);
}
});
/*str0.addContainerListener(new ContainerAdapter() {
@Override
public void componentAdded(ContainerEvent arg0) {
str0.getText();
str0.updateUI();
str0.setText(cell0);
}});
*/
str0.addInputMethodListener(new InputMethodListener() {
public void caretPositionChanged(InputMethodEvent arg0) {
}
public void inputMethodTextChanged(InputMethodEvent arg0) {
str0.getText();
str0.setText(cell0);
}
});
/*str0.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent arg0) {
}
});
*/
str0.setEditable(false);
str0.setFont(new Font("Tahoma", Font.BOLD, 12));
str0.setHorizontalAlignment(SwingConstants.CENTER);
str0.setForeground(Color.WHITE);
str0.setColumns(10);//TIME
str0.setBackground(Color.BLACK);
str0.setBounds(545, 115, 251, 20);
getContentPane().add(str0);
str1 = new JTextField(40);
str1.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent arg0) {
str1.getText();
//str1.updateUI();
str1.setText(cell1);
}
});
str1.addContainerListener(new ContainerAdapter() {
@Override
public void componentAdded(ContainerEvent arg0) {
str1.getText();
str1.updateUI();
str1.setText(cell1);
}
});
str1.setEditable(false);
str1.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent arg0) {
str1.setText(cell1);
}
});
str1.addInputMethodListener(new InputMethodListener() {
public void caretPositionChanged(InputMethodEvent arg0) {
}
public void inputMethodTextChanged(InputMethodEvent arg0) {
str1.getText();
str1.setText(cell1);
}
});
str1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
pink();
str1.getText();
str1.add(getStr1());
str1.setText(cell1);
}
});
str1.setBounds(180, 114, 162, 21);
str1.setFont(new Font("Tahoma", Font.BOLD, 12));
str1.setHorizontalAlignment(SwingConstants.CENTER);
str1.setForeground(Color.WHITE);
str1.setColumns(10);//GPS LATITUDE
str1.setBackground(Color.BLACK);
//str1.setText("35.214153");
contentPane.add(str1);
public void pink () {
SerialPort dronePorts [] = SerialPort.getCommPorts();
try{
SerialPort port = dronePorts[0];
if (port.openPort())
port.setBaudRate(57600);
port.setNumDataBits(8);
port.setParity(0);
port.setNumStopBits(1);
System.out.println("PortName: " + "Port" + " is connected ");
port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING,0,0);
// open file
Scanner scanner = new Scanner(port.getInputStream());
// read each line in the file
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String [] cell = line.split(",");
//this where u put the label to capture the data.
String b = "TIME, LAT, LONG, RSSI, PH,ENB, ET, IV, RV, TEMP";
String [] stre = b.split(",");
stre [0] = "TIME : " ;
stre [1] = "GPS LATITUDE : ";
stre [2] = "GPS LONGITUDE : ";
stre [3] = "RSSI : ";
stre [4] = "PHASE : ";
stre [5] = "EPC No. OF BYTES : ";
stre [6] = "EPC TAGBYTES : ";
stre [7] = "INPUT VOLTAGE : ";
stre [8] = "RFID VOLTAGE : ";
stre [9] = "TEMPERATURE : ";
if(cell.length > 7){
System.out.println("") ;
System.out.println("NewLine-Inside the pinkDroneDisplayTest") ;
for(int i = 0; i < cell.length; i++) {
System.out.println(stre[i]+cell[i]);
//THIS IS WORKING, problem how to transfer the str to the frame
str0.setText(cell[0]);
str1.setText(cell[1]);
str2.setText(cell[2]);
str3.setText(cell[3]);
str4.setText(cell[4]);
str5.setText(cell[5]);
str6.setText(cell[6]);
str7.setText(cell[7]);
str8.setText(cell[8]);
str9.setText(cell[9]);
}}};
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "Unable to connect to Port");
}
return;
}