13693261870
2025-07-02 6708810c4de34dfb9513061432d656f91d56ee3a
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.ruoyi.fuzhou.utils.waterdept;
 
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.fuzhou.utils.hj1239.StringToHexUtil;
import org.apache.ibatis.executor.ExecutorException;
 
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
/**
 * 水深数据接入
 */
public class ModbusWaterDeptUtils {
 
    public static void main(String[] args) {
        String value = getDept("0.0.0.0", 8320);
        System.out.println(value);
    }
    /**
     * @param ip   ip
     * @param port 端口
     */
    public static String getDept(String ip, int port) {
        // 服务端配置
        String SERVER_IP = ip;// 监听所有网络接口
        int SERVER_PORT = port;// 端口号
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(SERVER_PORT);
            System.out.println("🛡️ 服务端已启动,正在监听 " + SERVER_IP + ":" + SERVER_PORT + "...");
            serverSocket.setSoTimeout(8000);
            Socket clientSocket = serverSocket.accept();
            System.out.println("🔌 客户端已连接: " + clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort());
            try (InputStream inputStream = clientSocket.getInputStream()) {
                // 接收设备响应
                while (true) {
                    byte[] response = new byte[1024];
                    int bytesRead = inputStream.read(response);
                    if (bytesRead == -1) {
                        System.out.println("❌ 未收到设备响应");
                        return null;
                    }
                    // 打印响应数据
                    byte[] responseData = new byte[bytesRead];
                    System.arraycopy(response, 0, responseData, 0, bytesRead);
                    String resultData = bytesToHex(responseData);
                    System.out.println("📥 收到设备响应: " + StringToHexUtil.convertHexToString(resultData));
                }
                //return StringToHex.convertHexToString(resultData);
            } catch (IOException e) {
                System.out.println("⚠️ 客户端异常断开: " + e.getMessage());
            } finally {
                clientSocket.close();
            }
        } catch (IOException e) {
            if (serverSocket != null) {
                try {
                    System.out.println("设备不在线" + e.getMessage());
                    serverSocket.close();
                } catch (IOException ex) {
                    System.out.println("❌ 服务端关闭失败: " + ex.getMessage());
                }
            } else {
                System.out.println("❌ 服务端启动失败: " + e.getMessage());
            }
        }
        return null;
    }
 
    /**
     * @param ip   ip
     * @param port 端口
     */
    public static String getWaterDept(String ip, int port) {
        // 服务端配置
        String SERVER_IP = ip;// 监听所有网络接口
        int SERVER_PORT = port;// 端口号
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(SERVER_PORT);
            System.out.println("🛡️ 服务端已启动,水深正在监听 " + SERVER_IP + ":" + SERVER_PORT + "...");
            serverSocket.setSoTimeout(4000);
            Socket clientSocket = serverSocket.accept();
            System.out.println("🔌 客户端已连接: " + clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort());
            try (InputStream inputStream = clientSocket.getInputStream()) {
                // 接收设备响应
                    byte[] response = new byte[1024];
                    int bytesRead = inputStream.read(response);
                    if (bytesRead == -1) {
                        System.out.println("❌ 未收到设备响应");
                        return null;
                    }
                    // 打印响应数据
                    byte[] responseData = new byte[bytesRead];
                    System.arraycopy(response, 0, responseData, 0, bytesRead);
                    String resultData = bytesToHex(responseData);
                return resultData;
            } catch (IOException e) {
                System.out.println("⚠️ 客户端异常断开: " + e.getMessage());
            } finally {
                clientSocket.close();
            }
        } catch (IOException e) {
            if (serverSocket != null) {
                try {
                    System.out.println("设备不在线" + e.getMessage());
                    serverSocket.close();
                    throw new ServiceException("设备不在线");
                } catch (IOException ex) {
                    System.out.println("❌ 服务端关闭失败: " + ex.getMessage());
                }
            } else {
                System.out.println("❌ 服务端启动失败: " + e.getMessage());
            }
        }
        return null;
    }
    // 将字节数组转换为十六进制字符串
    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }
}