package index.JPanelMoudle;
|
|
import Anchor.anchor_Dell;
|
import baowen.Dell_Baowen;
|
import gnu.io.SerialPort;
|
import gnu.io.SerialPortEvent;
|
import tools.ChuanKou.SerialPortManager;
|
import tools.ChuanKou.SerialPortUtil;
|
import tools.GetNowTime;
|
import tools.ShowMessage;
|
import tools.Tools;
|
|
import javax.swing.*;
|
import javax.swing.border.TitledBorder;
|
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionListener;
|
import java.awt.event.FocusEvent;
|
import java.awt.event.FocusListener;
|
import java.util.TooManyListenersException;
|
|
public class ComMoudleComp {
|
|
|
private static JPanel com_jp;//选择串口模板
|
|
private static JLabel chuanKou_jl;//串口号标签
|
private static JLabel botelv_jl;//波特率标签
|
private static JComboBox chuanKou_jcom;//串口号下拉框
|
private static JComboBox boteLV_jcm;//波特率下拉框
|
private static JButton open;//打开串口按钮
|
private static JButton close;//关闭串口按钮
|
static String[] coms = null;
|
static String com = null;//选中的串口
|
static boolean chuankou = false;//串口是否被打开
|
static SerialPort serialPort = null;//串口对象
|
|
// 获取选择串口面板,若为空则创建
|
public static JPanel getCom_jp() {
|
if (com_jp == null) {
|
com_jp = new JPanel(); // 初始化该面板
|
com_jp.setBorder(new TitledBorder("选择串口"));
|
}
|
return com_jp;
|
}
|
|
// 获取 串口号标签,若为空则创建
|
public static JLabel getChuanKou_jl() {
|
if (chuanKou_jl == null) {
|
chuanKou_jl = new JLabel(" 串口号:"); // 初始化该标签
|
}
|
return chuanKou_jl;
|
}
|
|
// 获取波特率标签,若为空则创建
|
public static JLabel getBotelv_jl() {
|
if (botelv_jl == null) {
|
botelv_jl = new JLabel(" 波特率:"); // 初始化该标签
|
}
|
return botelv_jl;
|
}
|
|
// 获取 串口号下拉框,若为空则创建
|
public static JComboBox getChuanKou_jcom() {
|
if (chuanKou_jcom == null) {
|
chuanKou_jcom = new JComboBox(); // 初始化该下拉框
|
chuanKou_jcom.addFocusListener(new FocusListener() {
|
@Override
|
public void focusGained(FocusEvent e) {
|
int comnum = SerialPortManager.get_all_port().size();
|
if (coms.length != comnum) {
|
coms = new String[SerialPortManager.get_all_port().size()];
|
for (int i = 0; i < SerialPortManager.get_all_port().size(); i++) {
|
coms[i] = SerialPortManager.get_all_port().get(i);
|
}
|
ComboBoxModel<String> coModel = new DefaultComboBoxModel<>(coms);//下拉列表模型
|
chuanKou_jcom.setModel(coModel);
|
}
|
}
|
|
@Override
|
public void focusLost(FocusEvent e) {
|
|
}
|
});
|
if (SerialPortManager.get_all_port().size() != 0) {
|
coms = new String[SerialPortManager.get_all_port().size()];
|
for (int i = 0; i < SerialPortManager.get_all_port().size(); i++) {
|
coms[i] = SerialPortManager.get_all_port().get(i);
|
}
|
} else {
|
coms = new String[1];
|
coms[0] = "未识别到com口";
|
}
|
ComboBoxModel<String> coModel = new DefaultComboBoxModel<>(coms);//下拉列表模型
|
chuanKou_jcom.setModel(coModel);
|
}
|
return chuanKou_jcom;
|
}
|
|
|
// 获取波特率下拉框,若为空则创建
|
public static JComboBox getBoteLV_jcm() {
|
if (boteLV_jcm == null) {
|
String[] com = {"115200", "921600", "19200", "9600"};
|
boteLV_jcm = new JComboBox(com); // 初始化该下拉框
|
}
|
return boteLV_jcm;
|
}
|
|
|
// 获取 打开串口 按钮,若为空则创建
|
public static JButton getOpen() {
|
if (open == null) {
|
open = new JButton("打开串口"); // 初始化该按钮
|
open.addActionListener(new ActionListener() {
|
@Override
|
public void actionPerformed(ActionEvent e) {
|
com = (String) chuanKou_jcom.getSelectedItem();
|
if (com == null || com.equals("未识别到com口")) {
|
ShowMessage.zidingyi("未识别到com口,请插入配置线并重启软件");
|
comshuaxin();
|
return;
|
}
|
int bote = Integer.parseInt((String) boteLV_jcm.getSelectedItem());
|
if (!chuankou) {
|
serialPort = SerialPortManager.open_chuankou(com, bote);
|
if (serialPort == null) {
|
return;
|
}
|
chuankou = true;//串口被打开
|
open.setEnabled(false);
|
close.setEnabled(true);
|
try {
|
SerialPortUtil.setListenerToSerialPort(serialPort, event -> {
|
|
//数据通知
|
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
|
if (!versionUpgradeComp.isWrite()) {
|
byte[] bytes = SerialPortUtil.readData(serialPort);
|
if (bytes == null) {
|
return;
|
}
|
|
showDataComp.addAreaTextMes(bytes,"127.0.0.1",8888);
|
}
|
}
|
});
|
|
|
} catch (TooManyListenersException e1) {
|
// TODO 自动生成的 catch 块
|
e1.printStackTrace();
|
}
|
} else {
|
ShowMessage.zidingyi("请先关闭已打开的串口");
|
}
|
}
|
});
|
}
|
return open;
|
}
|
|
// 获取 关闭串口 按钮,若为空则创建
|
public static JButton getClose() {
|
if (close == null) {
|
close = new JButton("关闭串口"); // 初始化该按钮
|
close.setEnabled(false);
|
close.addActionListener(new ActionListener() {
|
@Override
|
public void actionPerformed(ActionEvent e) {
|
try {
|
if (chuankou) {
|
SerialPortUtil.closeSerialPort(serialPort);
|
open.setEnabled(true);
|
close.setEnabled(false);
|
chuankou = false;
|
anchor_Dell.getAnchors().removeAllElements();//清空信标基站的集合
|
anchorListComp.updateTable();//清空信标列表
|
} else {
|
ShowMessage.zidingyi("串口未打开");
|
}
|
} catch (Exception ex) {
|
throw new RuntimeException(ex);
|
}
|
}
|
});
|
}
|
return close;
|
}
|
|
|
//重新刷新串口
|
public static void comshuaxin() {
|
int comnum = SerialPortManager.get_all_port().size();
|
if (comnum != 0) {
|
coms = new String[SerialPortManager.get_all_port().size()];
|
for (int i = 0; i < SerialPortManager.get_all_port().size(); i++) {
|
coms[i] = SerialPortManager.get_all_port().get(i);
|
}
|
} else {
|
coms = new String[1];
|
coms[0] = "未识别到com口";
|
}
|
ComboBoxModel<String> coModel = new DefaultComboBoxModel<>(coms);//下拉列表模型
|
chuanKou_jcom.setModel(coModel);
|
chuanKou_jcom.repaint();
|
}
|
|
public static SerialPort getSerialPort() {
|
return serialPort;
|
}
|
|
public static boolean isChuankou() {
|
return chuankou;
|
}
|
}
|