autoPackingForm6157.ashx 15 KB

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