autoPackingForm6157.ashx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <%@ WebHandler Language="C#" Class="autoPackingForm6157" %>
  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 autoPackingForm6157 : 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. public static string dbGoodsCodes = "1000.18"; //产品型号 strig
  27. }
  28. public bool resultFlag = false;
  29. public JArray result = new JArray();
  30. public static S7 s7 = new S7();
  31. //打开PLC
  32. public int PlcOpen()
  33. {
  34. try
  35. {
  36. s7.Open(PLC.plcIp, PLC.plcPort);
  37. resultFlag = true;
  38. result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接成功!")));
  39. }
  40. catch (Exception e)
  41. {
  42. resultFlag = false;
  43. result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接失败!")));
  44. result.Add(new JObject(new JProperty("PLC错误信息", e.Message)));
  45. }
  46. return 1;
  47. }
  48. //关闭PLC
  49. public int PlcClose()
  50. {
  51. s7.Close();
  52. result.Add(new JObject(new JProperty("PLC关闭", PLC.plcIp + ":" + PLC.plcPort + " 关闭成功!")));
  53. return 1;
  54. }
  55. //主程序入口
  56. public void ProcessRequest(HttpContext context)
  57. {
  58. context.Response.ContentType = "text/plain";
  59. BarcodeDo(context);
  60. }
  61. // 轮询到标识
  62. public void BarcodeDo(HttpContext context)
  63. {
  64. if (PlcOpen() == 1)
  65. {
  66. int readFlag = PlcReadReadFlag();
  67. //标识等于1时,代表上一条数据 PLC 已保存完成
  68. if(readFlag == 1)
  69. {
  70. //读取包装工单 ID
  71. int formID = PlcReadFormID();
  72. if (formID > 0)
  73. {
  74. //MES 更新该条工单的同步状态;
  75. int updateFlag = UpdateForm(formID);
  76. //时根据包装线及执行顺序给 PLC 传输下一条的工单信息,更新标识位为 0,
  77. if (updateFlag > 0)
  78. {
  79. DataRow dr = GetForms(formID);
  80. //传输数据
  81. if (dr != null)
  82. {
  83. PlcWriteRepeat<int>(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID
  84. PlcWriteRepeat<short>(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量
  85. PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体
  86. PlcWriteRepeat<short>(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托
  87. PlcWriteRepeat<short>(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量
  88. PlcWriteRepeat<short>(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号 1000.12
  89. PlcWriteRepeat<string>(PLC.dbGoodsCodes, dr["GOODSCODES"].ToString());//产品型号 1000.18
  90. }
  91. result.Add(new JObject(new JProperty("PLC读出(核对)",
  92. "FormID["+ PLC.dbFormID + "] =" + s7.Read<int>(PLC.dbFormID) +
  93. ";StackNum[" + PLC.dbStackNum + "] = " + s7.Read<short>(PLC.dbStackNum) +
  94. ";GoodsType[" + PLC.dbGoodsType + "] = " + s7.Read<short>(PLC.dbGoodsType) +
  95. ";PlateType[" + PLC.dbPlateType + "] = " + s7.Read<short>(PLC.dbPlateType) +
  96. ";PlateNum[" + PLC.dbPlateNum + "] = " + s7.Read<short>(PLC.dbPlateNum) +
  97. ";GoodsCode[" + PLC.dbGoodsCode + "] = " + s7.Read<short>(PLC.dbGoodsCode) +
  98. ";GoodsCodes[" + PLC.dbGoodsCodes + "] = " + s7.Read<string>(PLC.dbGoodsCodes, 14)
  99. )));
  100. //========================================
  101. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  102. }
  103. else
  104. {
  105. DataRow dr = GetForms(formID);
  106. //传输数据
  107. if (dr != null)
  108. {
  109. PlcWriteRepeat<int>(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID
  110. PlcWriteRepeat<short>(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量
  111. PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体
  112. PlcWriteRepeat<short>(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托
  113. PlcWriteRepeat<short>(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量
  114. PlcWriteRepeat<short>(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号 1000.12
  115. PlcWriteRepeat<string>(PLC.dbGoodsCodes, dr["GOODSCODES"].ToString());//产品型号 1000.18
  116. }
  117. result.Add(new JObject(new JProperty("PLC读出(核对)",
  118. "FormID["+ PLC.dbFormID + "] =" + s7.Read<int>(PLC.dbFormID) +
  119. ";StackNum[" + PLC.dbStackNum + "] = " + s7.Read<short>(PLC.dbStackNum) +
  120. ";GoodsType[" + PLC.dbGoodsType + "] = " + s7.Read<short>(PLC.dbGoodsType) +
  121. ";PlateType[" + PLC.dbPlateType + "] = " + s7.Read<short>(PLC.dbPlateType) +
  122. ";PlateNum[" + PLC.dbPlateNum + "] = " + s7.Read<short>(PLC.dbPlateNum) +
  123. ";GoodsCode[" + PLC.dbGoodsCode + "] = " + s7.Read<short>(PLC.dbGoodsCode) +
  124. ";GoodsCodes[" + PLC.dbGoodsCodes + "] = " + s7.Read<string>(PLC.dbGoodsCodes, 14)
  125. )));
  126. //========================================
  127. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  128. }
  129. }
  130. else
  131. {
  132. DataRow dr = GetForms(0);
  133. //传输数据
  134. if (dr != null)
  135. {
  136. PlcWriteRepeat<int>(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID
  137. PlcWriteRepeat<short>(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量
  138. PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体
  139. PlcWriteRepeat<short>(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托
  140. PlcWriteRepeat<short>(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量
  141. PlcWriteRepeat<short>(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号 1000.12
  142. PlcWriteRepeat<string>(PLC.dbGoodsCodes, dr["GOODSCODES"].ToString());//产品型号 1000.18
  143. }
  144. result.Add(new JObject(new JProperty("PLC读出(核对)",
  145. "FormID["+ PLC.dbFormID + "] =" + s7.Read<int>(PLC.dbFormID) +
  146. ";StackNum[" + PLC.dbStackNum + "] = " + s7.Read<short>(PLC.dbStackNum) +
  147. ";GoodsType[" + PLC.dbGoodsType + "] = " + s7.Read<short>(PLC.dbGoodsType) +
  148. ";PlateType[" + PLC.dbPlateType + "] = " + s7.Read<short>(PLC.dbPlateType) +
  149. ";PlateNum[" + PLC.dbPlateNum + "] = " + s7.Read<short>(PLC.dbPlateNum) +
  150. ";GoodsCode[" + PLC.dbGoodsCode + "] = " + s7.Read<short>(PLC.dbGoodsCode) +
  151. ";GoodsCodes[" + PLC.dbGoodsCodes + "] = " + s7.Read<string>(PLC.dbGoodsCodes, 14)
  152. )));
  153. //========================================
  154. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  155. //包装工单 ID错误
  156. //PlcWriteResult(2);
  157. }
  158. }
  159. PlcClose();
  160. }
  161. context.Response.Write(System.Environment.NewLine + (new JsonResult(resultFlag ? JsonStatus.success : JsonStatus.error) { rows = result }.ToJson()));
  162. }
  163. //读取PLC标识
  164. public int PlcReadReadFlag()
  165. {
  166. if (!resultFlag) return -1;
  167. try
  168. {
  169. int read = Convert.ToInt16(s7.Read<short>(PLC.dbStatus));
  170. result.Add(new JObject(new JProperty("PLC读取(读取标识:OK)", PLC.dbStatus + " = " + read.ToString())));
  171. return read;
  172. }
  173. catch (Exception e)
  174. {
  175. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  176. resultFlag = false;
  177. return -1;
  178. }
  179. }
  180. //读取包装工单ID
  181. public int PlcReadFormID()
  182. {
  183. if (!resultFlag) return 0;
  184. try
  185. {
  186. int formID = Convert.ToInt32(s7.Read<int>(PLC.dbFormID));
  187. result.Add(new JObject(new JProperty("PLC读取(工单ID:OK)", PLC.dbFormID + " = " + formID.ToString())));
  188. return formID;
  189. }
  190. catch (Exception e)
  191. {
  192. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  193. resultFlag = false;
  194. return 0;
  195. }
  196. }
  197. //写入执行结果
  198. // 1 通过 2 条码格式错误 3 产品不可到达 4 MES异常 5 条码不一致 6 绑板完成 7 工号读取错误
  199. //public int PlcWriteResult(Int16 flag)
  200. //{
  201. // try
  202. // {
  203. // string message = "";
  204. // switch(flag)
  205. // {
  206. // case 1:message = "扫码通过:OK";break;
  207. // case 2:message = "工单信息错误:ERR";break;
  208. // case 3:message = "MES异常:ERR";break;
  209. // default:message = "未定义:ERR";break;
  210. // }
  211. // s7.Write<short>(PLC.dbStatus, flag);
  212. // result.Add(new JObject(new JProperty("PLC写入("+message+")", PLC.dbStatus + " = " + flag.ToString())));
  213. // return 1;
  214. // }
  215. // catch (Exception e)
  216. // {
  217. // result.Add(new JObject(new JProperty("PLC错误信息:ERROR", e.Message)));
  218. // return -1;
  219. // }
  220. //}
  221. public int UpdateForm(int packingformid)
  222. {
  223. try
  224. {
  225. using (IDataAccess conn = DataAccess.Create())
  226. {
  227. int results = conn.ExecuteNonQuery(@"
  228. update TP_MST_PACKINGFORM set sycnflag = 1 WHERE packingline = 2 and packingformid = @PACKINGFORMID@ and sycnflag = 0
  229. ",
  230. new CDAParameter("PACKINGFORMID", packingformid)
  231. );
  232. if(results > 0 )
  233. {
  234. result.Add(new JObject(new JProperty("MES更新(同步状态:OK)", "")));
  235. return results;
  236. }
  237. else
  238. {
  239. //resultFlag = false;
  240. result.Add(new JObject(new JProperty("MES更新(同步状态:ERR)","未更新或已更新!")));
  241. return 0;
  242. }
  243. }
  244. }
  245. catch (Exception e)
  246. {
  247. resultFlag = false;
  248. result.Add(new JObject(new JProperty("系统异常", e.Message)));
  249. return 0;
  250. }
  251. }
  252. public DataRow GetForms(int formid)
  253. {
  254. try
  255. {
  256. using (IDataAccess conn = DataAccess.Create())
  257. {
  258. string sql = null;
  259. if (formid > 0)
  260. {
  261. sql = @"
  262. SELECT
  263. pf.packingformid FORMID,
  264. g.SEATCOVERCODE GOODSCODE,
  265. pf.materialcode,
  266. d1.dictionaryvalue STACKNUM,
  267. CASE WHEN G.GOODSTYPEID = 18 THEN 1
  268. WHEN G.GOODSTYPEID = 3 THEN 2
  269. ELSE 3 END GOODSTYPE,
  270. CASE WHEN d.dictionaryvalue = '铁托' THEN 1
  271. WHEN d.dictionaryvalue = '胶托' THEN 2 ELSE 3 END PLATETYPE,
  272. pf.PLATENUM,
  273. g.GOODSCODE GOODSCODES
  274. FROM
  275. TP_MST_PACKINGFORM pf
  276. left join tp_mst_goods g on g.goodsid = pf.goodsid
  277. left join tp_mst_datadictionary d on pf.platetype = d.dictionaryid and d.dictionarytype = 'TPC031'
  278. left join tp_mst_datadictionary d1 on pf.stacknum = d1.dictionaryid and d1.dictionarytype = 'TPC032'
  279. left join V_GOODS_MATERIALCODE gg ON gg.GOODSID = pf.GOODSID AND gg.MATERIALCODE = pf.MATERIALCODE
  280. WHERE pf.packingformid = "+formid+@"
  281. ORDER BY PF.CREATETIME ,PF.ORDERNO
  282. ";
  283. /*and pf.packingline = '2'
  284. and pf.sycnflag = 0
  285. and pf.stacknum is not null
  286. and pf.platetype is not null
  287. and (pf.logoid not in(0,65) or (pf.logoid = 0 and gg.logoid <>65) )*/
  288. }
  289. else
  290. {
  291. sql = @"
  292. SELECT
  293. pf.packingformid FORMID,
  294. g.SEATCOVERCODE GOODSCODE,
  295. pf.materialcode,
  296. d1.dictionaryvalue STACKNUM,
  297. CASE WHEN G.GOODSTYPEID = 18 THEN 1
  298. WHEN G.GOODSTYPEID = 3 THEN 2
  299. ELSE 3 END GOODSTYPE,
  300. CASE WHEN d.dictionaryvalue = '铁托' THEN 1
  301. WHEN d.dictionaryvalue = '胶托' THEN 2 ELSE 3 END PLATETYPE,
  302. pf.PLATENUM,
  303. g.GOODSCODE GOODSCODES
  304. FROM
  305. TP_MST_PACKINGFORM pf
  306. left join tp_mst_goods g on g.goodsid = pf.goodsid
  307. left join tp_mst_datadictionary d on pf.platetype = d.dictionaryid and d.dictionarytype = 'TPC031'
  308. left join tp_mst_datadictionary d1 on pf.stacknum = d1.dictionaryid and d1.dictionarytype = 'TPC032'
  309. left join V_GOODS_MATERIALCODE gg ON gg.GOODSID = pf.GOODSID AND gg.MATERIALCODE = pf.MATERIALCODE
  310. WHERE
  311. pf.packingline = '2'
  312. and pf.sycnflag = 0
  313. and pf.stacknum is not null
  314. and pf.platetype is not null
  315. and (pf.logoid not in(0,65) or (pf.logoid = 0 and gg.logoid <>65) )
  316. ORDER BY PF.CREATETIME ,PF.ORDERNO
  317. ";
  318. }
  319. DataTable dt = conn.ExecuteDatatable(sql);
  320. if(dt.Rows.Count > 0 )
  321. {
  322. JObject obj = new JObject();
  323. obj.Add(new JProperty("FORMID", dt.Rows[0]["FORMID"].ToString()));
  324. obj.Add(new JProperty("PLATENUM", dt.Rows[0]["PLATENUM"].ToString()));
  325. obj.Add(new JProperty("STACKNUM", dt.Rows[0]["STACKNUM"].ToString()));
  326. obj.Add(new JProperty("PLATETYPE", dt.Rows[0]["PLATETYPE"].ToString()));
  327. obj.Add(new JProperty("GOODSTYPE", dt.Rows[0]["GOODSTYPE"].ToString()));
  328. obj.Add(new JProperty("GOODSCODE", dt.Rows[0]["GOODSCODE"].ToString()));
  329. obj.Add(new JProperty("GOODSCODES", dt.Rows[0]["GOODSCODES"].ToString()));
  330. result.Add(new JObject(new JProperty("MES读取(工单信息:OK)", obj)));
  331. return dt.Rows[0];
  332. }
  333. else
  334. {
  335. //resultFlag = false;
  336. result.Add(new JObject(new JProperty("MES读取(工单信息:ERR)","工单未找到!")));
  337. return null;
  338. }
  339. }
  340. }
  341. catch (Exception e)
  342. {
  343. resultFlag = false;
  344. result.Add(new JObject(new JProperty("系统异常", e.Message)));
  345. return null;
  346. }
  347. }
  348. public bool PlcWriteRepeat<T>(string db,object value,int repeat = 5)
  349. {
  350. bool isSuccess = false;
  351. isSuccess = s7.Write<T>(db, (T)(value));
  352. result.Add(new JObject(new JProperty("PLC写入数据:", db + "=" + value.ToString())));
  353. int i = 0;
  354. while ( !isSuccess && i<repeat )
  355. {
  356. s7.IS_OPEN = false;
  357. isSuccess = s7.Write<T>(db, (T)(value));
  358. result.Add(new JObject(new JProperty("PLC写入数据(重写第" + i.ToString() + "次):", db + "=" + value.ToString())));
  359. i++;
  360. }
  361. return isSuccess;
  362. }
  363. public bool IsReusable
  364. {
  365. get
  366. {
  367. return false;
  368. }
  369. }
  370. }