|
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+package com.sainyu.beidou3.demo;
|
|
|
2
|
+
|
|
|
3
|
+import com.yang.protocolstack.ProtocolStack;
|
|
|
4
|
+import com.yang.protocolstack.protocol.*;
|
|
|
5
|
+
|
|
|
6
|
+import javax.swing.*;
|
|
|
7
|
+import java.awt.*;
|
|
|
8
|
+import java.awt.event.ActionEvent;
|
|
|
9
|
+import java.net.InetAddress;
|
|
|
10
|
+import java.text.SimpleDateFormat;
|
|
|
11
|
+import java.util.Arrays;
|
|
|
12
|
+import java.util.Date;
|
|
|
13
|
+
|
|
|
14
|
+public class SocketFrame extends JFrame {
|
|
|
15
|
+
|
|
|
16
|
+ /**
|
|
|
17
|
+ * 程序界面宽度
|
|
|
18
|
+ */
|
|
|
19
|
+ private static final int WIDTH = 500;
|
|
|
20
|
+
|
|
|
21
|
+ /**
|
|
|
22
|
+ * 程序界面高度
|
|
|
23
|
+ */
|
|
|
24
|
+ private static final int HEIGHT = 500;
|
|
|
25
|
+
|
|
|
26
|
+ //数据显示界面
|
|
|
27
|
+ private JTextArea dataView = new JTextArea();
|
|
|
28
|
+ private JScrollPane scrollDataView = new JScrollPane(dataView);
|
|
|
29
|
+
|
|
|
30
|
+ // 网设置面板
|
|
|
31
|
+ private JPanel socketPanel = new JPanel();
|
|
|
32
|
+ private JLabel ipLabel = new JLabel("IP");
|
|
|
33
|
+ private JLabel portLabel = new JLabel("端口号");
|
|
|
34
|
+ private JLabel protocolTypeLabel = new JLabel("协议");
|
|
|
35
|
+ private JTextField ipField = new JTextField(12);
|
|
|
36
|
+ private JTextField portField = new JTextField(12);
|
|
|
37
|
+ private JComboBox protocolTypeChoice = new JComboBox();
|
|
|
38
|
+
|
|
|
39
|
+ private JButton LiuLiangCheck = new JButton("流量信息");
|
|
|
40
|
+ private JButton LiuLiangReSet = new JButton("记录清零");
|
|
|
41
|
+
|
|
|
42
|
+ // 操作面板
|
|
|
43
|
+ private JPanel operatePanel = new JPanel();
|
|
|
44
|
+ private JButton serialPortOperate = new JButton("打开网口");
|
|
|
45
|
+ private JButton iCInfoOperate = new JButton("读卡");
|
|
|
46
|
+ private JButton iCSubInfoOperate = new JButton("读取下属");
|
|
|
47
|
+ private JButton locateOperate = new JButton("定位");
|
|
|
48
|
+ private JButton powerOperate = new JButton("功率检测");
|
|
|
49
|
+ private JButton stateCheck = new JButton("自检信息");
|
|
|
50
|
+
|
|
|
51
|
+ private JButton locRepOperate = new JButton("位置报告");
|
|
|
52
|
+ private JButton sendMsgCOperate = new JButton("通信(汉字)");
|
|
|
53
|
+ private JButton sendMsgBOperate = new JButton("通信(BCD)");
|
|
|
54
|
+ private JButton sendMsgMOperate = new JButton("通信(混发)");
|
|
|
55
|
+
|
|
|
56
|
+ private int id;
|
|
|
57
|
+ private int SubCount = 0 ;
|
|
|
58
|
+ private int protocolType;
|
|
|
59
|
+ private SocketClient socketClient;
|
|
|
60
|
+ private ProtocolStack protocolStack;
|
|
|
61
|
+
|
|
|
62
|
+ private Thread readThread = new Thread(new Runnable() {
|
|
|
63
|
+ @Override
|
|
|
64
|
+ public void run() {
|
|
|
65
|
+ while (true) {
|
|
|
66
|
+ Response response;
|
|
|
67
|
+ if (protocolStack != null) {
|
|
|
68
|
+ response = protocolStack.getResponse();
|
|
|
69
|
+ if (response != null) {
|
|
|
70
|
+ switch (response.getMsgType()) {
|
|
|
71
|
+ case MsgType.BSI_PROTOCOL:
|
|
|
72
|
+ //2.1协议
|
|
|
73
|
+ dataView.append( "波束强度:" + Arrays.toString(((BDBSIResponse) response).getPower()) + "\r\n");
|
|
|
74
|
+ break;
|
|
|
75
|
+ case MsgType.ZDA_PROTOCOL:
|
|
|
76
|
+ //2.1协议
|
|
|
77
|
+ dataView.append("时间:" + ((BDZDAResponse) response).getTimeBJ() + "\r\n");
|
|
|
78
|
+ break;
|
|
|
79
|
+ case MsgType.GLZK_PROTOCOL:
|
|
|
80
|
+ //4.0协议
|
|
|
81
|
+ dataView.append("波束强度:" + Arrays.toString(((BDGLZKResponse) response).getPower())+ "\r\n");
|
|
|
82
|
+ break;
|
|
|
83
|
+ case MsgType.ICXX_PROTOCOL:
|
|
|
84
|
+ dataView.append("北斗卡号:" + ((BDICXXResponse) response).getId() + "\r\n" +
|
|
|
85
|
+ "通播地址:" + ((BDICXXResponse) response).getBroadcastId() + "\r\n" +
|
|
|
86
|
+ "用户机特征:" + ((BDICXXResponse) response).getFeature() + "\r\n" +
|
|
|
87
|
+ "服务频度:" + ((BDICXXResponse) response).getServerFreq() + "\r\n" +
|
|
|
88
|
+ "通信长度:" + ((BDICXXResponse) response).getMaxCommLength() + "\r\n");
|
|
|
89
|
+ id = ((BDICXXResponse) response).getId();
|
|
|
90
|
+ SubCount = ((BDICXXResponse) response).getNumber();
|
|
|
91
|
+ break;
|
|
|
92
|
+ case MsgType.ICI_PROTOCOL:
|
|
|
93
|
+ dataView.append("北斗卡号:" + ((BDICIResponse) response).getLocalId() + "\r\n" +
|
|
|
94
|
+ "通播地址:" + ((BDICIResponse) response).getBroadcastId() + "\r\n" +
|
|
|
95
|
+ "用户机特征:" + ((BDICIResponse) response).getFeature() + "\r\n" +
|
|
|
96
|
+ "服务频度:" + ((BDICIResponse) response).getServerFreq() + "\r\n" +
|
|
|
97
|
+ "通信长度:" + ((BDICIResponse) response).getMaxCommLength() + "\r\n");
|
|
|
98
|
+ id = ((BDICIResponse) response).getLocalId();
|
|
|
99
|
+ break;
|
|
|
100
|
+ case MsgType.SJXX_PROTOCOL:
|
|
|
101
|
+ //4.0协议
|
|
|
102
|
+ dataView.append("时间:" + ((BDSJXXResponse) response).getTime() + "\r\n");
|
|
|
103
|
+ break;
|
|
|
104
|
+ case MsgType.DWR_PROTOCOL:
|
|
|
105
|
+ dataView.append(String.format("定位位置:经度: %.06f 纬度: %.06f %n",((BDDWRResponse) response).getLongitude(),((BDDWRResponse) response).getLatitude()));
|
|
|
106
|
+ break;
|
|
|
107
|
+ case MsgType.DWXX_PROTOCOL:
|
|
|
108
|
+ dataView.append(String.format("定位位置:经度: %s 纬度: %s %n",((BDDWXXResponse) response).getLongitude(),((BDDWXXResponse) response).getLatitude()));
|
|
|
109
|
+ break;
|
|
|
110
|
+ case MsgType.FKI_PROTOCOL:
|
|
|
111
|
+ if (((BDFKIResponse) response).isCmdRes()) {
|
|
|
112
|
+ dataView.append("申请成功,等待反馈!\r\n");
|
|
|
113
|
+ } else {
|
|
|
114
|
+ dataView.append(String.format("申请失败,频度未到。等待时间:%d s%n",((BDFKIResponse) response).getWaitTime()));
|
|
|
115
|
+ }
|
|
|
116
|
+ break;
|
|
|
117
|
+ case MsgType.FKXX_PROTOCOL:
|
|
|
118
|
+ if (((BDFKXXResponse) response).isCmdResult()) {
|
|
|
119
|
+ dataView.append("申请成功,等待反馈!\r\n");
|
|
|
120
|
+ } else {
|
|
|
121
|
+ dataView.append(String.format("申请失败,频度未到。等待时间:%d s%n",((BDFKXXResponse) response).getWaitTime()));
|
|
|
122
|
+ }
|
|
|
123
|
+ break;
|
|
|
124
|
+ case MsgType.WAA_PROTOCOL:
|
|
|
125
|
+ dataView.append(String.format("收到位置报告:经度: %.06f 纬度: %.06f %n",((BDWAAResponse) response).getLongitude(),((BDWAAResponse) response).getLatitude()));
|
|
|
126
|
+ break;
|
|
|
127
|
+ case MsgType.TXR_PROTOCOL:
|
|
|
128
|
+ dataView.append(String.format("收到通信信息:内容: %s 发送方: %d 通信类型: %d %n",((BDTXRResponse) response).getCommInfo(),((BDTXRResponse) response).getSenderID(), ((BDTXRResponse) response).getCodeType()));
|
|
|
129
|
+ break;
|
|
|
130
|
+ case MsgType.TXXX_PROTOCOL:
|
|
|
131
|
+ if (((BDTXXXResponse) response).getType() == 3) {
|
|
|
132
|
+ dataView.append(String.format("4.0协议通过通信发送接收位置报告,接收到的位置信息: 时间: %tT,经度: %s,纬度: %s,高程: %d%n",new Date(((BDTXXXResponse) response).getTime()),
|
|
|
133
|
+ ((BDTXXXResponse) response).getLongitude(),((BDTXXXResponse) response).getLatitude(),((BDTXXXResponse) response).getAltitude()));
|
|
|
134
|
+ } else {
|
|
|
135
|
+ dataView.append(String.format("收到通信信息:内容: %s 发送方: %s 通信类型: %d %n",((BDTXXXResponse) response).getContent(),((BDTXXXResponse) response).getSendId(), ((BDTXXXResponse) response).getType()));
|
|
|
136
|
+ }
|
|
|
137
|
+ break;
|
|
|
138
|
+ case MsgType.ZJX_PROTOCOL:{//2.1协议
|
|
|
139
|
+ dataView.append("自检信息 IC状态:"+ ((BDZJXResponse) response).getCardState() + "\r\n");
|
|
|
140
|
+ dataView.append("自检信息 硬件状态:"+ ((BDZJXResponse) response).getHardState() + "\r\n");
|
|
|
141
|
+ dataView.append("自检信息 入站状态:"+ ((BDZJXResponse) response).getInState() + "\r\n");
|
|
|
142
|
+ }
|
|
|
143
|
+ break ;
|
|
|
144
|
+ case MsgType.ZJXX_PROTOCOL:{//4.0协议
|
|
|
145
|
+ dataView.append("自检信息 IC状态:"+ ((BDZJXXResponse) response).getCardState() + "\r\n");
|
|
|
146
|
+ dataView.append("自检信息 硬件状态:"+ ((BDZJXXResponse) response).getHardState() + "\r\n");
|
|
|
147
|
+ dataView.append("自检信息 入站状态:"+ ((BDZJXXResponse) response).getInState() + "\r\n");
|
|
|
148
|
+ }
|
|
|
149
|
+ break;
|
|
|
150
|
+ }
|
|
|
151
|
+ dataView.setCaretPosition(dataView.getDocument().getLength());
|
|
|
152
|
+ } else {
|
|
|
153
|
+ try {
|
|
|
154
|
+ Thread.sleep(1);
|
|
|
155
|
+ } catch (InterruptedException e) {
|
|
|
156
|
+ e.printStackTrace();
|
|
|
157
|
+ }
|
|
|
158
|
+ }
|
|
|
159
|
+ } else {
|
|
|
160
|
+ try {
|
|
|
161
|
+ Thread.sleep(1);
|
|
|
162
|
+ } catch (InterruptedException e) {
|
|
|
163
|
+ e.printStackTrace();
|
|
|
164
|
+ }
|
|
|
165
|
+ }
|
|
|
166
|
+ }
|
|
|
167
|
+ }
|
|
|
168
|
+ });
|
|
|
169
|
+
|
|
|
170
|
+
|
|
|
171
|
+ public SocketFrame() {
|
|
|
172
|
+ initView();
|
|
|
173
|
+ initComponents();
|
|
|
174
|
+ actionListener();
|
|
|
175
|
+ initData();
|
|
|
176
|
+ }
|
|
|
177
|
+
|
|
|
178
|
+ private void initView() {
|
|
|
179
|
+ // 关闭程序
|
|
|
180
|
+ setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
|
|
181
|
+ // 禁止窗口最大化
|
|
|
182
|
+ setResizable(false);
|
|
|
183
|
+
|
|
|
184
|
+ // 设置程序窗口居中显示
|
|
|
185
|
+ Point p = GraphicsEnvironment.getLocalGraphicsEnvironment()
|
|
|
186
|
+ .getCenterPoint();
|
|
|
187
|
+ setBounds(p.x - WIDTH / 2, p.y - HEIGHT / 2, WIDTH, HEIGHT);
|
|
|
188
|
+ this.setLayout(null);
|
|
|
189
|
+
|
|
|
190
|
+ setTitle("网口北斗通信Demo");
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+ private void initComponents() {
|
|
|
194
|
+ // 数据显示
|
|
|
195
|
+ dataView.setFocusable(false);
|
|
|
196
|
+ scrollDataView.setBounds(10, 10, 475, 190);
|
|
|
197
|
+ add(scrollDataView);
|
|
|
198
|
+
|
|
|
199
|
+ // 网口设置
|
|
|
200
|
+ socketPanel.setBorder(BorderFactory.createTitledBorder("网口设置"));
|
|
|
201
|
+ socketPanel.setBounds(10, 200, 170, 190);
|
|
|
202
|
+ socketPanel.setLayout(null);
|
|
|
203
|
+ add(socketPanel);
|
|
|
204
|
+
|
|
|
205
|
+ ipLabel.setForeground(Color.gray);
|
|
|
206
|
+ ipLabel.setBounds(10, 30, 40, 20);
|
|
|
207
|
+ socketPanel.add(ipLabel);
|
|
|
208
|
+
|
|
|
209
|
+ ipField.setBounds(60, 30, 100, 20);
|
|
|
210
|
+ socketPanel.add(ipField);
|
|
|
211
|
+
|
|
|
212
|
+ portLabel.setForeground(Color.gray);
|
|
|
213
|
+ portLabel.setBounds(10, 65, 40, 20);
|
|
|
214
|
+ socketPanel.add(portLabel);
|
|
|
215
|
+
|
|
|
216
|
+ portField.setBounds(60, 65, 100, 20);
|
|
|
217
|
+ socketPanel.add(portField);
|
|
|
218
|
+
|
|
|
219
|
+ protocolTypeLabel.setForeground(Color.gray);
|
|
|
220
|
+ protocolTypeLabel.setBounds(10, 100, 40, 20);
|
|
|
221
|
+ socketPanel.add(protocolTypeLabel);
|
|
|
222
|
+
|
|
|
223
|
+ protocolTypeChoice.setFocusable(false);
|
|
|
224
|
+ protocolTypeChoice.setBounds(60, 100, 100, 20);
|
|
|
225
|
+ socketPanel.add(protocolTypeChoice);
|
|
|
226
|
+
|
|
|
227
|
+ LiuLiangCheck.setFocusable(false);
|
|
|
228
|
+ LiuLiangCheck.setBounds(10, 130, 100, 20);
|
|
|
229
|
+ socketPanel.add(LiuLiangCheck);
|
|
|
230
|
+
|
|
|
231
|
+ LiuLiangReSet.setFocusable(false);
|
|
|
232
|
+ LiuLiangReSet.setBounds(10, 160, 100, 20);
|
|
|
233
|
+ socketPanel.add(LiuLiangReSet);
|
|
|
234
|
+
|
|
|
235
|
+ // 操作
|
|
|
236
|
+ operatePanel.setBorder(BorderFactory.createTitledBorder("操作"));
|
|
|
237
|
+ operatePanel.setBounds(200, 200, 285, 180);
|
|
|
238
|
+ operatePanel.setLayout(null);
|
|
|
239
|
+ add(operatePanel);
|
|
|
240
|
+
|
|
|
241
|
+
|
|
|
242
|
+ serialPortOperate.setFocusable(false);
|
|
|
243
|
+ serialPortOperate.setBounds(17, 22, 120, 20);
|
|
|
244
|
+ operatePanel.add(serialPortOperate);
|
|
|
245
|
+
|
|
|
246
|
+ iCInfoOperate.setFocusable(false);
|
|
|
247
|
+ iCInfoOperate.setBounds(17, 52, 120, 20);
|
|
|
248
|
+ operatePanel.add(iCInfoOperate);
|
|
|
249
|
+
|
|
|
250
|
+ locateOperate.setFocusable(false);
|
|
|
251
|
+ locateOperate.setBounds(17, 82, 120, 20);
|
|
|
252
|
+ operatePanel.add(locateOperate);
|
|
|
253
|
+
|
|
|
254
|
+ powerOperate.setFocusable(false);
|
|
|
255
|
+ powerOperate.setBounds(17, 112, 120, 20);
|
|
|
256
|
+ operatePanel.add(powerOperate);
|
|
|
257
|
+
|
|
|
258
|
+ locRepOperate.setFocusable(false);
|
|
|
259
|
+ locRepOperate.setBounds(147, 22, 120, 20);
|
|
|
260
|
+ operatePanel.add(locRepOperate);
|
|
|
261
|
+
|
|
|
262
|
+ sendMsgCOperate.setFocusable(false);
|
|
|
263
|
+ sendMsgCOperate.setBounds(147, 52, 120, 20);
|
|
|
264
|
+ operatePanel.add(sendMsgCOperate);
|
|
|
265
|
+
|
|
|
266
|
+ sendMsgBOperate.setFocusable(false);
|
|
|
267
|
+ sendMsgBOperate.setBounds(147, 82, 120, 20);
|
|
|
268
|
+ operatePanel.add(sendMsgBOperate);
|
|
|
269
|
+
|
|
|
270
|
+ sendMsgMOperate.setFocusable(false);
|
|
|
271
|
+ sendMsgMOperate.setBounds(147, 112, 120, 20);
|
|
|
272
|
+ operatePanel.add(sendMsgMOperate);
|
|
|
273
|
+
|
|
|
274
|
+ iCSubInfoOperate.setFocusable(false);
|
|
|
275
|
+ iCSubInfoOperate.setBounds(147, 142, 120, 20);
|
|
|
276
|
+ operatePanel.add(iCSubInfoOperate);
|
|
|
277
|
+
|
|
|
278
|
+ stateCheck.setFocusable(false);
|
|
|
279
|
+ stateCheck.setBounds(17, 142, 120, 20);
|
|
|
280
|
+ operatePanel.add(stateCheck);
|
|
|
281
|
+ }
|
|
|
282
|
+
|
|
|
283
|
+ private void actionListener() {
|
|
|
284
|
+
|
|
|
285
|
+ LiuLiangCheck.addActionListener(e -> {
|
|
|
286
|
+ if (protocolStack != null) {
|
|
|
287
|
+ long[] arr =protocolStack.GetLiuLiangXX();
|
|
|
288
|
+
|
|
|
289
|
+ Date recordDate = new Date(arr[0]);
|
|
|
290
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
291
|
+ dataView.append("记录时间:" + format.format(recordDate) + " 发送总数:" + arr[1] + " 成功:" + arr[2] + " 接收: " + arr[3] + "\r\n");
|
|
|
292
|
+ }
|
|
|
293
|
+ });
|
|
|
294
|
+
|
|
|
295
|
+ LiuLiangReSet.addActionListener(e -> {
|
|
|
296
|
+ if (protocolStack != null) {
|
|
|
297
|
+ protocolStack.ResetLiuLiangXX();
|
|
|
298
|
+ dataView.append("流量记录清除\r\n");
|
|
|
299
|
+ }
|
|
|
300
|
+ });
|
|
|
301
|
+
|
|
|
302
|
+ serialPortOperate.addActionListener(e -> {
|
|
|
303
|
+ if (("打开网口").equals(serialPortOperate.getText())) {
|
|
|
304
|
+ openSocket(e);
|
|
|
305
|
+ } else {
|
|
|
306
|
+ closeSocket(e);
|
|
|
307
|
+ }
|
|
|
308
|
+ });
|
|
|
309
|
+ iCInfoOperate.addActionListener(e -> {
|
|
|
310
|
+ if (protocolStack != null) {
|
|
|
311
|
+ dataView.append("读卡申请中、、" + "\r\n");
|
|
|
312
|
+ socketClient.send(protocolStack.getDKSQ());
|
|
|
313
|
+ }
|
|
|
314
|
+ });
|
|
|
315
|
+
|
|
|
316
|
+ iCSubInfoOperate.addActionListener(e -> {
|
|
|
317
|
+ if (protocolStack != null) {
|
|
|
318
|
+ dataView.append("读子用户申请中、、" + "\r\n");
|
|
|
319
|
+ byte icNo = 1;
|
|
|
320
|
+ for (int i= 0; i < SubCount/20; i++)
|
|
|
321
|
+ socketClient.send(protocolStack.getZYHDKSQ(icNo,id+"",false));//默认百用户 千用户时需要设置为true
|
|
|
322
|
+ }
|
|
|
323
|
+ });
|
|
|
324
|
+
|
|
|
325
|
+ locateOperate.addActionListener(e -> {
|
|
|
326
|
+ if (protocolStack != null) {
|
|
|
327
|
+ if (id == 0) {
|
|
|
328
|
+ dataView.append("未读卡,在获取到卡号之后才能定位" + "\r\n");
|
|
|
329
|
+ } else {
|
|
|
330
|
+ dataView.append("定位申请中、、" + "\r\n");
|
|
|
331
|
+ socketClient.send(protocolStack.getDWSQ(String.valueOf(id),false,0));
|
|
|
332
|
+ }
|
|
|
333
|
+ }
|
|
|
334
|
+ });
|
|
|
335
|
+
|
|
|
336
|
+ powerOperate.addActionListener(e -> {
|
|
|
337
|
+ if (protocolStack != null) {
|
|
|
338
|
+ dataView.append("检测功率中、、 " + "\r\n");
|
|
|
339
|
+ socketClient.send( protocolStack.getGLSQ());
|
|
|
340
|
+ }
|
|
|
341
|
+ });
|
|
|
342
|
+
|
|
|
343
|
+ stateCheck.addActionListener(e -> {
|
|
|
344
|
+ if (protocolStack != null) {
|
|
|
345
|
+ dataView.append("检测功率中、、 " + "\r\n");
|
|
|
346
|
+ socketClient.send( protocolStack.getZJSQ());
|
|
|
347
|
+ }
|
|
|
348
|
+ });
|
|
|
349
|
+
|
|
|
350
|
+ locRepOperate.addActionListener(e -> {
|
|
|
351
|
+ if (protocolStack != null) {
|
|
|
352
|
+ if (id == 0) {
|
|
|
353
|
+ dataView.append("未读卡,未获取到卡号" + "\r\n");
|
|
|
354
|
+ } else {
|
|
|
355
|
+ dataView.append("发送位置报告中、、" + "\r\n");
|
|
|
356
|
+ if (protocolType == 0) {
|
|
|
357
|
+ //仅2.1协议
|
|
|
358
|
+ socketClient.send(protocolStack.getWZBG(String.valueOf(id),0,null));
|
|
|
359
|
+ } else {
|
|
|
360
|
+ socketClient.send(protocolStack.getWZXX40(String.valueOf(id),new Date().getTime(),38.123d,112.456d,153,String.valueOf(id)));
|
|
|
361
|
+ }
|
|
|
362
|
+ }
|
|
|
363
|
+ }
|
|
|
364
|
+ });
|
|
|
365
|
+
|
|
|
366
|
+ sendMsgBOperate.addActionListener(e -> {
|
|
|
367
|
+ if (protocolStack != null) {
|
|
|
368
|
+ if (id == 0) {
|
|
|
369
|
+ dataView.append("未读卡,未获取到卡号" + "\r\n");
|
|
|
370
|
+ } else {
|
|
|
371
|
+ dataView.append("以代码方式发送通信信息,内容为 123 " + "\r\n");
|
|
|
372
|
+ socketClient.send(protocolStack.getTXSQ(1, String.valueOf(id),"123",true, String.valueOf(id)));
|
|
|
373
|
+ }
|
|
|
374
|
+ }
|
|
|
375
|
+ });
|
|
|
376
|
+
|
|
|
377
|
+ sendMsgCOperate.addActionListener(e -> {
|
|
|
378
|
+ if (protocolStack != null) {
|
|
|
379
|
+ if (id == 0) {
|
|
|
380
|
+ dataView.append("未读卡,未获取到卡号" + "\r\n");
|
|
|
381
|
+ } else {
|
|
|
382
|
+ dataView.append("以汉字方式发送通信信息,内容为 汉字 " + "\r\n");
|
|
|
383
|
+ socketClient.send(protocolStack.getTXSQ(0, String.valueOf(id),"汉字",true, String.valueOf(id)));
|
|
|
384
|
+ }
|
|
|
385
|
+ }
|
|
|
386
|
+ });
|
|
|
387
|
+
|
|
|
388
|
+ sendMsgMOperate.addActionListener(e -> {
|
|
|
389
|
+
|
|
|
390
|
+ socketClient.send(protocolStack.getTXSQ(2, String.valueOf("459650"),"汉字BCD",true, String.valueOf("459651")));
|
|
|
391
|
+ return ;
|
|
|
392
|
+
|
|
|
393
|
+// if (protocolStack != null) {
|
|
|
394
|
+// if (id == 0) {
|
|
|
395
|
+// dataView.append("未读卡,未获取到卡号" + "\r\n");
|
|
|
396
|
+// } else {
|
|
|
397
|
+// dataView.append("以混发方式发送通信信息,内容为 汉字BCD " + "\r\n");
|
|
|
398
|
+// socketClient.send(protocolStack.getTXSQ(2, String.valueOf(id),"汉字BCD",true, String.valueOf(id)));
|
|
|
399
|
+// }
|
|
|
400
|
+// }
|
|
|
401
|
+ });
|
|
|
402
|
+
|
|
|
403
|
+ }
|
|
|
404
|
+
|
|
|
405
|
+ private void closeSocket(ActionEvent e) {
|
|
|
406
|
+ protocolStack.stop();
|
|
|
407
|
+ socketClient.stop();
|
|
|
408
|
+ dataView.append("网口已关闭" + "\r\n");
|
|
|
409
|
+ serialPortOperate.setText("打开网口");
|
|
|
410
|
+ protocolStack = null;
|
|
|
411
|
+ }
|
|
|
412
|
+
|
|
|
413
|
+ private void openSocket(ActionEvent e) {
|
|
|
414
|
+ String ip = ipField.getText();
|
|
|
415
|
+ String prot = portField.getText();
|
|
|
416
|
+ protocolType = 0;
|
|
|
417
|
+ protocolType = protocolTypeChoice.getSelectedIndex();
|
|
|
418
|
+ try {
|
|
|
419
|
+ socketClient = new SocketClient(ip, Integer.parseInt(prot)) {
|
|
|
420
|
+ @Override
|
|
|
421
|
+ public void onReceive(InetAddress addr, byte[] bytes) {
|
|
|
422
|
+ protocolStack.setBytes(bytes);
|
|
|
423
|
+ }
|
|
|
424
|
+
|
|
|
425
|
+ @Override
|
|
|
426
|
+ public void onDisconnect(InetAddress addr) {
|
|
|
427
|
+ dataView.setText("网口已关闭" + "\r\n");
|
|
|
428
|
+ serialPortOperate.setText("打开网口");
|
|
|
429
|
+ }
|
|
|
430
|
+ };
|
|
|
431
|
+ protocolStack = new ProtocolStack();
|
|
|
432
|
+ protocolStack.init(protocolType,false);
|
|
|
433
|
+ socketClient.start();
|
|
|
434
|
+ } catch (Exception e1) {
|
|
|
435
|
+ e1.printStackTrace();
|
|
|
436
|
+ }
|
|
|
437
|
+ dataView.setText("网口已打开" + "\r\n");
|
|
|
438
|
+ serialPortOperate.setText("关闭网口");
|
|
|
439
|
+ if (!readThread.isAlive()) {
|
|
|
440
|
+ readThread.start();
|
|
|
441
|
+ }
|
|
|
442
|
+ }
|
|
|
443
|
+
|
|
|
444
|
+ @SuppressWarnings("unchecked")
|
|
|
445
|
+ private void initData() {
|
|
|
446
|
+ protocolTypeChoice.addItem("2.1协议");
|
|
|
447
|
+ protocolTypeChoice.addItem("4.0协议");
|
|
|
448
|
+
|
|
|
449
|
+ }
|
|
|
450
|
+
|
|
|
451
|
+ public static void main(String args[]) {
|
|
|
452
|
+ EventQueue.invokeLater(() -> new SocketFrame().setVisible(true));
|
|
|
453
|
+ }
|
|
|
454
|
+
|
|
|
455
|
+}
|