小张开发日记02

18 阅读2分钟

串口通讯使用java开发的工具类,由于小弟不会写C,就不用JNA了,直接java干。

private static class tool{
    public static int Time2snd(String dateTimeString){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串解析为 LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatter);
        // 将 LocalDateTime 转换为 ZonedDateTime(指定时区)
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        // 将 ZonedDateTime 转换为 Instant(时间戳)
        Instant instant = zonedDateTime.toInstant();
        // 获取时间戳(秒)
        long timestampInSeconds = instant.getEpochSecond();
        return (int)timestampInSeconds + 28800;
    }
    public static String snd2Time(byte[] hex, int oft){
        long snds = ByteToInt32(hex, oft) - 28800;
        long timestamp = snds * 1000;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(timestamp);
        String formattedDate = sdf.format(date);
        return formattedDate;
    }
    public static String snd2TimeNoDay(byte[] hex, int oft) {
        int snds = ByteToInt32(hex, oft);
        if (snds < 0) snds = 0;
        int h = snds / 3600;
        int m = (snds - (h * 3600)) / 60;
        int s = snds % 60;
        String strtime = String.format("%02d:%02d",h,m);
        return strtime;
    }
    public static int Time2sndNoDay(String timestr) {
        String[] ary = timestr.split(":");
        if (ary.length != 2)
        {
            return -1;
        }
        int h = Integer.parseInt(ary[0]);
        int m = Integer.parseInt(ary[1]);
        return h * 3600 + m * 60;
    }
    public static void Int32ToByte(int d, byte[] hex, int oft) {
        hex[oft+3] = (byte) (d & 0xFF);         // 最低字节
        hex[oft+2] = (byte) ((d >> 8) & 0xFF);  // 次低字节
        hex[oft+1] = (byte) ((d >> 16) & 0xFF); // 次高字节
        hex[oft+0] = (byte) ((d >> 24) & 0xFF); // 最高字节
    }
    public static int ByteToInt32( byte[] hex, int oft){
        return  (hex[oft+3] & 0xFF) |
                ((hex[oft+2] & 0xFF) << 8) |
                ((hex[oft+1] & 0xFF) << 16) |
                ((hex[oft+0] & 0xFF) << 24);
    }
    public static byte[] chkcrcn485(byte[] buf, int len){
        int i, j = 0;
        int crc = 0xFFFF;
        byte[] buffer = new byte[len + 2];
        while (len-- > 0){
            buffer[j] = buf[j];
            crc ^= (buf[j++] & 0xFF);
            for (i = 0; i < 8; i++){
                if ((crc & 0x0001) != 0)
                    crc = (crc >> 1) ^ 0xa001;
                else
                    crc = crc >> 1;
            }
        }
        buffer[j++] = (byte)(crc & 0xFF);
        buffer[j++] = (byte)((crc >> 8) & 0xFF);
        return buffer;
    }
    public static String HexToStr(byte[] pHex ){
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < pHex.length; i++) {
            if (i % 4 == 0 && i > 0){
                hexString.append(' ');
            }
            String hex = Integer.toHexString(0xff & pHex[i]);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
    public static byte[] StrToHex(String hexString){
        hexString = hexString.replaceAll(" ","");
        if (hexString.length() % 2 != 0) {
            hexString = "0" + hexString;
        }
        // 初始化 byte[] 长度
        int len = hexString.length();
        byte[] byteArray = new byte[len / 2];
        // 每两个字符转换为一个字节
        for (int i = 0; i < len; i += 2) {
            byteArray[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)  + Character.digit(hexString.charAt(i + 1), 16));
        }
        return byteArray;
    }
}