GK309协议电子工牌数据模拟器+Socket数据发送 JAVA版-源码

2021/6/5 12:20:59

本文主要是介绍GK309协议电子工牌数据模拟器+Socket数据发送 JAVA版-源码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

GK309协议电子工牌数据模拟器+Socket数据发送 JAVA版-源码

    • 说明
    • 源码
    • EXE执行文件

说明

单JAVA文件,直接运行main方法即可。
只做了GK309的基础位置数据模拟:

  • 0x01 登录信息
  • 0x10 一般位置
  • 0x16 SOS报警
  • 0xB0 上下班打卡

以及Socket数据发送功能,方便开发测试。需要模拟其他数据,请自行改源码添加,下文附送源码。

在这里插入图片描述

在这里插入图片描述

源码

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GK309 {
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yy-MM-dd-HH-mm-ss");

    public static double pi = 3.1415926535897932384626;
    public static double a = 6378245.0;
    public static double ee = 0.00669342162296594323;

    private static final String REGX = "[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}";
    private static final String REGIP = "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}";

    private static int SERNUM = 0;

    private static Socket SOCKET = null;


    public static void getSocket(String ip,Integer port){
        try {
            SOCKET = new Socket(ip,port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * SOCKET发送数据
     * @param data 数据
     * @param isHex 是否是16进制数据
     */
    public static void sendInfo(String data, boolean isHex){
        try {
            OutputStream out = SOCKET.getOutputStream();
            if (isHex){
                byte[] bytes = hexStrToByteArray(data);
                out.write(bytes);
                out.flush();
            }else {
                out.write(data.getBytes());
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /** SOCKET接收数据 */
    public static String getSocketInfo(){
        String res = null;
        try {
            InputStream in = SOCKET.getInputStream();
            byte[] b = new byte[8096];
            int len = in.read(b);
            //res =  new String(b);
            res =  byte2Hex(b,len).toUpperCase();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return res;
    }

    /** 判断连接是否断开 */
    public static Boolean isServerClose(){
        try{
            SOCKET.sendUrgentData(0xFF);//发送1个字节的紧急数据,默认情况下,服务器端没有开启紧急数据处理,不影响正常通信
            return false;
        }catch(Exception se){
            return true;
        }
    }

    /** 将byte转为16进制 */
    private static String byte2Hex(byte[] bytes,int len) {
        StringBuilder stringBuffer = new StringBuilder();
        String temp = null;
        for (int i = 0; i < len; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                // 1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    /** 判空 */
    public static boolean isEmpty(String str){
        if (str == null || "".equals(str.trim())) return true;
        return false;
    }

    /** 十六进制转字节 */
    public static byte[] hexStrToByteArray(String hex) {
        int len = hex.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
            b[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character
                    .digit(hex.charAt(i + 1), 16));
        }
        return b;
    }

    /**
     * 火星坐标系(GCJ-02)转GPS84字符串(lng,lat)
     * */
    public static String[] gcj02To84Str(String Gcj02Lng,String Gcj02Lat) {
        double lon = Double.parseDouble(Gcj02Lng);
        double lat = Double.parseDouble(Gcj02Lat);
        double[] gps = transform(lat, lon);
        double lng0 = lon * 2 - gps[1];
        double lat0 = lat * 2 - gps[0];
        return new String[] {""+lng0,""+lat0};
    }

    public static double[] transform(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new double[] { lat, lon };
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return new double[] { mgLat, mgLon };
    }

    public static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    public static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
                * pi)) * 2.0 / 3.0;
        return ret;
    }

    /** 是否在国外 */
    public static boolean outOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }



    private static String checkLengthTo2(String s) {
        if (s.length() == 1) {
            s = "0" + s;
        }
        return s.toUpperCase();
    }
    private static String checkLengthTo4(String s) {
        if (s.length() == 1) {
            s = "000" + s;
        } else if (s.length() == 2) {
            s = "00" + s;
        } else if (s.length() == 3) {
            s = "0" + s;
        }
        return s;
    }
    /** 将字符串长度补足8位 */
    private static String checkLengthTo8(String s) {
        while (s.length() < 8) {
            s = "0" + s;
        }
        return s;
    }

    /** 将字符串长度补足16位 */
    private static String checkLengthTo16(String s) {
        while (s.length() < 16) {
            s = "0" + s;
        }
        return s;
    }


    static char crctab16[] ={
            0X0000, 0X1189, 0X2312, 0X329B, 0X4624, 0X57AD, 0X6536, 0X74BF,
            0X8C48, 0X9DC1, 0XAF5A, 0XBED3, 0XCA6C, 0XDBE5, 0XE97E, 0XF8F7,
            0X1081, 0X0108, 0X3393, 0X221A, 0X56A5, 0X472C, 0X75B7, 0X643E,
            0X9CC9, 0X8D40, 0XBFDB, 0XAE52, 0XDAED, 0XCB64, 0XF9FF, 0XE876,
            0X2102, 0X308B, 0X0210, 0X1399, 0X6726, 0X76AF, 0X4434, 0X55BD,
            0XAD4A, 0XBCC3, 0X8E58, 0X9FD1, 0XEB6E, 0XFAE7, 0XC87C, 0XD9F5,
            0X3183, 0X200A, 0X1291, 0X0318, 0X77A7, 0X662E, 0X54B5, 0X453C,
            0XBDCB, 0XAC42, 0X9ED9, 0X8F50, 0XFBEF, 0XEA66, 0XD8FD, 0XC974,
            0X4204, 0X538D, 0X6116, 0X709F, 0X0420, 0X15A9, 0X2732, 0X36BB,
            0XCE4C, 0XDFC5, 0XED5E, 0XFCD7, 0X8868, 0X99E1, 0XAB7A, 0XBAF3,
            0X5285, 0X430C, 0X7197, 0X601E, 0X14A1, 0X0528, 0X37B3, 0X263A,
            0XDECD, 0XCF44, 0XFDDF, 0XEC56, 0X98E9, 0X8960, 0XBBFB, 0XAA72,
            0X6306, 0X728F, 0X4014, 0X519D, 0X2522, 0X34AB, 0X0630, 0X17B9,
            0XEF4E, 0XFEC7, 0XCC5C, 0XDDD5, 0XA96A, 0XB8E3, 0X8A78, 0X9BF1,
            0X7387, 0X620E, 0X5095, 0X411C, 0X35A3, 0X242A, 0X16B1, 0X0738,
            0XFFCF, 0XEE46, 0XDCDD, 0XCD54, 0XB9EB, 0XA862, 0X9AF9, 0X8B70,
            0X8408, 0X9581, 0XA71A, 0XB693, 0XC22C, 0XD3A5, 0XE13E, 0XF0B7,
            0X0840, 0X19C9, 0X2B52, 0X3ADB, 0X4E64, 0X5FED, 0X6D76, 0X7CFF,
            0X9489, 0X8500, 0XB79B, 0XA612, 0XD2AD, 0XC324, 0XF1BF, 0XE036,
            0X18C1, 0X0948, 0X3BD3, 0X2A5A, 0X5EE5, 0X4F6C, 0X7DF7, 0X6C7E,
            0XA50A, 0XB483, 0X8618, 0X9791, 0XE32E, 0XF2A7, 0XC03C, 0XD1B5,
            0X2942, 0X38CB, 0X0A50, 0X1BD9, 0X6F66, 0X7EEF, 0X4C74, 0X5DFD,
            0XB58B, 0XA402, 0X9699, 0X8710, 0XF3AF, 0XE226, 0XD0BD, 0XC134,
            0X39C3, 0X284A, 0X1AD1, 0X0B58, 0X7FE7, 0X6E6E, 0X5CF5, 0X4D7C,
            0XC60C, 0XD785, 0XE51E, 0XF497, 0X8028, 0X91A1, 0XA33A, 0XB2B3,
            0X4A44, 0X5BCD, 0X6956, 0X78DF, 0X0C60, 0X1DE9, 0X2F72, 0X3EFB,
            0XD68D, 0XC704, 0XF59F, 0XE416, 0X90A9, 0X8120, 0XB3BB, 0XA232,
            0X5AC5, 0X4B4C, 0X79D7, 0X685E, 0X1CE1, 0X0D68, 0X3FF3, 0X2E7A,
            0XE70E, 0XF687, 0XC41C, 0XD595, 0XA12A, 0XB0A3, 0X8238, 0X93B1,
            0X6B46, 0X7ACF, 0X4854, 0X59DD, 0X2D62, 0X3CEB, 0X0E70, 0X1FF9,
            0XF78F, 0XE606, 0XD49D, 0XC514, 0XB1AB, 0XA022, 0X92B9, 0X8330,
            0X7BC7, 0X6A4E, 0X58D5, 0X495C, 0X3DE3, 0X2C6A, 0X1EF1, 0X0F78,
    };


    /** 十进制数转十六进制数 */
    public static String intToHex(int n) {
        StringBuffer s = new StringBuffer();
        String a;
        char[] b = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        while (n != 0) {
            s = s.append(b[n % 16]);
            n = n / 16;
        }
        a = s.reverse().toString();
        return a;
    }

    /** 计算给定长度数据的 16 位 CRC */
    public static int getCrc16(byte[] pData, int nLength){
        int i=0;
        int fcs = 0xffff; // 初始化
        while(nLength>0){
            fcs = (fcs >> 8) ^ crctab16[(fcs ^ pData[i]) & 0xff];
            nLength--;
            i++;
        }
        int result = (~fcs & 0xffff);
        return result;
    }

    /** string 转byte[] {0x11,0x12} */
    public static byte[] hexString2Bytes(String s) {
        byte[] bytes;
        bytes = new byte[s.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            //十六进制转为十进制
            bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),16);
        }
        return bytes;
    }


    /** GK309获取校验码 */
    public static String getCrcCode(String str){
        byte[] data = hexString2Bytes(str);
        int crc16 = getCrc16(data, data.length);
        String res = intToHex(crc16);
        while (res.length() < 4){
            res = "0" + res;
        }
        return res;
    }

    /**
     * 获取校验码并整合内容
     */
    private static String concatMsg(String content) {
        // 获取校验码
        String code = getCrcCode(content);
        String msg = "7878" + content + code + "0D0A";
        return msg;
    }

    /** 转换GPS时间 */
    public static String covGpsTime(String time){
        StringBuilder res = new StringBuilder("");
        String[] split = time.split("-");
        for (String s : split) {
            String s1 = checkLengthTo2(intToHex(Integer.parseInt(s)));
            res.append(s1);
        }
        return res.toString();
    }

    /**
     * 加法运算保留6位小数
     */
    public static double addValue6(double num1,double num2){
        BigDecimal one = new BigDecimal(num1);
        BigDecimal two = new BigDecimal(num2);
        BigDecimal add = one.add(two);
        double doubleValue = add.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
        return doubleValue;
    }

    /**
     * 乘法法运算保留整数
     */
    public static int getIntValue(double num1,double num2){
        BigDecimal one = new BigDecimal(num1);
        BigDecimal two = new BigDecimal(num2);
        BigDecimal divide = one.multiply(two);
        int intValue = divide.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
        return intValue;
    }

    /** 转换坐标 */
    public static String covCoor(String coo){
        String[] split = coo.split("\\.");
        int z = Integer.parseInt(split[0]);
        double x = Double.parseDouble("0." + split[1]) * 60;
        double v = addValue6(z * 60, x);
        int intValue = getIntValue(v, 30000);
        String s = intToHex(intValue);
        return checkLengthTo8(s);
    }


    /**
     * 创建登录信息
     * @param deviceId IMEI号
     * @param type 类型识别码(例:GK301 LBS 版本)
     */
    public static String makeLoginInfo(String deviceId, String type){

        String len = "0F";//长度=协议号+信息内容+信息序列号+错误校验
        String agr = "01";//协议号 01登录信息
        String imei = checkLengthTo16(deviceId);
        SERNUM++;
        String seNum = checkLengthTo4(intToHex((SERNUM)));//流水号
        String content = len+agr+imei+type+seNum;
        String res = concatMsg(content);

        return res;
    }


    /** 生成位置信息 */
    public static String makePosInfo(String gpsTime,String lng0,String lat0,String speed0,int dir){

        String len = "19";//长度=协议号+信息内容+信息序列号+错误校验
        String agr = "10";//协议号 10一般位置
        String time = covGpsTime(gpsTime);
        String lat = covCoor(lat0);
        String lng = covCoor(lng0);
        String speed = checkLengthTo2(intToHex(Integer.parseInt(speed0)));
        //String sta = "1400";//状态
        int a = dir / 256;
        int b = dir % 256;
        String s1 = "000101"+checkLengthTo2(Integer.toBinaryString(a));
        Integer i1 = Integer.valueOf(s1, 2);
        String sa = checkLengthTo2(Integer.toHexString(i1));
        String s2 = checkLengthTo2(Integer.toHexString(b));
        String sta = sa+s2;

        SERNUM++;
        String seNum = checkLengthTo4(intToHex((SERNUM)));//流水号
        String content = len+agr+time+"C5"+lat+lng+speed+sta+"0001"+seNum;
        String res = concatMsg(content);

        return res;
    }


    /** 生成SOS信息 */
    public static String makeSosInfo(String gpsTime,String lng0,String lat0,String speed0,int dir){

        String len = "25";//长度=协议号+信息内容+信息序列号+错误校验
        String agr = "16";//协议号 16报警
        String time = covGpsTime(gpsTime);
        String lat = covCoor(lat0);
        String lng = covCoor(lng0);
        String speed = checkLengthTo2(intToHex(Integer.parseInt(speed0)));
        //String sta = "1400";//状态
        int a = dir / 256;
        int b = dir % 256;
        String s1 = "000101"+checkLengthTo2(Integer.toBinaryString(a));
        Integer i1 = Integer.valueOf(s1, 2);
        String sa = checkLengthTo2(Integer.toHexString(i1));
        String s2 = checkLengthTo2(Integer.toHexString(b));
        String sta = sa+s2;

        String lbs = "0901CC002866000EEE";//LBS(假数据)
        String sos = "01"+"1000"+"00";//中间1000表示SOS
        String sosHex = Integer.toHexString(Integer.valueOf(sos, 2));
        String sk = sosHex+"0604";//状态
        String kz = "0001";//扩展

        SERNUM++;
        String seNum = checkLengthTo4(intToHex((SERNUM)));//流水号

        String content = len+agr+time+"C5"+lat+lng+speed+sta+lbs+sk+kz+seNum;
        String res = concatMsg(content);

        return res;
    }


    /**
     * 生成上下班打卡信息
     * type 1上班 2下班
     */
    public static String makeClockInfo(String gpsTime,String lng0,String lat0,String speed0,int dir,int type){

        String len = "86";//长度=协议号+信息内容+信息序列号+错误校验
        String agr = "B0";//协议号 16报警
        String time = covGpsTime(gpsTime);
        String pos = "01";//定位
        String lat = covCoor(lat0);
        String lng = covCoor(lng0);
        String speed = checkLengthTo2(intToHex(Integer.parseInt(speed0)));
        //String sta = "1400";//状态
        int a = dir / 256;
        int b = dir % 256;
        String s1 = "000101"+checkLengthTo2(Integer.toBinaryString(a));
        Integer i1 = Integer.valueOf(s1, 2);
        String sa = checkLengthTo2(Integer.toHexString(i1));
        String s2 = checkLengthTo2(Integer.toHexString(b));
        String sta = sa+s2;

        String clock = null;//0001:上班打卡 0010:下班打卡
        if (type == 1){
            clock = "01"+"0001"+"00";
        }else {
            clock = "01"+"0010"+"00";
        }
        String clockHex = Integer.toHexString(Integer.valueOf(clock, 2));
        String sk = clockHex+"0604";//状态
        String kz = "0001";//扩展
        String ot = "01CC002502002A59310000000000000000000000000000000000000000000000000000000000000000000000000000000000"+
                "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";//其他

        SERNUM++;
        String seNum = checkLengthTo4(intToHex((SERNUM)));//流水号

        String content = len+agr+time+pos+"D702"+"C5"+lat+lng+speed+sta+sk+kz+ot+seNum;
        String res = concatMsg(content);

        return res;
    }


    /** 面板内容 */
    private static void placeComponents(JPanel panel) {

        /* 布局部分我们这边不多做介绍
         * 这边设置布局为 null
         */
        panel.setLayout(null);

        // 设备ID标题
        JLabel deviceLab = new JLabel("设备ID:");
        /* 这个方法定义了组件的位置。
         * setBounds(x, y, width, height)
         * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
         */
        deviceLab.setBounds(10,20,200,28);
        deviceLab.setFont(new Font("微软雅黑", Font.PLAIN,18));
        panel.add(deviceLab);

        // 输入设备ID
        final JTextField deviceStr = new JTextField(20);
        deviceStr.setBounds(100,20,200,28);
        deviceStr.setText("121212121212121");
        deviceStr.setFont(new Font("微软雅黑", Font.PLAIN,16));
        panel.add(deviceStr);

        // 类型识别码
        JLabel typeTitle = new JLabel("识别码:");
        typeTitle.setBounds(340,20,100,28);
        typeTitle.setFont(new Font("微软雅黑", Font.PLAIN,18));
        panel.add(typeTitle);

        // 类型识别码
        final JTextField devType = new JTextField(20);
        devType.setBounds(430,20,100,28);
        devType.setText("1004");
        devType.setFont(new Font("微软雅黑", Font.PLAIN,16));
        panel.add(devType);


        // GPS时间标题
        JLabel gpsLab = new JLabel("GPS时间:");
        gpsLab.setBounds(10,60,200,28);
        gpsLab.setFont(new Font("微软雅黑", Font.PLAIN,18));
        panel.add(gpsLab);

        // 输入GPS时间
        final JTextField gpsStr = new JTextField(20);
        gpsStr.setBounds(100,60,230,28);
        gpsStr.setText(SDF.format(new Date()));
        gpsStr.setFont(new Font("微软雅黑", Font.PLAIN,16));
        panel.add(gpsStr);

        // 刷新
        JButton fluButton = new JButton("f");
        fluButton.setBounds(360, 60, 40, 28);
        fluButton.setFont(new Font("微软雅黑", Font.BOLD,16));
        fluButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gpsStr.setText(SDF.format(new Date()));
            }
        });
        panel.add(fluButton);


        // 单选:SOS求救
        JRadioButton jr1 = new JRadioButton("SOS求救报警");
        jr1.setBounds(430,60,140,28);
        jr1.setFont(new Font("微软雅黑", Font.BOLD,16));
        jr1.setForeground(Color.red);
        JRadioButton jr2 = new JRadioButton("上班打卡");
        jr2.setBounds(430,100,140,28);
        jr2.setFont(new Font("微软雅黑", Font.BOLD,16));
        jr2.setForeground(Color.MAGENTA);
        JRadioButton jr3 = new JRadioButton("下班打卡");
        jr3.setBounds(430,140,140,28);
        jr3.setFont(new Font("微软雅黑", Font.BOLD,16));
        jr3.setForeground(Color.BLUE);
        JRadioButton jr4 = new JRadioButton("基本位置信息");
        jr4.setBounds(430,180,140,28);
        jr4.setFont(new Font("微软雅黑", Font.BOLD,16));
        jr4.setSelected(true);
        ButtonGroup btg = new ButtonGroup();
        btg.add(jr1);
        btg.add(jr2);
        btg.add(jr3);
        btg.add(jr4);
        panel.add(jr1);
        panel.add(jr2);
        panel.add(jr3);
        panel.add(jr4);


        // 经度标题
        JLabel lngLab = new JLabel("经度:");
        lngLab.setBounds(10,100,200,28);
        lngLab.setFont(new Font("微软雅黑", Font.PLAIN,18));
        panel.add(lngLab);

        // 输入经度
        final JTextField lngStr = new JTextField(20);
        lngStr.setBounds(100,100,300,28);
        lngStr.setText("113.334991");
        lngStr.setFont(new Font("微软雅黑", Font.PLAIN,16));
        panel.add(lngStr);


        // 纬度标题
        JLabel latLab = new JLabel("纬度:");
        latLab.setBounds(10,140,200,28);
        latLab.setFont(new Font("微软雅黑", Font.PLAIN,18));
        panel.add(latLab);

        // 输入纬度
        final JTextField latStr = new JTextField(20);
        latStr.setBounds(100,140,300,28);
        latStr.setText("23.132905");
        latStr.setFont(new Font("微软雅黑", Font.PLAIN,16));
        panel.add(latStr);


        // 速度标题
        JLabel speedLab = new JLabel("速度:");
        speedLab.setBounds(10,180,200,28);
        speedLab.setFont(new Font("微软雅黑", Font.PLAIN,18));
        panel.add(speedLab);

        // 输入速度
        final JTextField speedStr = new JTextField(20);
        speedStr.setBounds(100,180,100,28);
        speedStr.setText("10");
        speedStr.setFont(new Font("微软雅黑", Font.PLAIN,16));
        panel.add(speedStr);


        // 方向标题
        JLabel dirLab = new JLabel("方向:");
        dirLab.setBounds(230,180,100,28);
        dirLab.setFont(new Font("微软雅黑", Font.PLAIN,18));
        panel.add(dirLab);

        // 输入方向
        final JTextField dirStr = new JTextField(20);
        dirStr.setBounds(300,180,100,28);
        dirStr.setText("90");
        dirStr.setFont(new Font("微软雅黑", Font.PLAIN,16));
        panel.add(dirStr);



        /* 输出结果 */
        final JTextArea  resStr = new JTextArea();
        resStr.setBounds(10,280,585,100);
        resStr.setFont(new Font("微软雅黑", Font.PLAIN,16));
        resStr.setLineWrap(true);//激活自动换行功能
        resStr.setWrapStyleWord(true);//激活断行不断字功能
        panel.add(resStr);


        // GCJ02转换84坐标
        JLabel coorCheck = new JLabel("GCJ02转换84坐标");
        coorCheck.setBounds(10,230,140,28);
        coorCheck.setFont(new Font("微软雅黑", Font.PLAIN,16));
        coorCheck.setForeground(Color.red);
        panel.add(coorCheck);

        final JCheckBox coorbox = new JCheckBox();
        coorbox.setBounds(145,230,30,30);
        coorbox.setSize(30,30);
        panel.add(coorbox);


        // 生成登录信息
        JButton covButton = new JButton("登录信息");
        covButton.setBounds(330, 230, 115, 30);
        covButton.setFont(new Font("微软雅黑", Font.BOLD,20));
        covButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String imei = deviceStr.getText();
                if (isEmpty(imei)) {
                    JOptionPane.showMessageDialog(null, "设备号不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String type = devType.getText();
                if (isEmpty(type)) {
                    JOptionPane.showMessageDialog(null, "类型识别码(4位)不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String res = makeLoginInfo(imei,type);
                resStr.setText(res);
            }
        });
        panel.add(covButton);


        // 生成位置信息
        JButton createButton = new JButton("位置信息");
        createButton.setBounds(465, 230, 115, 30);
        createButton.setFont(new Font("微软雅黑", Font.BOLD,20));
        createButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String gpsTime = gpsStr.getText();
                if (isEmpty(gpsTime)) {
                    JOptionPane.showMessageDialog(null, "GPS时间不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (!gpsTime.matches(REGX)) {
                    JOptionPane.showMessageDialog(null, "GPS时间格式错误", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String lng = lngStr.getText();
                if (isEmpty(lng)) {
                    JOptionPane.showMessageDialog(null, "经度不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String lat = latStr.getText();
                if (isEmpty(lat)) {
                    JOptionPane.showMessageDialog(null, "纬度不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String speed = speedStr.getText();
                if (isEmpty(speed)) {
                    JOptionPane.showMessageDialog(null, "速度不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String dir = dirStr.getText();
                if (isEmpty(dir)) {
                    JOptionPane.showMessageDialog(null, "方向不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                boolean gcj = coorbox.isSelected();
                if (gcj){
                    String[] arr = gcj02To84Str(lng, lat);
                    lng = arr[0];
                    lat = arr[1];
                }

                String res = null;
                if (jr4.isSelected()){//基本位置信息
                    res = makePosInfo(gpsTime, lng, lat, speed, Integer.parseInt(dir.trim()));
                }
                if (jr1.isSelected()){//SOS信息
                    res = makeSosInfo(gpsTime, lng, lat, speed, Integer.parseInt(dir.trim()));
                }
                if (jr2.isSelected()){//上班打卡
                    res = makeClockInfo(gpsTime, lng, lat, speed, Integer.parseInt(dir.trim()),1);
                }
                if (jr3.isSelected()){//下班打卡
                    res = makeClockInfo(gpsTime, lng, lat, speed, Integer.parseInt(dir.trim()),2);
                }

                resStr.setText(res);
            }
        });
        panel.add(createButton);




        // IP标题
        JLabel ip = new JLabel("IP:");
        ip.setBounds(10,400,30,28);
        ip.setFont(new Font("微软雅黑", Font.PLAIN,18));
        panel.add(ip);

        // 输入IP
        final JTextField ipInfo = new JTextField(20);
        ipInfo.setBounds(40,400,150,28);
        ipInfo.setText("139.159.231.237");
        //ipInfo.setText("127.0.0.1");
        ipInfo.setFont(new Font("微软雅黑", Font.PLAIN,16));
        panel.add(ipInfo);

        // 端口标题
        JLabel port = new JLabel("端口:");
        port.setBounds(200,400,40,28);
        port.setFont(new Font("微软雅黑", Font.PLAIN,18));
        panel.add(port);

        // 输入端口
        final JTextField portInfo = new JTextField(20);
        portInfo.setBounds(250,400,60,28);
        portInfo.setText("9200");
        portInfo.setFont(new Font("微软雅黑", Font.PLAIN,16));
        panel.add(portInfo);

        // 连接状态
        final JLabel conStatus = new JLabel("未连接");
        conStatus.setBounds(330,400,60,28);
        conStatus.setFont(new Font("微软雅黑", Font.PLAIN,16));
        conStatus.setForeground(Color.red);
        panel.add(conStatus);


        // 连接按钮
        JButton connButton = new JButton("连接");
        connButton.setBounds(400, 400, 80, 30);
        connButton.setFont(new Font("微软雅黑", Font.BOLD,20));
        connButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String ipStr = ipInfo.getText();
                if (isEmpty(ipStr)) {
                    JOptionPane.showMessageDialog(null, "IP不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (!ipStr.matches(REGIP)) {
                    JOptionPane.showMessageDialog(null, "IP格式错误", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String portStr = portInfo.getText();
                if (isEmpty(portStr)) {
                    JOptionPane.showMessageDialog(null, "端口不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                getSocket(ipStr,Integer.parseInt(portStr));

                if (SOCKET != null){
                    conStatus.setText("已连接");
                    conStatus.setForeground(Color.GREEN);
                    System.out.println(SOCKET);
                }

            }
        });
        panel.add(connButton);

        // 断开按钮
        JButton desButton = new JButton("断开");
        desButton.setBounds(500, 400, 80, 30);
        desButton.setFont(new Font("微软雅黑", Font.BOLD,20));
        desButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    SOCKET.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                SOCKET = null;
                conStatus.setText("未连接");
                conStatus.setForeground(Color.RED);

            }
        });
        panel.add(desButton);


        /* 发送结果 */
        final JTextArea  sendStr = new JTextArea();
        sendStr.setBounds(10,440,470,80);
        sendStr.setLineWrap(true);//激活自动换行功能
        sendStr.setWrapStyleWord(true);//激活断行不断字功能
        panel.add(sendStr);


        // 发送按钮
        JButton senButton = new JButton("发送");
        senButton.setBounds(500, 440, 80, 80);
        senButton.setFont(new Font("微软雅黑", Font.BOLD,20));
        senButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (SOCKET == null || isServerClose()) {
                    SOCKET = null;
                    conStatus.setText("未连接");
                    conStatus.setForeground(Color.RED);
                    return;
                }

                String msg = resStr.getText();
                if (SOCKET != null && !isEmpty(msg)){
                    sendInfo(msg,true);
                    sendStr.setText("数据发送成功!"+"\n");
                    //sendStr.append(msg+"\n");

                    String res = getSocketInfo();
                    if (!isEmpty(res)){
                        sendStr.append("服务器响应:");
                        sendStr.append(res+"\n");
                    }
                }

            }
        });
        panel.add(senButton);


    }

    /** 创建 JFrame 实例 */
    public static void createJFrame(){
        // 创建 JFrame 实例
        JFrame frame = new JFrame("GK309数据生成器  -  by:MichLee");
        // Setting the width and height of frame
        frame.setSize(620, 580);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);//在屏幕中居中显示

        /* 创建面板,这个类似于 HTML 的 div 标签
         * 我们可以创建多个面板并在 JFrame 中指定位置
         * 面板中我们可以添加文本字段,按钮及其他组件。
         */
        JPanel panel = new JPanel();
        // 添加面板
        frame.add(panel);
        /*
         * 调用用户定义的方法并添加组件到面板
         */
        placeComponents(panel);

        // 设置界面可见
        frame.setVisible(true);
    }


    public static void main(String[] args) {
        createJFrame();

    }



}



EXE执行文件

说明:EXE执行文件是把JAVA打包,再用exe4j转换的执行文件。运行环境需要电脑配置了JDK环境,如果没有JDK环境,把JDK的JRE目录复制到EXE文件同目录也可以运行。

下载地址: https://mich.lanzoui.com/igZZRpt7xva



这篇关于GK309协议电子工牌数据模拟器+Socket数据发送 JAVA版-源码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程