张世豪
6 天以前 9d171a3c3a57ea54454d7e9d64dec213aa885a2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package chuankou;
 
import java.nio.charset.StandardCharsets;
 
/**
 * 串口发送入口,统一封装发送流程便于跨模块调用。
 */
public final class sendmessage {
    private static final SerialPortService DEFAULT_SERVICE = new SerialPortService();
    private static volatile SerialPortService activeService = DEFAULT_SERVICE;
 
    private sendmessage() {
        // utility class
    }
 
    /**
     * 发送原始字节数据。
     *
     * @param data 待发送字节
     * @return true 表示发送成功
     */
    public static boolean send(byte[] data) {
        if (data == null || data.length == 0) {
            return false;
        }
        SerialPortService service = getActiveService();
        if (service == null || !service.isOpen()) {
            return false;
        }
        return service.send(data);
    }
 
    /**
     * 发送十六进制字符串(允许包含空格)。
     */
    public static boolean sendHex(String hexString) {
        byte[] data = hexToBytes(hexString);
        return data != null && send(data);
    }
 
    /**
     * 发送 UTF-8 文本。
     */
    public static boolean sendText(String text) {
        if (text == null || text.isEmpty()) {
            return false;
        }
        return send(text.getBytes(StandardCharsets.UTF_8));
    }
 
    /**
     * 将外部打开的串口实例注入发送器,避免重复打开。
     */
    public static void attachService(SerialPortService serialPortService) {
        if (serialPortService == null) {
            return;
        }
        synchronized (sendmessage.class) {
            // 这里假设调用方提供的 service 已经成功打开串口
            if (serialPortService.isOpen()) {
                activeService = serialPortService;
            }
        }
    }
 
    /**
     * 获取当前使用的串口服务。
     */
    public static SerialPortService getActiveService() {
        SerialPortService service = activeService;
        return service != null ? service : DEFAULT_SERVICE;
    }
 
    /**
     * 通过活动串口服务发送数据(保证引用同步)。
     */
    public static boolean sendViaActive(byte[] data) {
        SerialPortService service = getActiveService();
        if (service == null || !service.isOpen()) {
            return false;
        }
        return service.send(data);
    }
 
    private static byte[] hexToBytes(String hexString) {
        if (hexString == null) {
            return null;
        }
        String normalized = hexString.replaceAll("\\s+", "").toUpperCase();
        if (normalized.length() == 0 || normalized.length() % 2 != 0) {
            return null;
        }
        byte[] result = new byte[normalized.length() / 2];
        for (int i = 0; i < normalized.length(); i += 2) {
            int high = Character.digit(normalized.charAt(i), 16);
            int low = Character.digit(normalized.charAt(i + 1), 16);
            if (high < 0 || low < 0) {
                return null;
            }
            result[i / 2] = (byte) ((high << 4) + low);
        }
        return result;
    }
 
}