zsh_root
2025-01-06 7857a444de69124e9f7fb45f98b0ae818b107f23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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;
    }
}