Compare commits
2 Commits
master
...
modbus_cli
Author | SHA1 | Date |
---|---|---|
gengby | fb2913c9aa | 6 months ago |
gengby | 6bb18a3312 | 6 months ago |
@ -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; |
|||
} |
|||
} |
|||
} |
Before Width: | Height: | Size: 125 KiB After Width: | Height: | Size: 125 KiB |
Before Width: | Height: | Size: 125 KiB After Width: | Height: | Size: 125 KiB |
Before Width: | Height: | Size: 125 KiB After Width: | Height: | Size: 125 KiB |
Before Width: | Height: | Size: 277 KiB After Width: | Height: | Size: 125 KiB |
Before Width: | Height: | Size: 275 KiB After Width: | Height: | Size: 125 KiB |
Before Width: | Height: | Size: 125 KiB After Width: | Height: | Size: 125 KiB |