gengby
6 months ago
9 changed files with 325 additions and 57 deletions
@ -0,0 +1,38 @@ |
|||||
|
package org.nl.acs.device_driver.shangdianke; |
||||
|
|
||||
|
import cn.hutool.core.util.StrUtil; |
||||
|
import lombok.Getter; |
||||
|
import lombok.Setter; |
||||
|
import org.nl.acs.modbus.TCPClient; |
||||
|
import org.nl.acs.modbus.TCPClient2; |
||||
|
|
||||
|
import java.util.Optional; |
||||
|
|
||||
|
/** |
||||
|
* @author zhangjiangwei |
||||
|
*/ |
||||
|
@Getter |
||||
|
@Setter |
||||
|
public class ItemProtocol { |
||||
|
|
||||
|
/** |
||||
|
* 光电信号 |
||||
|
*/ |
||||
|
public static final String ITEM_MOVE = "move"; |
||||
|
|
||||
|
private final PhotoelectricDetectionDeviceDriver driver; |
||||
|
|
||||
|
public ItemProtocol(PhotoelectricDetectionDeviceDriver driver) { |
||||
|
this.driver = driver; |
||||
|
} |
||||
|
|
||||
|
public int getMove() { |
||||
|
String ip = Optional.ofNullable(this.getDriver().getDevice().getExtraValue().get("ipAddress")).map(Object::toString).orElse(null); |
||||
|
String index = Optional.ofNullable(this.getDriver().getDevice().getExtraValue().get("index")).map(Object::toString).orElse("-1"); |
||||
|
if (StrUtil.isNotEmpty(ip) && !StrUtil.equals(index, "-1")) { |
||||
|
return TCPClient.startConnection(ip, 502, Integer.parseInt(index)); |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,99 @@ |
|||||
|
package org.nl.acs.modbus; |
||||
|
|
||||
|
import java.io.*; |
||||
|
import java.net.Socket; |
||||
|
import java.net.SocketTimeoutException; |
||||
|
|
||||
|
public class TCPClient { |
||||
|
|
||||
|
public static synchronized int startConnection(String ip, int port, int index) { |
||||
|
String hexMessage = "000600000006010200000008"; |
||||
|
try (Socket socket = new Socket(ip, port); |
||||
|
DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream()); |
||||
|
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { |
||||
|
socket.setSoTimeout(10000); |
||||
|
sendMessage(outToServer, hexMessage); |
||||
|
String message = receiveMessage(socket); |
||||
|
int[] binary = binary(message); |
||||
|
// for (int bit : binary) {
|
||||
|
// System.out.println(bit);
|
||||
|
// }
|
||||
|
return binary[index - 1]; |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
private static void sendMessage(DataOutputStream out, String hexMessage) throws IOException { |
||||
|
byte[] messageBytes = hexStringToByteArray(hexMessage); |
||||
|
out.write(messageBytes); |
||||
|
} |
||||
|
|
||||
|
private static String receiveMessage(Socket socket) throws IOException { |
||||
|
try (InputStream inputStream = socket.getInputStream(); |
||||
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { |
||||
|
byte[] data = new byte[1024]; |
||||
|
int nRead; |
||||
|
while ((nRead = inputStream.read(data, 0, data.length)) != -1) { |
||||
|
buffer.write(data, 0, nRead); |
||||
|
if (nRead < 1024) { |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
buffer.flush(); |
||||
|
return byteArrayToHexString(buffer.toByteArray()); |
||||
|
} catch (SocketTimeoutException e) { |
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static byte[] hexStringToByteArray(String s) { |
||||
|
int length = s.length(); |
||||
|
byte[] data = new byte[length / 2]; |
||||
|
for (int i = 0; i < length; i += 2) { |
||||
|
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) |
||||
|
+ Character.digit(s.charAt(i + 1), 16)); |
||||
|
} |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
private static String byteArrayToHexString(byte[] bytes) { |
||||
|
StringBuilder sb = new StringBuilder(); |
||||
|
for (byte b : bytes) { |
||||
|
sb.append(String.format("%02X", b)); |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static int[] binary(String hexString) { |
||||
|
String lastFourHex = hexString.substring(hexString.length() - 4); |
||||
|
int decimal = Integer.parseInt(lastFourHex, 16); |
||||
|
String binaryString = Integer.toBinaryString(decimal); |
||||
|
System.out.println("length:" + binaryString.length()); |
||||
|
int[] binaryArray = new int[binaryString.length()]; |
||||
|
for (int i = 0; i < binaryString.length(); i++) { |
||||
|
binaryArray[i] = Character.getNumericValue(binaryString.charAt(i)); |
||||
|
} |
||||
|
reverseArray(binaryArray); |
||||
|
return binaryArray; |
||||
|
} |
||||
|
|
||||
|
public static void reverseArray(int[] array) { |
||||
|
int temp; |
||||
|
int n = array.length; |
||||
|
for (int i = 0; i < n / 2; i++) { |
||||
|
temp = array[i]; |
||||
|
array[i] = array[n - i - 1]; |
||||
|
array[n - i - 1] = temp; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
int move = startConnection("192.168.1.12", 502, 2); |
||||
|
System.out.println(move); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,96 @@ |
|||||
|
package org.nl.acs.modbus; |
||||
|
|
||||
|
import lombok.SneakyThrows; |
||||
|
|
||||
|
import java.io.*; |
||||
|
import java.net.Socket; |
||||
|
import java.util.concurrent.ConcurrentHashMap; |
||||
|
import java.util.concurrent.locks.ReentrantLock; |
||||
|
|
||||
|
import static oshi.util.ParseUtil.byteArrayToHexString; |
||||
|
import static oshi.util.ParseUtil.hexStringToByteArray; |
||||
|
|
||||
|
public class TCPClient2 { |
||||
|
|
||||
|
private static final ConcurrentHashMap<String, Socket> connectionPools = new ConcurrentHashMap<>(); |
||||
|
private static final ReentrantLock lock = new ReentrantLock(); |
||||
|
private static final String hexMessage = "000600000006010200000008"; |
||||
|
|
||||
|
@SneakyThrows |
||||
|
public static int startConnection(String ip, int port, int index) { |
||||
|
String key = ip + ":" + port; |
||||
|
Socket socket = getSocketFromPool(key, ip, port); |
||||
|
if (socket == null) return -1; |
||||
|
DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream()); |
||||
|
sendMessage(outToServer, hexMessage); |
||||
|
String message = receiveMessage(socket); |
||||
|
int[] binary = binary(message); |
||||
|
return binary[index - 1]; |
||||
|
} |
||||
|
|
||||
|
//@SneakyThrows
|
||||
|
private static Socket getSocketFromPool(String key, String ip, int port) { |
||||
|
lock.lock(); |
||||
|
try { |
||||
|
if (connectionPools.keySet().contains(key) && connectionPools.get(key).isConnected()) { |
||||
|
return connectionPools.get(key); |
||||
|
} else { |
||||
|
connectionPools.remove(key); |
||||
|
Socket socket = new Socket(ip, port); |
||||
|
socket.setKeepAlive(true); |
||||
|
socket.setSoTimeout(10000); |
||||
|
connectionPools.put(key, socket); |
||||
|
return socket; |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} finally { |
||||
|
lock.unlock(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
private static void sendMessage(DataOutputStream out, String hexMessage) throws IOException { |
||||
|
byte[] messageBytes = hexStringToByteArray(hexMessage); |
||||
|
out.write(messageBytes); |
||||
|
} |
||||
|
|
||||
|
private static String receiveMessage(Socket socket) throws IOException { |
||||
|
InputStream inputStream = socket.getInputStream(); |
||||
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
||||
|
byte[] data = new byte[1024]; |
||||
|
int nRead; |
||||
|
while ((nRead = inputStream.read(data, 0, data.length)) != -1) { |
||||
|
buffer.write(data, 0, nRead); |
||||
|
if (nRead < 1024) { |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
buffer.flush(); |
||||
|
return byteArrayToHexString(buffer.toByteArray()); |
||||
|
} |
||||
|
|
||||
|
private static int[] binary(String hexString) { |
||||
|
if (hexString == null) { |
||||
|
hexString = "00060000000401020100"; |
||||
|
} |
||||
|
String lastFourHex = hexString.substring(hexString.length() - 4); |
||||
|
int decimal = Integer.parseInt(lastFourHex, 16); |
||||
|
String binaryString = Integer.toBinaryString(decimal); |
||||
|
int[] binaryArray = new int[binaryString.length()]; |
||||
|
for (int i = 0; i < binaryString.length(); i++) { |
||||
|
binaryArray[i] = Character.getNumericValue(binaryString.charAt(i)); |
||||
|
} |
||||
|
reverseArray(binaryArray); |
||||
|
return binaryArray; |
||||
|
} |
||||
|
|
||||
|
private static void reverseArray(int[] array) { |
||||
|
for (int i = 0, j = array.length - 1; i < j; i++, j--) { |
||||
|
int temp = array[i]; |
||||
|
array[i] = array[j]; |
||||
|
array[j] = temp; |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue