【Modbus】Java使用 modbus-master-tcp 读取和写入Modbus服务器数据

参考了云逸的 Java实现ModbusTCP通信

1、前言

在井下综采面需要用到工业级控制协议,采用了Modbus主站从站通讯方式,直接操作寄存器数据,实现读取和控制。

2、引用pom

<dependency>
<groupId>com.digitalpetri.modbus</groupId>
<artifactId>modbus-master-tcp</artifactId>
<version>1.1.0</version>
</dependency>

3、上代码

package com.ruoyi.project.socket.underJava;
import com.digitalpetri.modbus.FunctionCode;
import com.digitalpetri.modbus.codec.Modbus;
import com.digitalpetri.modbus.master.ModbusTcpMaster;
import com.digitalpetri.modbus.master.ModbusTcpMasterConfig;
import com.digitalpetri.modbus.requests.*;
import com.digitalpetri.modbus.responses.*;
import com.ruoyi.project.utils.HexUtils;
import com.ruoyi.project.utils.LogHelper;
import io.netty.buffer.ByteBuf;
import io.netty.util.ReferenceCountUtil;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/**
* @version: V1.0
* @description: modbus TCP协议 数据读取写入
* @date: 2021-02-04
*/
public class ModbusMasterTCPFromZhengMei {
/**
* 日志记录
*/
private static LogHelper log = new LogHelper();
/**
* tcp连接对象
*/
private static ModbusTcpMaster modbusTcpMaster;
/**
* modbus ip地址
*/
private static final String IP = "192.168.19.130";
/**
* 端口
*/
private static final Integer PORT = 502;
/**
* modubs从站ID
*/
private static final Integer UNIT_ID = 1;
/**
* 成功代码
*/
private static final String SUCCESS_CODE = "0x000000";
/**
* 与modubs连接异常
*/
private static final String COON_FAIL_CODE = "0x000001";
/**
* 向modubs发送命令执行异常
*/
private static final String EXEC_FAIL_CODE = "0x000002";
/**
* 数据写入失败
*/
private static final String WRITE_FAIL_CODE = "0x000004";
private static String logName = "ModbusMasterTCPFromZhengMei ";
/**
* @description: 初始化连接
* @param:
* @return: 结果值
*/
private static String init() {
try {
if (modbusTcpMaster == null) {
// 创建配置
ModbusTcpMasterConfig config = new ModbusTcpMasterConfig.Builder(IP).setPort(PORT).build();
// 新建连接
modbusTcpMaster = new ModbusTcpMaster(config);
}
return SUCCESS_CODE;
} catch (Exception e) {
log.error("ModbusMasterTCP::init - " + e.getMessage() + "(0x000001)" +
"\r\n" + Arrays.toString(e.getStackTrace()));
return COON_FAIL_CODE;
}
}
/**
* @description: 释放连接
* @param:
* @return: 结果值
*/
private static String release() {
try {
if (modbusTcpMaster != null) {
modbusTcpMaster.disconnect();
}
Modbus.releaseSharedResources();
return SUCCESS_CODE;
} catch (Exception e) {
return COON_FAIL_CODE;
}
}
/**
* @param address 寄存器地址
* @param value 写入值
* @param unitId id
* @description: 写HoldingRegister数据
* @return: 结果值
*/
public static String writeHoldingRegisters(Integer address, Integer value, Integer unitId) {
ModbusResponse modbusResponse;
try {
// 发送单个寄存器数据,一般是无符号16位值:比如10
CompletableFuture<ModbusResponse> future = modbusTcpMaster.sendRequest(new WriteSingleRegisterRequest(address, value), unitId);
//获取写入的响应流
modbusResponse = future.get();
if (modbusResponse == null) {
System.out.println("FCSC-ExternalConnection WriteHoldingRegisters:modbusResponse is null ");
return WRITE_FAIL_CODE;
}
//获取写入的响应FunctionCode
FunctionCode functionCode = modbusResponse.getFunctionCode();
System.out.println("FCSC-ExternalConnection functionCode=" + functionCode + " value=" + value);
if (functionCode == FunctionCode.WriteSingleRegister) {
return SUCCESS_CODE;
} else {
return WRITE_FAIL_CODE;
}
} catch (Exception e) {
log.error("ModbusMasterTCP::writeHoldingRegisters - " + e.getMessage() + ",value=" + value + "(0x000002)"
+ "\r\n" + Arrays.toString(e.getStackTrace()));
e.printStackTrace();
return EXEC_FAIL_CODE;
} finally {
// String releaseRes = release();
// //如果释放连接失败,返回执行失败
// if (!SUCCESS_CODE.equals(releaseRes)) {
// return releaseRes;
// }
}
}
/**
* @param address 寄存器地址
* @param quantity 写位数
* @param values 写入值
* @description: 写HoldingRegister数据
* @return: 结果值
*/
public static String WriteMultipleRegisters(Integer address, Integer quantity, byte[] values) {
try {
WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(address, quantity, values);
// 发送单个寄存器数据,一般是无符号16位值:比如10
CompletableFuture<ModbusResponse> future = modbusTcpMaster.sendRequest(request, UNIT_ID);
ModbusResponse modbusResponse;
//获取写入的响应流
modbusResponse = future.get();
if (modbusResponse == null) {
System.out.println("FCSC-ExternalConnection WriteMultipleRegisters:modbusResponse is null ");
return WRITE_FAIL_CODE;
}
//获取写入的响应FunctionCode
FunctionCode functionCode = modbusResponse.getFunctionCode();
System.out.println("FCSC-ExternalConnection functionCode.getCode()===" + functionCode.getCode() + "=" + functionCode);
if (functionCode == FunctionCode.WriteMultipleRegisters) {
return SUCCESS_CODE;
} else {
return WRITE_FAIL_CODE;
}
} catch (InterruptedException e) {
e.printStackTrace();
return EXEC_FAIL_CODE;
} catch (ExecutionException e) {
e.printStackTrace();
return EXEC_FAIL_CODE;
} finally {
// String releaseRes = release();
// //如果释放连接失败,返回执行失败
// if (!SUCCESS_CODE.equals(releaseRes)) {
// return releaseRes;
// }
}
}
/**
* @description: 写入数据
* @param: address 寄存器地址
* @param: value 写入值
* @return: 结果值
*/
public static String writeByteData(byte[] values) {
String initRes = init();
//如果初始化失败,则立即返回
if (!SUCCESS_CODE.equals(initRes)) {
return initRes;
}
String writeRes = WriteMultipleRegisters(916, 2, values);
//如果写入失败,返回
if (!SUCCESS_CODE.equals(writeRes)) {
return writeRes;
}
return SUCCESS_CODE;
}
/**
* @description: 写入数据
* @param: address 寄存器地址
* @param: value 写入值
* @return: 结果值
*/
public static String writeData(Integer address, Integer value) {
String initRes = init();
//如果初始化失败,则立即返回
if (!SUCCESS_CODE.equals(initRes)) {
return initRes;
}
String writeRes = writeHoldingRegisters(address, value, UNIT_ID);
//如果写入失败,返回
if (!SUCCESS_CODE.equals(writeRes)) {
return writeRes;
}
return SUCCESS_CODE;
}
/**
* @description: writeDemo
* @param:
* @return:
*/
public static void writeDemo() {
// 初始化资源
init();
Random random = new Random();
int value = random.nextInt(100) + 1;
System.out.println("write value=" + value);
try {
writeHoldingRegisters(222, value, UNIT_ID);
} catch (Exception e) {
e.printStackTrace();
}
// 释放资源
release();
}
/**
* @description: readDemo
* @param:
* @return:
*/
public static void readDemo() {
try {
// 初始化资源
init();
System.out.println("readDemo=" + readHoldingRegisters(222, 4, 1));
release();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取Coils开关量
*
* @param address 寄存器开始地址
* @param quantity 数量
* @param unitId ID
* @return 读取值
* @throws InterruptedException 异常
* @throws ExecutionException 异常
*/
public static Boolean readCoils(int address, int quantity, int unitId)
throws InterruptedException, ExecutionException {
Boolean result = null;
CompletableFuture<ReadCoilsResponse> future = modbusTcpMaster.sendRequest(new ReadCoilsRequest(address, quantity), unitId);
ReadCoilsResponse readCoilsResponse = future.get();// 工具类做的同步返回.实际使用推荐结合业务进行异步处理
if (readCoilsResponse != null) {
ByteBuf buf = readCoilsResponse.getCoilStatus();
result = buf.readBoolean();
ReferenceCountUtil.release(readCoilsResponse);
}
return result;
}
/**
* 读取readDiscreteInputs开关量
*
* @param address 寄存器开始地址
* @param quantity 数量
* @param unitId ID
* @return 读取值
* @throws InterruptedException 异常
* @throws ExecutionException 异常
*/
public static Boolean readDiscreteInputs(int address, int quantity, int unitId)
throws InterruptedException, ExecutionException {
Boolean result = null;
CompletableFuture<ReadDiscreteInputsResponse> future = modbusTcpMaster
.sendRequest(new ReadDiscreteInputsRequest(address, quantity), unitId);
ReadDiscreteInputsResponse discreteInputsResponse = future.get();// 工具类做的同步返回.实际使用推荐结合业务进行异步处理
if (discreteInputsResponse != null) {
ByteBuf buf = discreteInputsResponse.getInputStatus();
result = buf.readBoolean();
ReferenceCountUtil.release(discreteInputsResponse);
}
return result;
}
/**
* 读取HoldingRegister数据
*
* @param address 寄存器地址
* @param quantity 寄存器数量
* @param unitId id
* @return 读取结果
* @throws InterruptedException 异常
* @throws ExecutionException 异常
*/
public static Number readHoldingRegisters(int address, int quantity, int unitId)
throws InterruptedException, ExecutionException {
Number result = null;
CompletableFuture<ReadHoldingRegistersResponse> future = modbusTcpMaster
.sendRequest(new ReadHoldingRegistersRequest(address, quantity), unitId);
ReadHoldingRegistersResponse readHoldingRegistersResponse = future.get();// 工具类做的同步返回.实际使用推荐结合业务进行异步处理
if (readHoldingRegistersResponse != null) {
ByteBuf buf = readHoldingRegistersResponse.getRegisters();
byte[] bytes = new byte[buf.capacity()];
buf.readBytes(bytes, 0, buf.capacity());
// System.out.println("bytes=" + Arrays.toString(bytes));
result = HexUtils.bytes2Short(bytes, 0);
ReferenceCountUtil.release(readHoldingRegistersResponse);
}
return result;
}
public static Integer readData(Integer address, Integer quantity) throws Exception {
String initRes = init();
//如果初始化失败,则立即返回
if (!SUCCESS_CODE.equals(initRes)) {
return -5;
}
Number number = readHoldingRegisters(address, quantity, UNIT_ID);
return number.intValue();
}
/**
* 读取InputRegisters模拟量数据
*
* @param address 寄存器开始地址
* @param quantity 数量
* @param unitId ID
* @return 读取值
* @throws InterruptedException 异常
* @throws ExecutionException 异常
*/
public static Number readInputRegisters(int address, int quantity, int unitId)
throws InterruptedException, ExecutionException {
Number result = null;
CompletableFuture<ReadInputRegistersResponse> future = modbusTcpMaster
.sendRequest(new ReadInputRegistersRequest(address, quantity), unitId);
ReadInputRegistersResponse readInputRegistersResponse = future.get();// 工具类做的同步返回.实际使用推荐结合业务进行异步处理
if (readInputRegistersResponse != null) {
ByteBuf buf = readInputRegistersResponse.getRegisters();
result = buf.readDouble();
ReferenceCountUtil.release(readInputRegistersResponse);
}
return result;
}
/**
* @description: writeDemo2
* @param:
* @return:
*/
public static void writeDemo2() {
Random random = new Random();
int value = random.nextInt(100) + 1;
System.out.println("ready write value=" + value);
String res = writeData(222, value);
System.out.println("res=" + res);
}
public static void writeDemo3() {
byte[] bytes = new byte[]{0, 2, 0, 3};
String res = writeByteData(bytes);
System.out.println(res);
}
public static byte[] double2Bytes(double d) {
long value = Double.doubleToRawLongBits(d);
byte[] byteRet = new byte[8];
for (int i = 0; i < 8; i++) {
byteRet[i] = (byte) ((value >> 8 * i) & 0xff);
}
return byteRet;
}
public static String writeCoils(int address, boolean value) {
try {
init();
WriteSingleCoilRequest writeSingleCoilRequest = new WriteSingleCoilRequest(address, value);
CompletableFuture<ModbusResponse> request = modbusTcpMaster.sendRequest(writeSingleCoilRequest, UNIT_ID);
ModbusResponse modbusResponse = request.get();
if (modbusResponse == null) {
System.out.println(logName + "writeCoils:modbusResponse is null ");
return WRITE_FAIL_CODE;
}
FunctionCode functionCode = modbusResponse.getFunctionCode();
System.out.println(logName + "writeCoils address=" + address + " value=" + value + " functionCode=" + functionCode);
if (functionCode == FunctionCode.WriteSingleCoil) {
return SUCCESS_CODE;
} else {
return WRITE_FAIL_CODE;
}
} catch (Exception e) {
log.error(logName + "writeCoils - " + e.getMessage() + ",address" + address + ",value=" + value + "(0x000002)"
+ "\r\n" + Arrays.toString(e.getStackTrace()));
System.out.println(logName + "writeCoils - " + e.getMessage());
return WRITE_FAIL_CODE;
}
}
/**
* @description: main
* @param:
* @return:
*/
public static void main(String[] args) {
// writeDemo();
// readDemo();
writeDemo3();
release();
}
}

4、HexUtils

package com.ruoyi.project.utils;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
public class HexUtils {
public static void main(String[] args) {
// byte[] bytes = shortToByteBig((short) 33);
// System.out.println(Arrays.toString(bytes));
}
private static final int[] DEC = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15};
private static final byte[] HEX = new byte[]{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102};
private static final char[] hex = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102};
private static final char[] HEXES = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};
public HexUtils() {
}
/**
* 字符获取十进制
*
* @param index
* @return
*/
public static int getDec(int index) {
try {
return DEC[index - 48];
} catch (ArrayIndexOutOfBoundsException var2) {
return -1;
}
}
public static byte getHex(int index) {
return HEX[index];
}
public static String toHexString(byte[] bytes) {
if (null == bytes) {
return null;
} else {
StringBuilder sb = new StringBuilder(bytes.length << 1);
for (int i = 0; i < bytes.length; ++i) {
sb.append(hex[(bytes[i] & 240) >> 4]).append(hex[bytes[i] & 15]);
}
return sb.toString();
}
}
public static byte[] fromHexString(String input) {
if (input == null) {
return null;
} else if ((input.length() & 1) == 1) {
// 肯定是偶数位,奇数位转换失败:一个byte对应两个字符
throw new IllegalArgumentException("hexUtils.fromHex.oddDigits");
} else {
char[] inputChars = input.toCharArray();
byte[] result = new byte[input.length() >> 1];
for (int i = 0; i < result.length; ++i) {
int upperNibble = getDec(inputChars[2 * i]);
int lowerNibble = getDec(inputChars[2 * i + 1]);
if (upperNibble < 0 || lowerNibble < 0) {
// 字符不存在
throw new IllegalArgumentException("hexUtils.fromHex.nonHex");
}
result[i] = (byte) ((upperNibble << 4) + lowerNibble);
}
return result;
}
}
/**
* byte数组 转换成 16进制小写字符串
*/
public static String bytes2HexStr(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
StringBuilder hex = new StringBuilder();
for (byte b : bytes) {
hex.append(HEXES[(b >> 4) & 0x0F]);
hex.append(HEXES[b & 0x0F]);
}
return hex.toString();
}
/**
* byte[]数组转成字符串
*
* @param bytes
* @return
*/
public static String bytes2String(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
String str1 = "";
StringBuilder sb = new StringBuilder(str1);
for (byte element : bytes) {
sb.append(String.valueOf(element));
}
return sb.toString();
}
/**
* byte[]数组转成16进制大写字符串
*
* @param bytes
* @return
*/
public static String bytes2HexStrUpper(byte[] bytes) {
String jiaoYan = bytes2String(bytes);
BigInteger target = new BigInteger(jiaoYan);
jiaoYan = Long.toHexString(target.longValue()).toUpperCase();
return jiaoYan;
}
/**
* byte[]数组转char[]数组
*
* @param bytes
* @return
*/
public static char[] bytes2Chars(byte[] bytes) {
Charset cs = Charset.forName("UTF-8");
ByteBuffer bb = ByteBuffer.allocate(bytes.length);
bb.put(bytes);
bb.flip();
CharBuffer cb = cs.decode(bb);
return cb.array();
}
/**
* 16进制字符串 转换为对应的 byte数组
*/
public static byte[] hex2Bytes(String hex) {
if (hex == null || hex.length() == 0) {
return null;
}
char[] hexChars = hex.toCharArray();
byte[] bytes = new byte[hexChars.length / 2]; // 如果 hex 中的字符不是偶数个, 则忽略最后一个
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
}
return bytes;
}
//十六进制字符串转字符串
public static String fromHex(String hex) {
return new String(decodeHex(hex.toCharArray()));
}
protected static byte[] decodeHex(char[] data) {
int len = data.length;
if ((len & 0x01) != 0) {
throw new RuntimeException("字符个数应该为偶数");
}
byte[] out = new byte[len >> 1];
for (int i = 0, j = 0; j < len; i++) {
int f = toDigit(data[j], j) << 4;
j++;
f |= toDigit(data[j], j);
j++;
out[i] = (byte) (f & 0xFF);
}
return out;
}
protected static int toDigit(char ch, int index) {
int digit = Character.digit(ch, 16);
if (digit == -1) {
throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index);
}
return digit;
}
// /**
// * 计算校验和
// */
// public static String sumCheck(byte[] b, int len) {
// int sum = 0;
// for (int i = 0; i < len; i++) {
// sum = sum + b[i];
// }
// // System.out.println(sum);
// /*
// * if(sum > 0xff){ //超过了255,使用补码(补码 = 原码取反 + 1) sum = ~sum; sum = sum +
// * 1; }
// */
// return Integer.toHexString((sum & 0x0ff)).toUpperCase();
// }
/**
* 计算校验和
*/
public static byte sumCheckByte(byte[] b) {
int sum = 0;
for (int i = 0; i < b.length; i++) {
sum = sum + b[i];
}
return (byte) (sum & 0x0ff);
}
/**
* 将int转为高字节在前,低字节在后的byte数组(大端)
*
* @param n int
* @return byte[]
*/
public static byte[] intToByteBig(int n) {
byte[] b = new byte[4];
b[3] = (byte) (n & 0xff);
b[2] = (byte) (n >> 8 & 0xff);
b[1] = (byte) (n >> 16 & 0xff);
b[0] = (byte) (n >> 24 & 0xff);
return b;
}
/**
* 将int转为低字节在前,高字节在后的byte数组(小端)
*
* @param n int
* @return byte[]
*/
public static byte[] intToByteLittle(int n) {
byte[] b = new byte[4];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
return b;
}
/**
* byte数组到int的转换(小端)
*
* @param bytes
* @return
*/
public static int bytes2IntLittle(byte[] bytes) {
int int1 = bytes[0] & 0xff;
int int2 = (bytes[1] & 0xff) << 8;
int int3 = (bytes[2] & 0xff) << 16;
int int4 = (bytes[3] & 0xff) << 24;
return int1 | int2 | int3 | int4;
}
/**
* byte数组到int的转换(大端)
*
* @param bytes
* @return
*/
public static int bytes2IntBig(byte[] bytes) {
int int1 = bytes[3] & 0xff;
int int2 = (bytes[2] & 0xff) << 8;
int int3 = (bytes[1] & 0xff) << 16;
int int4 = (bytes[0] & 0xff) << 24;
return int1 | int2 | int3 | int4;
}
/**
* 将short转为高字节在前,低字节在后的byte数组(大端)
*
* @param n short
* @return byte[]
*/
public static byte[] shortToByteBig(short n) {
byte[] b = new byte[2];
b[1] = (byte) (n & 0xff);
b[0] = (byte) (n >> 8 & 0xff);
return b;
}
/**
* 将short转为低字节在前,高字节在后的byte数组(小端)
*
* @param n short
* @return byte[]
*/
public static byte[] shortToByteLittle(short n) {
byte[] b = new byte[2];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
return b;
}
/**
* 读取小端byte数组为short
*
* @param b
* @return
*/
public static short byteToShortLittle(byte[] b) {
return (short) (((b[1] << 8) | b[0] & 0xff));
}
/**
* 读取大端byte数组为short
*
* @param b
* @return
*/
public static short byteToShortBig(byte[] b) {
return (short) (((b[0] << 8) | b[1] & 0xff));
}
/**
* long类型转byte[] (大端)
*
* @param n
* @return
*/
public static byte[] longToBytesBig(long n) {
byte[] b = new byte[8];
b[7] = (byte) (n & 0xff);
b[6] = (byte) (n >> 8 & 0xff);
b[5] = (byte) (n >> 16 & 0xff);
b[4] = (byte) (n >> 24 & 0xff);
b[3] = (byte) (n >> 32 & 0xff);
b[2] = (byte) (n >> 40 & 0xff);
b[1] = (byte) (n >> 48 & 0xff);
b[0] = (byte) (n >> 56 & 0xff);
return b;
}
/**
* long类型转byte[] (小端)
*
* @param n
* @return
*/
public static byte[] longToBytesLittle(long n) {
byte[] b = new byte[8];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
b[4] = (byte) (n >> 32 & 0xff);
b[5] = (byte) (n >> 40 & 0xff);
b[6] = (byte) (n >> 48 & 0xff);
b[7] = (byte) (n >> 56 & 0xff);
return b;
}
/**
* byte[]转long类型(小端)
*
* @param array
* @return
*/
public static long bytesToLongLittle(byte[] array) {
return ((((long) array[0] & 0xff) << 0)
| (((long) array[1] & 0xff) << 8)
| (((long) array[2] & 0xff) << 16)
| (((long) array[3] & 0xff) << 24)
| (((long) array[4] & 0xff) << 32)
| (((long) array[5] & 0xff) << 40)
| (((long) array[6] & 0xff) << 48)
| (((long) array[7] & 0xff) << 56));
}
/**
* byte[]转long类型(大端)
*
* @param array
* @return
*/
public static long bytesToLongBig(byte[] array) {
return ((((long) array[0] & 0xff) << 56)
| (((long) array[1] & 0xff) << 48)
| (((long) array[2] & 0xff) << 40)
| (((long) array[3] & 0xff) << 32)
| (((long) array[4] & 0xff) << 24)
| (((long) array[5] & 0xff) << 16)
| (((long) array[6] & 0xff) << 8)
| (((long) array[7] & 0xff) << 0));
}
/**
* byte[]数组转short类型
*/
public static short bytes2Short(byte[] bytes, int startIndex) {
byte high = bytes[startIndex];
byte low = bytes[startIndex + 1];
short z = (short) (((high & 0xFF) << 8) | (0xFF & low));
return z;
}
/**
* byte[]数组转short类型,大端排序
*/
public static short bytes2ShortBig(byte[] bytes) {
byte high = bytes[0];
byte low = bytes[1];
short z = (short) (((high & 0xFF)) | (0xFF & low) >> 8);
return z;
}
// /**
// * short类型转byte[]数组,
// *
// * @param s
// * @return
// */
// public static byte[] unsignedShortToByte2(short s) {
// byte[] targets = new byte[2];
// targets[0] = (byte) (0xFF & s);
// targets[1] = (byte) (0x00FF & (s >> 8));
// return targets;
// }
// /**
// * short类型转byte[],高前 低后
// */
// public static byte[] shortToByte(short s) {
// byte[] targets = new byte[2];
// targets[0] = (byte) (0xFF & s);
// targets[1] = (byte) (0x00FF & (s >> 8));
// return targets;
// }
// /**
// * byte[]数组转short类型,得到字节数组长度
// *
// * @param bytes
// * @param startIndex
// * @return
// */
// public static short bytes2ShortLen(byte[] bytes, int startIndex) {
// byte high = bytes[startIndex + 1];
// byte low = bytes[startIndex];
// short z = (short) (((high & 0xFF) << 8) | (0xFF & low));
// return z;
// }
// /**
// * byte[]数组转short类型
// */
// public static short bytes2Short(byte[] bytes, int startIndex) {
// byte high = bytes[startIndex];
// byte low = bytes[startIndex + 1];
// short z = (short) (((high & 0xFF) << 8) | (0xFF & low));
// return z;
// }
// /**
// * byte[]数组转short类型,大端排序
// */
// public static short bytes2ShortBig(byte[] bytes) {
// byte high = bytes[0];
// byte low = bytes[1];
// short z = (short) (((high & 0xFF)) | (0xFF & low) >> 8);
// return z;
// }
// /**
// * int转byte[]
// *
// * @param i
// * @return
// */
// public static byte[] int2Bytes(int i) {
// byte[] result = new byte[4];
// result[0] = (byte) ((i >> 24) & 0xFF);
// result[1] = (byte) ((i >> 16) & 0xFF);
// result[2] = (byte) ((i >> 8) & 0xFF);
// result[3] = (byte) (i & 0xFF);
// return result;
// }
//
// /**
// * byte[]转int
// *
// * @param bytes
// * @return
// */
// public static int bytes2Int(byte[] bytes) {
// int num = bytes[3] & 0xFF;
// num |= ((bytes[2] << 8) & 0xFF00);
// num |= ((bytes[1] << 16) & 0xFF0000);
// num |= ((bytes[0] << 24) & 0xFF0000);
// return num;
// }
}
posted @   一只桔子2233  阅读(12866)  评论(4编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2019-01-05 30.1 一个简单的混合锁
2019-01-05 29.4 内核模式构造
点击右上角即可分享
微信分享提示