| | |
| | | return String.format("%4s", HexString).replace(' ', '0'); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // System.out.println(HexUtil.hexToAscii("3c5354413e48656c6c6f20576f726c64217c5468697320697320746865206669727374207369676e616c2066726f6d20646576696365213c454f463e")); |
| | | // System.out.println(HexUtil.asciiToHex("<STA>Hello World!|This is the first signal from device!<EOF>")); |
| | | // |
| | | // System.out.println(String.format("%4S", HexUtil.intToHex(55)).replace(' ', '0')); |
| | | // System.out.println(HexUtil.hexToInt("00d2")); |
| | | // |
| | | // System.out.println(HexUtil.formatHex("3c5354413e")); |
| | | String message = "Hello World!|This is the first signal from device!"; |
| | | int length = message.length() * 2 + 10; // 长度包括结尾的<EOF>,一个字符在信号中由两个字节表示。 |
| | | |
| | | String command = HexUtil.asciiToHex("<STA>"); // 添加开始标识 |
| | | |
| | | command += (String.format("%4s", HexUtil.intToHex(length)).replace(' ', '0')); // 添加长度标识 |
| | | command += "01"; // 添加设备类型 0x01表示切割机,0x02表示钢化炉 |
| | | command += "0001"; // 添加设备型号,0x0001表示基本款,0x0002表示中级款 |
| | | command += "0000"; // 功能号,0x0000是下位机主动发给上位机;0x0001是上位机修改下位机RTC |
| | | command += "00"; // 加密方式,0x00表示不加密 |
| | | |
| | | Calendar c = Calendar.getInstance(); |
| | | |
| | | // 添加时间,并将时间转换为16进制表示 |
| | | command += (String.format("%4s", HexUtil.intToHex(c.get(Calendar.YEAR))).replace(' ', '0')); |
| | | command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.MONTH) + 1)).replace(' ', '0')); |
| | | command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.DAY_OF_MONTH))).replace(' ', '0')); |
| | | command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.HOUR_OF_DAY))).replace(' ', '0')); |
| | | command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.MINUTE))).replace(' ', '0')); |
| | | command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.SECOND))).replace(' ', '0')); |
| | | |
| | | command += ("00000001"); // 唯一标识,使用序列号 |
| | | |
| | | command += (HexUtil.asciiToHex(message)); // 添加主要信息内容 |
| | | |
| | | command += (HexUtil.asciiToHex("<EOF>")); // 添加结尾标识 |
| | | |
| | | System.out.println(command); |
| | | |
| | | System.out.println(String.format("%4s", "1")); |
| | | |
| | | Date time = new Date(); |
| | | System.out.println(new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(time)); |
| | | |
| | | String timeHex = timeToHex(time); |
| | | System.out.println(timeHex); |
| | | System.out.println(hexToTime(timeHex)); |
| | | |
| | | System.out.println("3c5354413e001a00000000000000000000000000010007e0021500152800000000000000005041001d017c017c01017c3c454f463e".length()); |
| | | |
| | | System.out.println(hexToBinary("0c0a")); |
| | | |
| | | System.out.println(Integer.MAX_VALUE); |
| | | } |
| | | |
| | | public static String hexTo2Binary(String hexString) { |
| | | String binaryString = Integer.toBinaryString(hexToInt(hexString)); |
| | | return String.format("%8s", binaryString).replace(' ', '0'); |