Bladeren bron

first commit

hanqingsong 3 jaren geleden
commit
613d492999

+ 37 - 0
.gitignore

@@ -0,0 +1,37 @@
1
+HELP.md
2
+target/
3
+!.mvn/wrapper/maven-wrapper.jar
4
+!**/src/main/**/target/
5
+!**/src/test/**/target/
6
+
7
+### STS ###
8
+.apt_generated
9
+.classpath
10
+.factorypath
11
+.project
12
+.settings
13
+.springBeans
14
+.sts4-cache
15
+
16
+### IntelliJ IDEA ###
17
+.idea
18
+*.iws
19
+*.iml
20
+*.ipr
21
+
22
+### NetBeans ###
23
+/nbproject/private/
24
+/nbbuild/
25
+/dist/
26
+/nbdist/
27
+/.nb-gradle/
28
+build/
29
+!**/src/main/**/build/
30
+!**/src/test/**/build/
31
+
32
+### VS Code ###
33
+.vscode/
34
+
35
+### gradle ###
36
+*.gradle
37
+/build/**

BIN
gradle/wrapper/gradle-wrapper.jar


+ 5 - 0
gradle/wrapper/gradle-wrapper.properties

@@ -0,0 +1,5 @@
1
+distributionBase=GRADLE_USER_HOME
2
+distributionPath=wrapper/dists
3
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
4
+zipStoreBase=GRADLE_USER_HOME
5
+zipStorePath=wrapper/dists

+ 185 - 0
gradlew

@@ -0,0 +1,185 @@
1
+#!/usr/bin/env sh
2
+
3
+#
4
+# Copyright 2015 the original author or authors.
5
+#
6
+# Licensed under the Apache License, Version 2.0 (the "License");
7
+# you may not use this file except in compliance with the License.
8
+# You may obtain a copy of the License at
9
+#
10
+#      https://www.apache.org/licenses/LICENSE-2.0
11
+#
12
+# Unless required by applicable law or agreed to in writing, software
13
+# distributed under the License is distributed on an "AS IS" BASIS,
14
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+# See the License for the specific language governing permissions and
16
+# limitations under the License.
17
+#
18
+
19
+##############################################################################
20
+##
21
+##  Gradle start up script for UN*X
22
+##
23
+##############################################################################
24
+
25
+# Attempt to set APP_HOME
26
+# Resolve links: $0 may be a link
27
+PRG="$0"
28
+# Need this for relative symlinks.
29
+while [ -h "$PRG" ] ; do
30
+    ls=`ls -ld "$PRG"`
31
+    link=`expr "$ls" : '.*-> \(.*\)$'`
32
+    if expr "$link" : '/.*' > /dev/null; then
33
+        PRG="$link"
34
+    else
35
+        PRG=`dirname "$PRG"`"/$link"
36
+    fi
37
+done
38
+SAVED="`pwd`"
39
+cd "`dirname \"$PRG\"`/" >/dev/null
40
+APP_HOME="`pwd -P`"
41
+cd "$SAVED" >/dev/null
42
+
43
+APP_NAME="Gradle"
44
+APP_BASE_NAME=`basename "$0"`
45
+
46
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47
+DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48
+
49
+# Use the maximum available, or set MAX_FD != -1 to use that value.
50
+MAX_FD="maximum"
51
+
52
+warn () {
53
+    echo "$*"
54
+}
55
+
56
+die () {
57
+    echo
58
+    echo "$*"
59
+    echo
60
+    exit 1
61
+}
62
+
63
+# OS specific support (must be 'true' or 'false').
64
+cygwin=false
65
+msys=false
66
+darwin=false
67
+nonstop=false
68
+case "`uname`" in
69
+  CYGWIN* )
70
+    cygwin=true
71
+    ;;
72
+  Darwin* )
73
+    darwin=true
74
+    ;;
75
+  MSYS* | MINGW* )
76
+    msys=true
77
+    ;;
78
+  NONSTOP* )
79
+    nonstop=true
80
+    ;;
81
+esac
82
+
83
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84
+
85
+
86
+# Determine the Java command to use to start the JVM.
87
+if [ -n "$JAVA_HOME" ] ; then
88
+    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89
+        # IBM's JDK on AIX uses strange locations for the executables
90
+        JAVACMD="$JAVA_HOME/jre/sh/java"
91
+    else
92
+        JAVACMD="$JAVA_HOME/bin/java"
93
+    fi
94
+    if [ ! -x "$JAVACMD" ] ; then
95
+        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96
+
97
+Please set the JAVA_HOME variable in your environment to match the
98
+location of your Java installation."
99
+    fi
100
+else
101
+    JAVACMD="java"
102
+    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103
+
104
+Please set the JAVA_HOME variable in your environment to match the
105
+location of your Java installation."
106
+fi
107
+
108
+# Increase the maximum file descriptors if we can.
109
+if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110
+    MAX_FD_LIMIT=`ulimit -H -n`
111
+    if [ $? -eq 0 ] ; then
112
+        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113
+            MAX_FD="$MAX_FD_LIMIT"
114
+        fi
115
+        ulimit -n $MAX_FD
116
+        if [ $? -ne 0 ] ; then
117
+            warn "Could not set maximum file descriptor limit: $MAX_FD"
118
+        fi
119
+    else
120
+        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121
+    fi
122
+fi
123
+
124
+# For Darwin, add options to specify how the application appears in the dock
125
+if $darwin; then
126
+    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127
+fi
128
+
129
+# For Cygwin or MSYS, switch paths to Windows format before running java
130
+if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131
+    APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132
+    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133
+
134
+    JAVACMD=`cygpath --unix "$JAVACMD"`
135
+
136
+    # We build the pattern for arguments to be converted via cygpath
137
+    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138
+    SEP=""
139
+    for dir in $ROOTDIRSRAW ; do
140
+        ROOTDIRS="$ROOTDIRS$SEP$dir"
141
+        SEP="|"
142
+    done
143
+    OURCYGPATTERN="(^($ROOTDIRS))"
144
+    # Add a user-defined pattern to the cygpath arguments
145
+    if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146
+        OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147
+    fi
148
+    # Now convert the arguments - kludge to limit ourselves to /bin/sh
149
+    i=0
150
+    for arg in "$@" ; do
151
+        CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152
+        CHECK2=`echo "$arg"|egrep -c "^-"`                                 ### Determine if an option
153
+
154
+        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### Added a condition
155
+            eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156
+        else
157
+            eval `echo args$i`="\"$arg\""
158
+        fi
159
+        i=`expr $i + 1`
160
+    done
161
+    case $i in
162
+        0) set -- ;;
163
+        1) set -- "$args0" ;;
164
+        2) set -- "$args0" "$args1" ;;
165
+        3) set -- "$args0" "$args1" "$args2" ;;
166
+        4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167
+        5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168
+        6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169
+        7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170
+        8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171
+        9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172
+    esac
173
+fi
174
+
175
+# Escape application args
176
+save () {
177
+    for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178
+    echo " "
179
+}
180
+APP_ARGS=`save "$@"`
181
+
182
+# Collect all arguments for the java command, following the shell quoting and substitution rules
183
+eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184
+
185
+exec "$JAVACMD" "$@"

+ 89 - 0
gradlew.bat

@@ -0,0 +1,89 @@
1
+@rem
2
+@rem Copyright 2015 the original author or authors.
3
+@rem
4
+@rem Licensed under the Apache License, Version 2.0 (the "License");
5
+@rem you may not use this file except in compliance with the License.
6
+@rem You may obtain a copy of the License at
7
+@rem
8
+@rem      https://www.apache.org/licenses/LICENSE-2.0
9
+@rem
10
+@rem Unless required by applicable law or agreed to in writing, software
11
+@rem distributed under the License is distributed on an "AS IS" BASIS,
12
+@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+@rem See the License for the specific language governing permissions and
14
+@rem limitations under the License.
15
+@rem
16
+
17
+@if "%DEBUG%" == "" @echo off
18
+@rem ##########################################################################
19
+@rem
20
+@rem  Gradle startup script for Windows
21
+@rem
22
+@rem ##########################################################################
23
+
24
+@rem Set local scope for the variables with windows NT shell
25
+if "%OS%"=="Windows_NT" setlocal
26
+
27
+set DIRNAME=%~dp0
28
+if "%DIRNAME%" == "" set DIRNAME=.
29
+set APP_BASE_NAME=%~n0
30
+set APP_HOME=%DIRNAME%
31
+
32
+@rem Resolve any "." and ".." in APP_HOME to make it shorter.
33
+for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34
+
35
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36
+set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37
+
38
+@rem Find java.exe
39
+if defined JAVA_HOME goto findJavaFromJavaHome
40
+
41
+set JAVA_EXE=java.exe
42
+%JAVA_EXE% -version >NUL 2>&1
43
+if "%ERRORLEVEL%" == "0" goto execute
44
+
45
+echo.
46
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47
+echo.
48
+echo Please set the JAVA_HOME variable in your environment to match the
49
+echo location of your Java installation.
50
+
51
+goto fail
52
+
53
+:findJavaFromJavaHome
54
+set JAVA_HOME=%JAVA_HOME:"=%
55
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56
+
57
+if exist "%JAVA_EXE%" goto execute
58
+
59
+echo.
60
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61
+echo.
62
+echo Please set the JAVA_HOME variable in your environment to match the
63
+echo location of your Java installation.
64
+
65
+goto fail
66
+
67
+:execute
68
+@rem Setup the command line
69
+
70
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71
+
72
+
73
+@rem Execute Gradle
74
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75
+
76
+:end
77
+@rem End local scope for the variables with windows NT shell
78
+if "%ERRORLEVEL%"=="0" goto mainEnd
79
+
80
+:fail
81
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82
+rem the _cmd.exe /c_ return code!
83
+if  not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84
+exit /b 1
85
+
86
+:mainEnd
87
+if "%OS%"=="Windows_NT" endlocal
88
+
89
+:omega

BIN
libs/ProtocolStack_main.jar


+ 162 - 0
src/main/java/com/sainyu/beidou3/demo/SocketClient.java

@@ -0,0 +1,162 @@
1
+package com.sainyu.beidou3.demo;
2
+
3
+/**
4
+ * Created by stew on 16/8/29.
5
+ * mail: stewforani@gmail.com
6
+ * Socket收发器 通过Socket发送数据,并使用新线程监听Socket接收到的数据
7
+ */
8
+
9
+import java.io.DataInputStream;
10
+import java.io.DataOutputStream;
11
+import java.io.IOException;
12
+import java.net.InetAddress;
13
+import java.net.Socket;
14
+import java.util.Arrays;
15
+
16
+
17
+public abstract class SocketClient implements Runnable {
18
+
19
+    protected Socket socket;
20
+    protected InetAddress addr;
21
+    protected DataInputStream in;
22
+    protected DataOutputStream out;
23
+    private boolean runFlag;
24
+
25
+    /**
26
+     * 实例化
27
+     */
28
+    public SocketClient(String ip,int port) throws IOException {
29
+        this.socket = new Socket(ip,port);
30
+        this.addr = socket.getInetAddress();
31
+    }
32
+
33
+    /**
34
+     * 获取连接到的Socket地址
35
+     *
36
+     * @return InetAddress对象
37
+     */
38
+    public InetAddress getInetAddress() {
39
+        return addr;
40
+    }
41
+
42
+    /**
43
+     * 开启Socket收发
44
+     * <p>
45
+     * 如果开启失败,会断开连接并回调{@code onDisconnect()}
46
+     */
47
+    public void start() {
48
+        runFlag = true;
49
+        new Thread(this).start();
50
+    }
51
+
52
+    /**
53
+     * 断开连接(主动)
54
+     * <p>
55
+     * 连接断开后,会回调{@code onDisconnect()}
56
+     */
57
+    public void stop() {
58
+        runFlag = false;
59
+        try {
60
+            socket.shutdownInput();
61
+            in.close();
62
+        } catch (Exception e) {
63
+            e.printStackTrace();
64
+        }
65
+    }
66
+
67
+    /**
68
+     * 发送字符串
69
+     *
70
+     * @param s 字符串
71
+     * @return 发送成功返回true
72
+     */
73
+    public boolean send(byte s) {
74
+        if (out != null) {
75
+            try {
76
+                out.write(s);
77
+                out.flush();
78
+                return true;
79
+            } catch (Exception e) {
80
+                e.printStackTrace();
81
+            }
82
+        }
83
+        return false;
84
+    }
85
+
86
+    /**修改ssid
87
+     * @param s
88
+     * @return
89
+     */
90
+    public boolean send(byte[] s) {
91
+        if (out != null) {
92
+            try {
93
+                out.write(s);
94
+                out.flush();
95
+                return true;
96
+            } catch (Exception e) {
97
+                e.printStackTrace();
98
+            }
99
+        }
100
+        return false;
101
+    }
102
+
103
+    /**
104
+     * 监听Socket接收的数据(新线程中运行)
105
+     */
106
+    @Override
107
+    public void run() {
108
+        try {
109
+            in = new DataInputStream(this.socket.getInputStream());
110
+            out = new DataOutputStream(this.socket.getOutputStream());
111
+        } catch (IOException e) {
112
+            e.printStackTrace();
113
+            runFlag = false;
114
+        }
115
+        byte[] bytes;
116
+        while (runFlag) {
117
+            try {
118
+                byte[] readBuffer = new byte[512];
119
+                int bytesNum = in.read(readBuffer);
120
+                if (bytesNum > 0) {
121
+                    bytes = Arrays.copyOf(readBuffer,bytesNum);
122
+                    //System.out.println(new String(bytes,"gb2312"));
123
+                    this.onReceive(addr, bytes);
124
+                }
125
+            } catch (IOException e) {
126
+                // 连接被断开(被动)
127
+                runFlag = false;
128
+            }
129
+        }
130
+        // 断开连接
131
+        try {
132
+            in.close();
133
+            out.close();
134
+            socket.close();
135
+            in = null;
136
+            out = null;
137
+            socket = null;
138
+        } catch (IOException e) {
139
+            e.printStackTrace();
140
+        }
141
+        this.onDisconnect(addr);
142
+    }
143
+
144
+    /**
145
+     * 接收到数据
146
+     * <p>
147
+     * 注意:此回调是在新线程中执行的
148
+     *
149
+     * @param addr 连接到的Socket地址
150
+     * @param bytes    收到的字符数组
151
+     */
152
+    public abstract void onReceive(InetAddress addr, byte[] bytes);
153
+
154
+    /**
155
+     * 连接断开
156
+     * <p>
157
+     * 注意:此回调是在新线程中执行的
158
+     *
159
+     * @param addr 连接到的Socket地址
160
+     */
161
+    public abstract void onDisconnect(InetAddress addr);
162
+}

+ 455 - 0
src/main/java/com/sainyu/beidou3/demo/SocketFrame.java

@@ -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
+}