PLCUtils.java 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.mes.util;
  2. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  3. import com.mes.ui.MesClient;
  4. public class PLCUtils {
  5. // 写入允许启动方法
  6. public static boolean writeStartMethod(S7PLC plc) {
  7. // 对plc写入ture
  8. plc.writeBoolean("V90.0",true);
  9. return true;
  10. }
  11. // 将允许启动置空
  12. public static boolean writeStopMethod(S7PLC plc) {
  13. plc.writeBoolean("V90.0",false);
  14. return true;
  15. }
  16. // 读取PLC结束加工信号
  17. public static boolean readStopMethod(S7PLC plc) {
  18. return plc.readBoolean("M4.0");
  19. }
  20. // 获取参数
  21. public static QmParam getParameter(S7PLC plc) {
  22. // TODO
  23. String leakageValue = "0.0";
  24. // 结果
  25. String result = getResult(plc);
  26. // 检测压
  27. String pressure = plc.readFloat32("VD852")+"";
  28. // 预充时间
  29. String preChargeTime = plc.readInt16("VW2048")+"";
  30. // 充气时间
  31. String chargeTime = Math.round(plc.readInt16("VW504") / 10.0) + "";
  32. // 平衡时间
  33. String balanceTime = Math.round(plc.readInt16("VW506") / 10.0) + "";
  34. // 检测时间
  35. String detectTime = Math.round(plc.readInt16("VW508") / 10.0) + "";
  36. //循环时间
  37. String loopTime = Math.round(plc.readInt16("VW850") / 10.0) + "";
  38. QmParam qmParam = new QmParam();
  39. qmParam.setLeakRateUnit("kPa");
  40. qmParam.setLeakValUnit("kPa");
  41. qmParam.setTestPressure(pressure);
  42. qmParam.setTestPressureUnit("kPa");
  43. qmParam.setTestResult(result);
  44. qmParam.setCqDuration(chargeTime);
  45. qmParam.setByDuration(balanceTime);
  46. qmParam.setCsDuration(detectTime);
  47. qmParam.setDeviceType(MesClient.csyq);
  48. qmParam.setTestPace(loopTime);
  49. long start = System.currentTimeMillis();
  50. final long MAX_WAIT = 5000;
  51. while (true){
  52. leakageValue = plc.readString("VB307", 7);
  53. // 条件1:读取到非空,直接跳出循环
  54. if (leakageValue != null && !leakageValue.trim().isEmpty()) {
  55. break;
  56. }
  57. long now = System.currentTimeMillis();
  58. if (now - start >= MAX_WAIT) {
  59. break;
  60. }
  61. try {
  62. Thread.sleep(50);
  63. } catch (InterruptedException e) {
  64. break;
  65. }
  66. }
  67. qmParam.setLeakVal(leakageValue);
  68. return qmParam;
  69. }
  70. public static String getResult(S7PLC plc){
  71. byte data = plc.readByte("VB503");
  72. switch (data) {
  73. case 2:
  74. return "OK";
  75. case 3:
  76. return "NG";
  77. default:
  78. return "未知状态";
  79. }
  80. }
  81. }