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();
|
}
|
}
|