Modbus.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.jeesite.modules;
  2. import com.alibaba.fastjson.JSON;
  3. //import com.github.xingshuangs.iot.common.buff.EByteBuffFormat;
  4. import com.github.xingshuangs.iot.protocol.common.buff.EByteBuffFormat;
  5. import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
  6. import java.nio.ByteBuffer;
  7. import java.nio.ByteOrder;
  8. import java.nio.charset.Charset;
  9. import java.util.List;
  10. public class Modbus {
  11. public static void main(String[] args) {
  12. // ModbusTcp modbusTcp = new ModbusTcp(2, "192.168.1.211",8000);
  13. // List<Boolean> list = modbusTcp.readCoil(26,1);
  14. // System.err.println("6002:"+list.toString());
  15. ModbusTcp modbusTcp = new ModbusTcp(1,"192.168.1.91");
  16. modbusTcp.writeInt16(8,(short)0);
  17. Short value = modbusTcp.readInt16(8);
  18. System.out.println(value);
  19. // ModbusTcp modbusTcp = new ModbusTcp(1,"192.168.88.99");
  20. // modbusTcp.writeInt32(15000*2,0, EByteBuffFormat.DC_BA);
  21. //
  22. // Integer ret3 = modbusTcp.readInt32(15000*2,EByteBuffFormat.DC_BA);
  23. // System.out.println(ret3);
  24. // Integer ret33 = modbusTcp.readInt32(15001*2,EByteBuffFormat.DC_BA);
  25. // System.out.println(ret33);
  26. // Integer ret333 = modbusTcp.readInt32(15004*2,EByteBuffFormat.DC_BA);
  27. // System.out.println(ret333);
  28. // Integer ret = modbusTcp.readInt32(58*2, EByteBuffFormat.DC_BA);
  29. // modbusTcp.writeInt32(6002*2,1, EByteBuffFormat.DC_BA);
  30. // modbusTcp.writeInt32(58*2, 0);
  31. // Integer ret = modbusTcp.readInt32(6000*2, EByteBuffFormat.DC_BA);
  32. // System.err.println("6001:"+ret);
  33. //
  34. // Integer ret2 = modbusTcp.readInt32(6002*2, EByteBuffFormat.DC_BA);
  35. // System.err.println("6002:"+ret2);
  36. // ModbusTcp plc = new ModbusTcp(1,"192.168.20.21");
  37. //// System.out.println("plc:"+plc.readCoil(99,1));
  38. ////
  39. // int val = plc.readInt32(99, EByteBuffFormat.DC_BA);
  40. ////
  41. //
  42. // System.out.println("转换后的整数:" + val);
  43. //// int val = plc.readInt32(100, EByteBuffFormat.AB_CD);
  44. //
  45. // String binaryString = String.format("%16s", Integer.toBinaryString(val & 0xFF)).replace(' ', '0');
  46. // System.out.println("Binary string: " + binaryString);
  47. // System.out.println("0: " + getCharByPosition(binaryString,0));
  48. // System.out.println("1: " + getCharByPosition(binaryString,1));
  49. // System.out.println("2: " + getCharByPosition(binaryString,2));
  50. // System.out.println("3: " + getCharByPosition(binaryString,3));
  51. // System.out.println("4: " + getCharByPosition(binaryString,4));
  52. // System.out.println("5: " + getCharByPosition(binaryString,5));
  53. // System.out.println("6: " + getCharByPosition(binaryString,6));
  54. // System.out.println("7: " + getCharByPosition(binaryString,7));
  55. //
  56. // int value = Integer.parseInt(binaryString, 2);
  57. // System.out.println("转换后的整数:" + value);
  58. //
  59. // String newBinaryString = setCharByPosition(binaryString,0,'0');
  60. // System.out.println("newBinaryString: " + newBinaryString);
  61. //
  62. // int value2 = Integer.parseInt(newBinaryString, 2);
  63. // System.out.println("转换后的整数2:" + value2);
  64. //
  65. //// plc.writeInt32(99,value2, EByteBuffFormat.DC_BA);
  66. //
  67. // System.out.println("D100.0: " + getCharByPosition(binaryString, 0));
  68. // System.out.println("D100.1: " + getCharByPosition(binaryString, 1));
  69. // System.out.println("D100.2: " + getCharByPosition(binaryString, 2));
  70. // System.out.println("D100.3: " + getCharByPosition(binaryString, 3));
  71. // System.out.println("D100.4: " + getCharByPosition(binaryString, 4));
  72. // System.out.println("D100.5: " + getCharByPosition(binaryString, 5));
  73. // System.out.println("D100.6: " + getCharByPosition(binaryString, 6));
  74. // System.out.println("D100.7: " + getCharByPosition(binaryString, 7));
  75. // double ww = Double.valueOf("1.1");
  76. // System.out.println("ww:"+ww);
  77. // ModbusTcp plc = new ModbusTcp(1,"192.168.1.211",8000);
  78. //
  79. // plc.writeCoil(8217, true);
  80. //
  81. // System.out.println("plc:"+plc.readCoil(200,1));
  82. //
  83. // int D500 = plc.readInt32(102);//500=>100
  84. // int D208 = plc.readInt32(208);
  85. //
  86. // System.out.println("D500:"+D500);
  87. //
  88. // System.out.println("D208:"+D208);
  89. // ModbusTcp plc = new ModbusTcp("192.168.1.18");
  90. // 单枪
  91. // plc.writeCoil(3328,true);
  92. // plc.writeCoil(3079,false);
  93. // plc.writeCoil(3328,true); // 必须选关掉关机,开机才起左右
  94. // plc.writeCoil(3128,true);
  95. // plc.writeCoil(3128,false);
  96. // plc.writeInt16(1128,(short) 3);
  97. // plc.writeInt16(1136,(short) 0);
  98. // plc.writeInt16(1138,(short) 0);
  99. // plc.writeCoil(3128,true);
  100. // // 设置模式 1=标记铆接模式
  101. // plc.writeInt16(1092,(short) 1);
  102. // // 设置任务
  103. // plc.writeInt16(1128,(short) 80);
  104. // plc.writeInt16(1136,(short) 2);
  105. // plc.writeInt16(1138,(short) 1);
  106. //
  107. // plc.writeCoil(3128,true);
  108. //
  109. //
  110. // System.out.println("任务数量:"+plc.readInt16(1128)); // 1129 任务数量
  111. // System.out.println("已铆接数量:"+plc.readInt16(1136)); // 1137 任务数量
  112. // System.out.println("合格数量:"+plc.readInt16(1138)); // 1139 合格数量
  113. //
  114. // System.out.println("任务数量2:"+plc.readInt16(1126)); // 1129 任务数量
  115. // System.out.println("已铆接数量2:"+plc.readInt16(1144)); // 1137 任务数量
  116. // System.out.println("合格数量2:"+plc.readInt16(1146)); // 1139 合格数量
  117. // System.out.println("合格数量2:"+plc.readInt16(1160)); // 1139 合格数量
  118. // System.out.println("合格数量2:"+plc.readInt16(1162)); // 1139 合格数量
  119. ////
  120. // System.out.println("控制模式:"+plc.readInt16(1090)); // 1091 控制模式
  121. // System.out.println("系统运行中:"+plc.readCoil(3128,1)); // 3129 系统运行中
  122. // System.out.println("结束暂停:"+plc.readCoil(3132,1)); // 3133 系统运行中
  123. // System.out.println("远程开机:"+plc.readCoil(3328,1)); // 3329 远程开机
  124. // System.out.println("远程关机:"+plc.readCoil(3079,1)); // 3080 远程关机
  125. // System.out.println("作业模式:"+plc.readInt16(1092)); // 1093 作业模式 1=标记铆接模式
  126. // System.out.println("最大拉力:"+plc.readInt16(1064)); // 1065 最大拉力
  127. // System.out.println("对应位移:"+plc.readInt16(1065)); // 1066 对应位移
  128. // System.out.println("任务数量:"+plc.readInt16(1128)); // 1129 任务数量
  129. // System.out.println("已铆接数量:"+plc.readInt16(1136)); // 1137 任务数量
  130. // System.out.println("合格数量:"+plc.readInt16(1138)); // 1139 合格数量
  131. // System.out.println("质量状态:"+plc.readInt16(1072)); // 1073 质量状态
  132. // System.out.println("F-out:"+plc.readInt16(1064));
  133. // System.out.println("S-out:"+plc.readInt16(1065));
  134. // System.out.println("F-min:"+plc.readInt16(1068));
  135. // System.out.println("S-min:"+plc.readInt16(1070));
  136. // System.out.println("F-max:"+plc.readInt16(1069));
  137. // System.out.println("S-max:"+plc.readInt16(1071));
  138. // System.out.println("OK:"+plc.readCoil(3101,1));
  139. // System.out.println("NG:"+plc.readCoil(3102,1));
  140. // System.out.println("工件完成:"+plc.readCoil(3155,1));
  141. // System.out.println("暂停:"+plc.readCoil(3156,1));
  142. }
  143. public static int getCharByPosition(String binaryString, int position) {
  144. //// 想要获取的位的位置(从右到左,从0开始计数) position
  145. // 获取指定位置的字符并转换为数字
  146. char bit = binaryString.charAt(15 - position); // 调整位置以符合从右到左的计数方式
  147. int value = (int) Character.getNumericValue(bit);
  148. return value;
  149. }
  150. public static String setCharByPosition(String binaryString, int position, char value) {
  151. //// 想要获取的位的位置(从右到左,从0开始计数) position
  152. StringBuilder sb = new StringBuilder(binaryString);
  153. sb.setCharAt(15 - position, value);
  154. return sb.toString();
  155. }
  156. // 大端排序转字符串
  157. public static String bytesToBigEndianString(byte[] bytes) {
  158. // 使用ByteBuffer创建一个大端缓冲区
  159. ByteBuffer buffer = ByteBuffer.allocate(bytes.length).order(ByteOrder.BIG_ENDIAN);
  160. // 将byte数组复制到ByteBuffer中
  161. buffer.put(bytes);
  162. // 重设缓冲区位置
  163. buffer.flip();
  164. // 根据需要转换为不同类型,这里转换为字符串
  165. String result = "";
  166. for (int i = 0; i < bytes.length; i++) {
  167. result += String.format("%02X", buffer.get()); // 以16进制字符串的形式拼接
  168. }
  169. return result;
  170. }
  171. public static int getBit(byte b, int n) {
  172. // 将byte转换为二进制字符串
  173. String binaryString = Integer.toBinaryString(b & 0xFF);
  174. // 补足位数到8位
  175. binaryString = String.format("%8s", binaryString).replace(' ', '0');
  176. // 获取第n位的字符,然后转换为整数
  177. return binaryString.charAt(n) == '1' ? 1 : 0;
  178. }
  179. }