autoPackingForm6156.ashx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <%@ WebHandler Language="C#" Class="autoPackingForm6156" %>
  2. using System;
  3. using System.Web;
  4. using System.Data;
  5. using DK.XuWei.WebMes;
  6. using Curtain.DataAccess;
  7. using Newtonsoft.Json.Linq;
  8. /// <summary>
  9. /// PLC对接使用
  10. /// 轮询处理逻辑;
  11. /// qq add 2025-08-06
  12. /// </summary>
  13. public class autoPackingForm6156 : IHttpHandler
  14. {
  15. public static class PLC
  16. {
  17. public static string plcIp = "172.18.36.95";
  18. public static int plcPort = 102;
  19. public static string dbFormID = "1000.0"; //工单ID
  20. public static string dbStackNum = "1000.4"; //码垛数量
  21. public static string dbGoodsType = "1000.6"; //产品类型 1:智能 2:连体
  22. public static string dbGoodsCode = "1000.8"; //产品型号
  23. public static string dbPlateType = "1000.10"; //托盘类型 1:铁托 2:胶托
  24. public static string dbPlateNum = "1000.12"; //产品数量
  25. public static string dbStatus = "1000.14"; //状态标识 1时读取下一条信息传递
  26. }
  27. public bool resultFlag = false;
  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. resultFlag = true;
  37. result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接成功!")));
  38. }
  39. catch (Exception e)
  40. {
  41. resultFlag = false;
  42. result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接失败!")));
  43. result.Add(new JObject(new JProperty("PLC错误信息", e.Message)));
  44. }
  45. return 1;
  46. }
  47. //主程序入口
  48. public void ProcessRequest(HttpContext context)
  49. {
  50. context.Response.ContentType = "text/plain";
  51. BarcodeDo(context);
  52. }
  53. // 轮询到标识
  54. public void BarcodeDo(HttpContext context)
  55. {
  56. if (PlcOpen() == 1)
  57. {
  58. int readFlag = PlcReadReadFlag();
  59. //标识等于1时,代表上一条数据 PLC 已保存完成
  60. if(readFlag == 1)
  61. {
  62. //读取包装工单 ID
  63. int formID = PlcReadFormID();
  64. if (formID > 0)
  65. {
  66. //MES 更新该条工单的同步状态;
  67. int updateFlag = UpdateForm(formID);
  68. //时根据包装线及执行顺序给 PLC 传输下一条的工单信息,更新标识位为 0,
  69. if (updateFlag > 0)
  70. {
  71. DataRow dr = GetForms();
  72. //传输数据
  73. if (dr != null)
  74. {
  75. PlcWriteRepeat<int>(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID
  76. PlcWriteRepeat<short>(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量
  77. PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体
  78. PlcWriteRepeat<string>(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号
  79. PlcWriteRepeat<short>(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托
  80. PlcWriteRepeat<short>(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量
  81. }
  82. result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbFormID + " = " + s7.Read<int>(PLC.dbFormID) + ";" + PLC.dbStackNum +
  83. " = " + s7.Read<short>(PLC.dbStackNum) + ";" + PLC.dbGoodsType + " = " + s7.Read<short>(PLC.dbGoodsType)+ ";" + PLC.dbGoodsCode +
  84. " = " + s7.Read<short>(PLC.dbGoodsCode) + ";"+ PLC.dbPlateType + " = " + s7.Read<short>(PLC.dbPlateType) +";" + PLC.dbPlateNum +
  85. " = " + s7.Read<short>(PLC.dbPlateNum) + ";")));
  86. //========================================
  87. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  88. }
  89. }
  90. else
  91. {
  92. DataRow dr = GetForms();
  93. //传输数据
  94. if (dr != null)
  95. {
  96. PlcWriteRepeat<int>(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID
  97. PlcWriteRepeat<short>(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量
  98. PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体
  99. PlcWriteRepeat<string>(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号
  100. PlcWriteRepeat<short>(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托
  101. PlcWriteRepeat<short>(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量
  102. }
  103. result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbFormID + " = " + s7.Read<int>(PLC.dbFormID) + ";" + PLC.dbStackNum +
  104. " = " + s7.Read<short>(PLC.dbStackNum) + ";" + PLC.dbGoodsType + " = " + s7.Read<short>(PLC.dbGoodsType)+ ";" + PLC.dbGoodsCode +
  105. " = " + s7.Read<short>(PLC.dbGoodsCode) + ";"+ PLC.dbPlateType + " = " + s7.Read<short>(PLC.dbPlateType) +";" + PLC.dbPlateNum +
  106. " = " + s7.Read<short>(PLC.dbPlateNum) + ";")));
  107. //========================================
  108. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  109. }
  110. }
  111. }
  112. context.Response.Write(new JsonResult(resultFlag ? JsonStatus.success : JsonStatus.error) { rows = result }.ToJson());
  113. }
  114. //读取PLC标识
  115. public int PlcReadReadFlag()
  116. {
  117. if (!resultFlag) return -1;
  118. try
  119. {
  120. int read = Convert.ToInt16(s7.Read<short>(PLC.dbStatus));
  121. result.Add(new JObject(new JProperty("PLC读取(读取标识:OK)", PLC.dbStatus + " = " + read.ToString())));
  122. return read;
  123. }
  124. catch (Exception e)
  125. {
  126. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  127. resultFlag = false;
  128. return -1;
  129. }
  130. }
  131. //读取包装工单ID
  132. public int PlcReadFormID()
  133. {
  134. if (!resultFlag) return 0;
  135. try
  136. {
  137. int formID = Convert.ToInt32(s7.Read<int>(PLC.dbFormID));
  138. result.Add(new JObject(new JProperty("PLC读取(工单ID:OK)", PLC.dbFormID + " = " + formID.ToString())));
  139. return formID;
  140. }
  141. catch (Exception e)
  142. {
  143. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  144. resultFlag = false;
  145. return 0;
  146. }
  147. }
  148. //写入执行结果
  149. // 1 通过 2 条码格式错误 3 产品不可到达 4 MES异常 5 条码不一致 6 绑板完成 7 工号读取错误
  150. //public int PlcWriteResult(Int16 flag)
  151. //{
  152. // try
  153. // {
  154. // string message = "";
  155. // switch(flag)
  156. // {
  157. // case 1:message = "扫码通过:OK";break;
  158. // case 2:message = "工单信息错误:ERR";break;
  159. // case 3:message = "MES异常:ERR";break;
  160. // default:message = "未定义:ERR";break;
  161. // }
  162. // s7.Write<short>(PLC.dbStatus, flag);
  163. // result.Add(new JObject(new JProperty("PLC写入("+message+")", PLC.dbStatus + " = " + flag.ToString())));
  164. // return 1;
  165. // }
  166. // catch (Exception e)
  167. // {
  168. // result.Add(new JObject(new JProperty("PLC错误信息:ERROR", e.Message)));
  169. // return -1;
  170. // }
  171. //}
  172. public int UpdateForm(int packingformid)
  173. {
  174. try
  175. {
  176. using (IDataAccess conn = DataAccess.Create())
  177. {
  178. int results = conn.ExecuteNonQuery(@"
  179. update TP_MST_PACKINGFORM set sycnflag = 1 WHERE packingline = 1 and packingformid = @PACKINGFORMID@
  180. ",
  181. new CDAParameter("PACKINGFORMID", packingformid)
  182. );
  183. if(results > 0 )
  184. {
  185. result.Add(new JObject(new JProperty("MES更新(同步状态:OK)", "")));
  186. return results;
  187. }
  188. else
  189. {
  190. resultFlag = false;
  191. result.Add(new JObject(new JProperty("MES更新(同步状态:ERR)","未更新!")));
  192. return 0;
  193. }
  194. }
  195. }
  196. catch (Exception e)
  197. {
  198. resultFlag = false;
  199. result.Add(new JObject(new JProperty("系统异常", e.Message)));
  200. return 0;
  201. }
  202. }
  203. public DataRow GetForms()
  204. {
  205. try
  206. {
  207. using (IDataAccess conn = DataAccess.Create())
  208. {
  209. DataTable dt = conn.ExecuteDatatable(@"
  210. SELECT
  211. pf.packingformid FORMID,
  212. g.SEATCOVERCODE goodscode,
  213. pf.materialcode,
  214. d1.dictionaryvalue stacknum,
  215. CASE WHEN G.GOODSTYPEID = 18 THEN 1
  216. WHEN G.GOODSTYPEID = 3 THEN 2
  217. ELSE 3 END goodstypecode,
  218. CASE WHEN d.dictionaryvalue = '铁托' THEN 1
  219. WHEN d.dictionaryvalue = '胶托' THEN 2 ELSE 3 END platetype,
  220. pf.platenum
  221. FROM
  222. TP_MST_PACKINGFORM pf
  223. left join tp_mst_goods g on g.goodsid = pf.goodsid
  224. left join tp_mst_datadictionary d on pf.platetype = d.dictionaryid and d.dictionarytype = 'TPC031'
  225. left join tp_mst_datadictionary d1 on pf.stacknum = d1.dictionaryid and d1.dictionarytype = 'TPC032'
  226. WHERE
  227. pf.packingline = '1'
  228. and pf.sycnflag = 0
  229. and pf.stacknum is not null
  230. and pf.platetype is not null
  231. ORDER BY PF.CREATETIME ,PF.ORDERNO
  232. "
  233. );
  234. if(dt.Rows.Count > 0 )
  235. {
  236. JObject obj = new JObject();
  237. obj.Add(new JProperty("FORMID", dt.Rows[0]["FORMID"].ToString()));
  238. obj.Add(new JProperty("PLATENUM", dt.Rows[0]["platenum"].ToString()));
  239. obj.Add(new JProperty("STACKNUM", dt.Rows[0]["stacknum"].ToString()));
  240. obj.Add(new JProperty("PLATETYPE", dt.Rows[0]["platetype"].ToString()));
  241. obj.Add(new JProperty("GOODSTYPE", dt.Rows[0]["goodstypecode"].ToString()));
  242. obj.Add(new JProperty("GOODSCODE", dt.Rows[0]["goodscode"].ToString()));
  243. result.Add(new JObject(new JProperty("MES读取(工单信息:OK)", obj)));
  244. return dt.Rows[0];
  245. }
  246. else
  247. {
  248. resultFlag = false;
  249. result.Add(new JObject(new JProperty("MES读取(工单信息:ERR)","工单未找到!")));
  250. return null;
  251. }
  252. }
  253. }
  254. catch (Exception e)
  255. {
  256. resultFlag = false;
  257. result.Add(new JObject(new JProperty("系统异常", e.Message)));
  258. return null;
  259. }
  260. }
  261. public bool PlcWriteRepeat<T>(string db,object value,int repeat = 5)
  262. {
  263. bool isSuccess = false;
  264. isSuccess = s7.Write<T>(db, (T)(value));
  265. result.Add(new JObject(new JProperty("PLC写入数据:", db + "=" + value.ToString())));
  266. int i = 0;
  267. while ( !isSuccess && i<repeat )
  268. {
  269. s7.IS_OPEN = false;
  270. isSuccess = s7.Write<T>(db, (T)(value));
  271. result.Add(new JObject(new JProperty("PLC写入数据(重写第" + i.ToString() + "次):", db + "=" + value.ToString())));
  272. i++;
  273. }
  274. return isSuccess;
  275. }
  276. public bool IsReusable
  277. {
  278. get
  279. {
  280. return false;
  281. }
  282. }
  283. }