plc_chengjianfl3125.ashx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <%@ WebHandler Language="C#" Class="plc_chengjianfl3125" %>
  2. using System;
  3. using System.Web;
  4. using DK.XuWei.WebMes;
  5. using Curtain.DataAccess;
  6. using Curtain.Log;
  7. using System.Data;
  8. using Newtonsoft.Json.Linq;
  9. /// <summary>
  10. /// 成检分流
  11. /// qq
  12. /// 2024-07-26
  13. /// </summary>
  14. public class plc_chengjianfl3125 : IHttpHandler
  15. {
  16. public static class PLC
  17. {
  18. public static string plcIp = "172.19.26.136";
  19. public static int plcPort = 102;
  20. public static string dbResult = "6.0"; //结果;扫码结果(1:通过,2:条码错误,3:产品不可到达,4:MES异常,0:复位)
  21. public static string dbGoodsCode = "6.2";//产品编码(数值写入,产品型号)
  22. public static string dbGoodsType = "6.4";//产品类型(1:智能、2:连体)
  23. public static string dbGoodsSpec = "6.6";//出水距(1:水距305,2:水距400.3:220)
  24. public static string dbBarcode = "6.10"; //产品条码
  25. }
  26. //输入参数 【条码】
  27. public string barCode = "";
  28. public JArray result = new JArray();
  29. public static S7 s7 = new S7();
  30. //打开PLC
  31. public int PlcOpen()
  32. {
  33. try
  34. {
  35. s7.Open(PLC.plcIp, PLC.plcPort);
  36. result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接成功!")));
  37. }
  38. catch (Exception e)
  39. {
  40. result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接失败!")));
  41. result.Add(new JObject(new JProperty("PLC错误信息", e.Message)));
  42. }
  43. return 1;
  44. }
  45. //关闭PLC
  46. public int PlcClose()
  47. {
  48. s7.Close();
  49. result.Add(new JObject(new JProperty("PLC关闭", PLC.plcIp + ":" + PLC.plcPort + " 关闭成功!")));
  50. return 1;
  51. }
  52. //主程序入口
  53. public void ProcessRequest(HttpContext context)
  54. {
  55. context.Response.ContentType = "text/plain";
  56. //输入产品条码参数
  57. barCode = context.Request["barcode"] is object ? context.Request["barcode"].ToString() : "";
  58. DataTable infoDt = new DataTable();
  59. //写入PLC
  60. //正式
  61. if (context.Request["barcode"] is object)
  62. {
  63. //string barCode = context.Request["barcode"].ToString();
  64. if (PlcOpen() == 1)
  65. {
  66. DataTable goodsType = GetGoodinfo(barCode);
  67. if (goodsType != null && goodsType.Rows.Count > 0)
  68. {
  69. bool goodscodeflag = PlcWriteRepeat<short>(PLC.dbGoodsCode, Convert.ToInt16(goodsType.Rows[0]["SEATCOVERCODE"].ToString()));
  70. bool goodstypeflag = PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(goodsType.Rows[0]["GOODSTYPENAME"].ToString()));
  71. bool goodsSpecflag = PlcWriteRepeat<short>(PLC.dbGoodsSpec, Convert.ToInt16(goodsType.Rows[0]["GOODSSPECIFICATION"].ToString()));
  72. bool barcodeflag = PlcWriteRepeat<string>(PLC.dbBarcode, barCode);
  73. bool resultflag = PlcWriteRepeat<short>(PLC.dbResult, Convert.ToInt16(1));
  74. result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbResult + " = " + s7.Read<short>(PLC.dbResult) + ";" + PLC.dbGoodsCode + " = " + s7.Read<short>(PLC.dbGoodsCode) + ";"+ PLC.dbGoodsType + " = " + s7.Read<short>(PLC.dbGoodsType) + ";" + PLC.dbBarcode + " = " + s7.Read<string>(PLC.dbBarcode, 11))));
  75. }
  76. else
  77. {
  78. s7.Write<short>(PLC.dbResult, Convert.ToInt16(2));
  79. }
  80. }
  81. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  82. }
  83. else
  84. {
  85. context.Response.Write(new JsonResult(JsonStatus.error) { rows = new JObject(new JProperty("MES读取(条码信息:ERR)", "无条码")) }.ToJson());
  86. }
  87. //SiemensS7.Close();
  88. }
  89. //xuwei add begin 2024-01-19 增加多次写入容错方法===============
  90. public bool PlcWriteRepeat<T>(string db,object value,int repeat = 5)
  91. {
  92. try
  93. {
  94. bool isSuccess = false;
  95. isSuccess = s7.Write<T>(db, (T)(value));
  96. result.Add(new JObject(new JProperty("PLC写入数据成功标识:", isSuccess)));
  97. result.Add(new JObject(new JProperty("PLC写入数据:", db + "=" + value.ToString())));
  98. int i = 0;
  99. while ( !isSuccess && i<repeat )
  100. {
  101. s7.IS_OPEN = false;
  102. isSuccess = s7.Write<T>(db, (T)(value));
  103. result.Add(new JObject(new JProperty("PLC写入数据(重写第" + i.ToString() + "次):", db + "=" + value.ToString())));
  104. i++;
  105. }
  106. return isSuccess;
  107. }
  108. catch (Exception ex )
  109. {
  110. result.Add(new JObject(new JProperty("系统异常:ERROR", ex.Message)));
  111. throw ex ;
  112. }
  113. }
  114. //读取产品信息
  115. public DataTable GetGoodinfo(string barcode)
  116. {
  117. try
  118. {
  119. using (IDataAccess conn = DataAccess.Create())
  120. {
  121. DataTable dt = conn.ExecuteDatatable(@"
  122. SELECT G.SEATCOVERCODE
  123. ,CASE
  124. WHEN INSTR(GT.GOODSTYPENAME, '智能') = 1 THEN 1
  125. WHEN INSTR(GT.GOODSTYPENAME, '连体') = 1 THEN 2
  126. END AS GOODSTYPENAME
  127. ,GDD.BARCODE
  128. ,G.GOODSCODE
  129. ,CASE
  130. WHEN INSTR(G.GOODSSPECIFICATION, '305') = 1 THEN 1
  131. WHEN INSTR(G.GOODSSPECIFICATION, '400') = 1 THEN 2
  132. WHEN INSTR(G.GOODSSPECIFICATION, '220') = 1 THEN 3
  133. WHEN INSTR(G.GOODSSPECIFICATION, '横排') = 1 THEN 4
  134. END AS GOODSSPECIFICATION
  135. FROM TP_PM_GROUTINGDAILYDETAIL GDD
  136. LEFT JOIN TP_MST_GOODS G
  137. ON GDD.GOODSID = G.GOODSID
  138. LEFT JOIN TP_MST_GOODSTYPE GT
  139. ON GT.GOODSTYPEID = G.GOODSTYPEID
  140. WHERE GDD.BARCODE = @BARCODE@
  141. ",
  142. new CDAParameter("BARCODE", barcode)
  143. );
  144. if (dt.Rows.Count > 0)
  145. {
  146. result.Add(new JObject(new JProperty("MES读取(产品信息:OK)", barcode + ";产品类型:" + dt.Rows[0]["GOODSTYPENAME"].ToString())));
  147. return dt;
  148. }
  149. else
  150. {
  151. result.Add(new JObject(new JProperty("MES读取(用户信息:ERR)", "产品未找到!")));
  152. return null;
  153. }
  154. }
  155. }
  156. catch (Exception e)
  157. {
  158. result.Add(new JObject(new JProperty("系统异常:ERROR", e.Message)));
  159. return null;
  160. }
  161. }
  162. public bool IsReusable
  163. {
  164. get
  165. {
  166. return false;
  167. }
  168. }
  169. }