S7Util.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. package com.mes.ui;
  2. import com.github.s7connector.api.DaveArea;
  3. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  4. import java.math.BigInteger;
  5. import java.text.DecimalFormat;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. public class S7Util {
  9. public static Integer db = 9014; // FSW=9014 CNC=9015
  10. // 监听状态
  11. public static void getDeviceState(){
  12. try {
  13. Boolean deviceStatus = getDeviceStatus();
  14. if(MesClient.tjFlag == 1) {
  15. MesClient.tjTime++;
  16. if (!deviceStatus) {
  17. MesClient.tjFlag = 2;
  18. MesClient.status_menu.setText(MesClient.tjFlagText2);
  19. }
  20. if (deviceStatus || MesClient.tjTime >= 120) { // 120秒内去除MES允许
  21. sendStartSignal(false);
  22. }
  23. }else if(MesClient.tjFlag == 2){
  24. MesClient.tjTime++;
  25. if(deviceStatus || MesClient.tjTime >= 380){
  26. MesClient.tjFlag = 3;
  27. MesClient.status_menu.setText(MesClient.tjFlagText3);
  28. }
  29. }else if(MesClient.tjFlag == 3) {
  30. DataUtil.sendQuality(MesClient.nettyClient,MesClient.product_sn.getText(),"OK",MesClient.user20);
  31. if (!deviceStatus) {
  32. //禁止启动
  33. sendStartSignal(true);
  34. }
  35. }else{
  36. //未扫码 如果程序没结束 禁止设备
  37. if(!deviceStatus){
  38. sendStartSignal(true);
  39. }
  40. }
  41. }catch (Exception e){
  42. e.printStackTrace();
  43. }
  44. }
  45. //屏蔽mes交互 如果设备禁止启动 则启动设备
  46. public static void getDeviceShield(){
  47. try {
  48. Boolean deviceStart = getDeviceStart();
  49. if (deviceStart){
  50. sendStartSignal(false);
  51. }
  52. }catch (Exception e){
  53. e.printStackTrace();
  54. }
  55. }
  56. // 监听急停
  57. public static void checkStop(){
  58. try {
  59. MesClient.initS7();
  60. byte[] bytes = MesClient.s7Connector.read(DaveArea.DB, 9052, 1, 0);
  61. // System.out.println("DB9052.DBX0:"+byteToBinaryString(bytes));
  62. int jtstatus = getBit(bytes[0],7);
  63. if(jtstatus == 0){
  64. switchEnable(0);
  65. }
  66. if(MesClient.tjFlag == 2 && jtstatus == 0){
  67. MesClient.tjResult = "NG";
  68. MesClient.finish_ok_bt.setEnabled(true);
  69. MesClient.finish_ng_bt.setEnabled(true);
  70. }
  71. }catch (Exception e){
  72. e.printStackTrace();
  73. }
  74. }
  75. public static int getBit(byte b, int n) {
  76. // 将byte转换为二进制字符串
  77. String binaryString = Integer.toBinaryString(b & 0xFF);
  78. // 补足位数到8位
  79. binaryString = String.format("%8s", binaryString).replace(' ', '0');
  80. // 获取第n位的字符,然后转换为整数
  81. return binaryString.charAt(n) == '1' ? 1 : 0;
  82. }
  83. private static String byteToBinaryString(byte[] byteArray) {
  84. StringBuilder binaryString = new StringBuilder();
  85. for (byte b : byteArray) {
  86. binaryString.append(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'));
  87. }
  88. return binaryString.toString();
  89. }
  90. // 转大端排序
  91. private static byte[] xtob( byte[] bytes){
  92. // 创建新的byte数组并复制原始数据
  93. byte[] reversedBytes = new byte[bytes.length];
  94. System.arraycopy(bytes, 0, reversedBytes, 0, bytes.length);
  95. // 反转byte数组元素的位置
  96. for (int i = 0; i < bytes.length / 2; i++) {
  97. byte temp = reversedBytes[i];
  98. reversedBytes[i] = reversedBytes[reversedBytes.length - 1 - i];
  99. reversedBytes[reversedBytes.length - 1 - i] = temp;
  100. }
  101. return reversedBytes;
  102. }
  103. // 格式化s7取出的值
  104. private static String formatS7Val(byte[] val){
  105. int intValue = new BigInteger(xtob(val)).intValue();
  106. float floatValue = Float.intBitsToFloat(intValue);
  107. // 保留1位小数
  108. DecimalFormat df = new DecimalFormat("#.#");
  109. df.setMaximumFractionDigits(1);
  110. String result = df.format(floatValue);
  111. float roundedNum = Float.parseFloat(result);
  112. return roundedNum+"";
  113. }
  114. private static Integer formatS7Int(byte[] val){
  115. return new BigInteger(xtob(val)).intValue();
  116. }
  117. private static Float formatS7Float(byte[] val){
  118. int intValue = new BigInteger(xtob(val)).intValue();
  119. float floatValue = Float.intBitsToFloat(intValue);
  120. // 保留1位小数
  121. DecimalFormat df = new DecimalFormat("#.#");
  122. df.setMaximumFractionDigits(1);
  123. String result = df.format(floatValue);
  124. float roundedNum = Float.parseFloat(result);
  125. return roundedNum;
  126. }
  127. // 禁止/启用启动
  128. public static Boolean switchEnable(int state){
  129. try{
  130. MesClient.initS7();
  131. byte[] bytes2 = new byte[1];
  132. if(state == 1){ //启用启动
  133. bytes2[0] = 0x01;
  134. }else{ // 禁止启动
  135. bytes2[0] = 0x00;
  136. }
  137. MesClient.s7Connector.write(DaveArea.DB,9052,0,bytes2);
  138. return true;
  139. }catch (Exception e){
  140. e.printStackTrace();
  141. return false;
  142. }
  143. }
  144. // 获取允许运行状态
  145. public static Integer getSwitchEnable(){
  146. try{
  147. MesClient.initS7();
  148. //DB9052.DBB0
  149. Integer ss = new BigInteger(xtob(MesClient.s7Connector.read(DaveArea.DB, 9052, 1, 0))).intValue();
  150. return ss;
  151. }catch (Exception e){
  152. e.printStackTrace();
  153. return 0;
  154. }
  155. }
  156. // 获取设备运行状态(是否结束) DB9015.DBX0.0 =1 运行结束,true =0 运行中,false
  157. public static Boolean getDeviceStatus() {
  158. try {
  159. MesClient.initS7();
  160. // 读取 DB9015.DBB0(1字节,包含 DBX0.0~DBX0.7) 1个字节,从第0个字节开始
  161. byte[] data9015 = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 0);
  162. byte b9015 = data9015[0];
  163. Boolean result = (b9015 & (1 << 0)) != 0;
  164. return result;
  165. } catch (Exception e) {
  166. e.printStackTrace();
  167. }
  168. return false;
  169. }
  170. //XY数据
  171. // 获取报警状态(是否报警) DB9015.DBX0.2 =1 报警,true =0 没有报警,false
  172. public static Boolean getDeviceAlarm() {
  173. try {
  174. MesClient.initS7();
  175. // 读取 DB9015.DBB0(1字节,包含 DBX0.0~DBX0.7) 1个字节,从第0个字节开始
  176. byte[] data9015 = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 0);
  177. byte b9015 = data9015[0];
  178. Boolean result = (b9015 & (1 << 2)) != 0;
  179. return result;
  180. } catch (Exception e) {
  181. e.printStackTrace();
  182. }
  183. return false;
  184. }
  185. // 获取设备允许启动信号 DB9015.DBX0.1 =0 允许启动,true =1 禁止启动,false
  186. public static Boolean getDeviceStart() {
  187. try {
  188. MesClient.initS7();
  189. // 读取 DB9015.DBB0(1字节,包含 DBX0.0~DBX0.7) 1个字节,从第0个字节开始
  190. byte[] data9015 = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 0);
  191. byte b9015 = data9015[0];
  192. Boolean result = (b9015 & (1 << 1)) != 0;
  193. return result;
  194. } catch (Exception e) {
  195. e.printStackTrace();
  196. }
  197. return false;
  198. }
  199. // 获取设备允许启动信号 DB9000.DBX3.2 =0 MES启动,true =1 禁止启动,false
  200. public static Boolean getMesStatus() {
  201. try {
  202. MesClient.initS7();
  203. byte[] data9000 = MesClient.s7Connector.read(DaveArea.DB, 9000, 1, 3);
  204. byte b9000 = data9000[0];
  205. Boolean result = (b9000 & (1 << 2)) != 0;
  206. return result;
  207. } catch (Exception e) {
  208. e.printStackTrace();
  209. }
  210. return false;
  211. }
  212. // 发送设备允许启动信号DB9000.DBX3.2 =0 MES开启,true =1 MES屏蔽,false
  213. public static boolean sendMesStatus(boolean value){
  214. try {
  215. MesClient.initS7();
  216. byte[] data = MesClient.s7Connector.read(DaveArea.DB, 9000, 1, 3);
  217. byte b = data[0];
  218. String binary = String.format("%8s", Integer.toBinaryString(b & 0xFF))
  219. .replace(' ', '0');
  220. // 输出"11110110"
  221. System.out.println("s:"+binary);
  222. int s = getBit(b,2);
  223. int ss = getBit(b,4);
  224. System.out.println("s:"+s);
  225. System.out.println("ss:"+ss);
  226. if (value) {
  227. b = (byte)(b | (1 << 2)); // 置1
  228. } else {
  229. b = (byte)(b & ~(1 << 2)); // 清0
  230. }
  231. // 写回去
  232. byte[] toWrite = new byte[]{b};
  233. byte c = toWrite[0];
  234. String binary2 = String.format("%8s", Integer.toBinaryString(c & 0xFF))
  235. .replace(' ', '0');
  236. // 输出"11110110"
  237. System.out.println("s:"+binary2);
  238. MesClient.s7Connector.write(DaveArea.DB, 9000, 3, toWrite);
  239. return true;
  240. } catch (Exception e) {
  241. e.printStackTrace();
  242. }
  243. return false;
  244. }
  245. //读取 测试
  246. public static Map<String, Boolean> readBits() {
  247. Map<String, Boolean> result = new HashMap<>();
  248. try {
  249. MesClient.initS7();
  250. // 读取 DB9015.DBB0(1字节,包含 DBX0.0~DBX0.7) 1个字节,从第0个字节开始
  251. byte[] data9015 = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 0);
  252. byte b9015 = data9015[0];
  253. result.put("DB9015.DBX0.0", (b9015 & (1 << 0)) != 0);
  254. result.put("DB9015.DBX0.1", (b9015 & (1 << 1)) != 0);
  255. result.put("DB9015.DBX0.2", (b9015 & (1 << 2)) != 0);
  256. // 读取 DB9000.DBB3(1字节,包含 DBX3.0~DBX3.7)
  257. // byte[] data9000 = MesClient.s7Connector.read(DaveArea.DB, 9000, 1, 3);
  258. // byte b9000 = data9000[0];
  259. //
  260. // result.put("DB9000.DBX3.2", (b9000 & (1 << 2)) != 0);
  261. } catch (Exception e) {
  262. e.printStackTrace();
  263. }
  264. return result;
  265. }
  266. // 发送设备允许启动信号 DB9015.DBX0.1 =0 允许启动,true =1 禁止启动,false
  267. public static boolean sendStartSignal(boolean value){
  268. try {
  269. MesClient.initS7();
  270. byte[] data = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 0);
  271. byte b = data[0];
  272. System.out.println(b);
  273. if (value) {
  274. b = (byte)(b | (1 << 1)); // 置1
  275. } else {
  276. b = (byte)(b & ~(1 << 1)); // 清0
  277. }
  278. System.out.println("b:"+b);
  279. // b = 1;
  280. // 写回去
  281. byte[] toWrite = new byte[]{b};
  282. System.out.println(toWrite.toString());
  283. MesClient.s7Connector.write(DaveArea.DB, S7Util.db, 0, toWrite);
  284. return true;
  285. } catch (Exception e) {
  286. e.printStackTrace();
  287. }
  288. return false;
  289. }
  290. //写入 测试
  291. public static boolean writeBit(int dbNumber, int byteIndex, int bitIndex, boolean value) {
  292. try {
  293. MesClient.initS7();
  294. // 先读出对应字节
  295. byte[] data = MesClient.s7Connector.read(DaveArea.DB, dbNumber, 1, byteIndex);
  296. byte b = data[0];
  297. // 修改指定位
  298. if (value) {
  299. b = (byte)(b | (1 << bitIndex)); // 置1
  300. } else {
  301. b = (byte)(b & ~(1 << bitIndex)); // 清0
  302. }
  303. // 写回去
  304. byte[] toWrite = new byte[]{b};
  305. MesClient.s7Connector.write(DaveArea.DB, dbNumber, byteIndex, toWrite);
  306. return true;
  307. } catch (Exception e) {
  308. e.printStackTrace();
  309. return false;
  310. }
  311. }
  312. // 获取运行的程序名
  313. public static String getProgramName(S7PLC s7PLC){
  314. String programName = "";
  315. try{
  316. programName = s7PLC.readProgramName();
  317. }catch (Exception e){
  318. e.printStackTrace();
  319. programName = "";
  320. }
  321. return programName;
  322. }
  323. // 读取主轴转速 - 传入s7PLC参数
  324. public static double readActSpindleSpeed(S7PLC s7PLC){
  325. try {
  326. return s7PLC.readActSpindleSpeed();
  327. } catch (Exception e) {
  328. return 0;
  329. }
  330. }
  331. // 读取进给速度 - DB9050.DBD32 (4字节浮点数)
  332. public static double readFeedRate(S7PLC s7PLC){
  333. try {
  334. MesClient.initS7();
  335. byte[] data = MesClient.s7Connector.read(DaveArea.DB, 9050, 4, 32);
  336. return formatS7Float(data);
  337. } catch (Exception e) {
  338. return 0;
  339. }
  340. }
  341. // 读取扭矩值 - DB4900.DBD364 (4字节浮点数)
  342. public static double readTorque(S7PLC s7PLC){
  343. try {
  344. MesClient.initS7();
  345. byte[] data = MesClient.s7Connector.read(DaveArea.DB, 4900, 4, 364);
  346. return formatS7Float(data);
  347. } catch (Exception e) {
  348. return 0;
  349. }
  350. }
  351. // DB9043.DBX120.0
  352. public static Boolean getParamFlag() {
  353. try {
  354. MesClient.initS7();
  355. // 读取 DB9043.DBB0(1字节,包含 DBX0.0~DBX0.7) 1个字节,从第0个字节开始
  356. byte[] data9015 = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 120);
  357. byte b9015 = data9015[0];
  358. Boolean result = (b9015 & (1 << 0)) != 0;
  359. return result;
  360. } catch (Exception e) {
  361. e.printStackTrace();
  362. }
  363. return false;
  364. }
  365. // 读取X位移 - DB5700.DBD0 (4字节浮点数)
  366. public static double readXPosition(){
  367. try {
  368. MesClient.initS7();
  369. byte[] data = MesClient.s7Connector.read(DaveArea.DB, 5700, 4, 0);
  370. return formatS7Float(data);
  371. } catch (Exception e) {
  372. return 0;
  373. }
  374. }
  375. // 读取Y位移 - DB5701.DBD0 (4字节浮点数)
  376. public static double readYPosition(){
  377. try {
  378. MesClient.initS7();
  379. byte[] data = MesClient.s7Connector.read(DaveArea.DB, 5701, 4, 0);
  380. return formatS7Float(data);
  381. } catch (Exception e) {
  382. return 0;
  383. }
  384. }
  385. }