S7Util.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.mes.util;
  2. import com.github.s7connector.api.DaveArea;
  3. import com.github.s7connector.api.S7Connector;
  4. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import java.math.BigInteger;
  8. import java.text.DecimalFormat;
  9. public class S7Util {
  10. public static final Logger log = LoggerFactory.getLogger(S7Util.class);
  11. public static Boolean getFswAlarm(S7Connector s7Connector){
  12. byte[] bytes1 = s7Connector.read(DaveArea.DB, 9051, 1, 24);
  13. int alarm242 = getBit(bytes1[0],5);
  14. if(alarm242 == 1){
  15. return false;
  16. }
  17. int alarm243 = getBit(bytes1[0],4);
  18. if(alarm243 == 1){
  19. return false;
  20. }
  21. int alarm244 = getBit(bytes1[0],3);
  22. if(alarm244 == 1){
  23. return false;
  24. }
  25. int alarm245 = getBit(bytes1[0],2);
  26. if(alarm245 == 1){
  27. return false;
  28. }
  29. int alarm246 = getBit(bytes1[0],1);
  30. if(alarm246 == 1){
  31. return false;
  32. }
  33. int alarm247 = getBit(bytes1[0],0);
  34. if(alarm247 == 1){
  35. return false;
  36. }
  37. byte[] bytes2 = s7Connector.read(DaveArea.DB, 9051, 1, 25);
  38. int alarm250 = getBit(bytes2[0],7);
  39. if(alarm250 == 1){
  40. return false;
  41. }
  42. int alarm251 = getBit(bytes2[0],6);
  43. if(alarm251 == 1){
  44. return false;
  45. }
  46. int alarm252 = getBit(bytes2[0],5);
  47. if(alarm252 == 1){
  48. return false;
  49. }
  50. int alarm253 = getBit(bytes2[0],4);
  51. if(alarm253 == 1){
  52. return false;
  53. }
  54. int alarm254 = getBit(bytes2[0],3);
  55. if(alarm254 == 1){
  56. return false;
  57. }
  58. int alarm255 = getBit(bytes2[0],2);
  59. if(alarm255 == 1){
  60. return false;
  61. }
  62. int alarm256 = getBit(bytes2[0],1);
  63. if(alarm256 == 1){
  64. return false;
  65. }
  66. return true;
  67. }
  68. // 格式化s7取出的值
  69. public static String formatS7Val(byte[] val){
  70. int intValue = new BigInteger(xtob(val)).intValue();
  71. float floatValue = Float.intBitsToFloat(intValue);
  72. // 保留1位小数
  73. DecimalFormat df = new DecimalFormat("#.#");
  74. df.setMaximumFractionDigits(1);
  75. String result = df.format(floatValue);
  76. float roundedNum = Float.parseFloat(result);
  77. return roundedNum+"";
  78. }
  79. // 转大端排序
  80. private static byte[] xtob( byte[] bytes){
  81. return bytes;
  82. // // 创建新的byte数组并复制原始数据
  83. // byte[] reversedBytes = new byte[bytes.length];
  84. // System.arraycopy(bytes, 0, reversedBytes, 0, bytes.length);
  85. //
  86. // // 反转byte数组元素的位置
  87. // for (int i = 0; i < bytes.length / 2; i++) {
  88. // byte temp = reversedBytes[i];
  89. // reversedBytes[i] = reversedBytes[reversedBytes.length - 1 - i];
  90. // reversedBytes[reversedBytes.length - 1 - i] = temp;
  91. // }
  92. // return reversedBytes;
  93. }
  94. private static String byteToBinaryString(byte[] byteArray) {
  95. StringBuilder binaryString = new StringBuilder();
  96. for (byte b : byteArray) {
  97. binaryString.append(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'));
  98. }
  99. return binaryString.toString();
  100. }
  101. public static int getBit(byte b, int n) {
  102. // 将byte转换为二进制字符串
  103. String binaryString = Integer.toBinaryString(b & 0xFF);
  104. // 补足位数到8位
  105. binaryString = String.format("%8s", binaryString).replace(' ', '0');
  106. // 获取第n位的字符,然后转换为整数
  107. return binaryString.charAt(n) == '1' ? 1 : 0;
  108. }
  109. public static Integer formatS7Int(byte[] val){
  110. return new BigInteger(xtob(val)).intValue();
  111. }
  112. }