Skip to content

easymodbus4j 日志工具类

zengfr曾繁荣 edited this page Jun 1, 2019 · 3 revisions

easymodbus4j 日志工具类

maven:
<dependency>
  <artifactId>easymodbus4j-core</artifactId>
  <groupId>com.github.zengfr</groupId>
  <version>0.0.1</version>
<dependency>

public class ModbusFrameUtil {

private static String splitLine = PadUtil.padLeft("-", 128, '-');

public static void showFrameLog(InternalLogger logger, Channel channel, ModbusFrame frame) {
	byte[] bytes = ByteBufUtil.toBytes(frame.encode());
	String v1 = HexUtil.bytesToHexString(bytes, " ");
	String v2 = frame.toString();

	StringBuilder sBuilder = new StringBuilder();
	sBuilder.append(String.format("%s", getChannelAddress(channel)));
	sBuilder.append(String.format("\r\n%s", splitLine));
	sBuilder.append(String.format("\r\n%s", v1));
	sBuilder.append(String.format("\r\n%s", v2));
	sBuilder.append(String.format("\r\n%s", splitLine));
	if (frame.getFunction().toString().contains("Response")) {
		byte[] valuesArray = ModbusFunctionUtil.getFunctionValues(frame.getFunction());
		sBuilder.append(String.format("\r\nhex    :%s",HexUtil.bytesToHexString(valuesArray, " ")));
		sBuilder.append(String.format("\r\nbitset :%s",BitSet.valueOf(valuesArray)));
		sBuilder.append(String.format("\r\nbyte   :%s", StringUtils.join(valuesArray, ',')));
		sBuilder.append(String.format("\r\nshort  :%s", StringUtils.join(ByteUtil.toShortArray(valuesArray), ',')));
		sBuilder.append(String.format("\r\nint    :%s", StringUtils.join(ByteUtil.toIntArray(valuesArray), ',')));
		sBuilder.append(String.format("\r\nlong   b:%s", StringUtils.join(ByteUtil.toLongArray(valuesArray), ',')));
		sBuilder.append(String.format("\r\nfloat  b:%s", StringUtils.join(ByteUtil.toFloatArray(valuesArray), ',')));
		sBuilder.append(String.format("\r\ndouble b:%s", StringUtils.join(ByteUtil.toDoubleArray(valuesArray), ',')));
		sBuilder.append(String.format("\r\nchar   b:%s", StringUtils.join(ByteUtil.toCharArray(valuesArray), ',')));
		
		sBuilder.append(String.format("\r\nlong   l:%s", StringUtils.join(ByteUtil.toLongArray(valuesArray,false), ',')));
		sBuilder.append(String.format("\r\nfloat  l:%s", StringUtils.join(ByteUtil.toFloatArray(valuesArray,false), ',')));
		sBuilder.append(String.format("\r\ndouble l:%s", StringUtils.join(ByteUtil.toDoubleArray(valuesArray,false), ',')));
		sBuilder.append(String.format("\r\nchar   l:%s", StringUtils.join(ByteUtil.toCharArray(valuesArray,false), ',')));
		
		sBuilder.append(String.format("\r\n%s", splitLine));
	}
	logger.info(sBuilder.toString());

}