<%@ WebHandler Language="C#" Class="autoPackingForm6157" %> using System; using System.Web; using System.Data; using DK.XuWei.WebMes; using Curtain.DataAccess; using Newtonsoft.Json.Linq; /// /// PLC对接使用 /// 轮询处理逻辑; /// qq add 2025-08-06 /// public class autoPackingForm6157 : IHttpHandler { public static class PLC { public static string plcIp = "172.18.36.95"; public static int plcPort = 102; public static string dbFormID = "1000.0"; //工单ID public static string dbStackNum = "1000.4"; //码垛数量 public static string dbGoodsType = "1000.6"; //产品类型 1:智能 2:连体 public static string dbGoodsCode = "1000.8"; //产品型号 public static string dbPlateType = "1000.10"; //托盘类型 1:铁托 2:胶托 public static string dbPlateNum = "1000.12"; //产品数量 public static string dbStatus = "1000.14"; //状态标识 1时读取下一条信息传递 public static string dbGoodsCodes = "1000.18"; //产品型号 strig } public bool resultFlag = false; public JArray result = new JArray(); public static S7 s7 = new S7(); //打开PLC public int PlcOpen() { try { s7.Open(PLC.plcIp, PLC.plcPort); resultFlag = true; result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接成功!"))); } catch (Exception e) { resultFlag = false; result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接失败!"))); result.Add(new JObject(new JProperty("PLC错误信息", e.Message))); } return 1; } //关闭PLC public int PlcClose() { s7.Close(); result.Add(new JObject(new JProperty("PLC关闭", PLC.plcIp + ":" + PLC.plcPort + " 关闭成功!"))); return 1; } //主程序入口 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; BarcodeDo(context); } // 轮询到标识 public void BarcodeDo(HttpContext context) { if (PlcOpen() == 1) { int readFlag = PlcReadReadFlag(); //标识等于1时,代表上一条数据 PLC 已保存完成 if(readFlag == 1) { //读取包装工单 ID int formID = PlcReadFormID(); if (formID > 0) { //MES 更新该条工单的同步状态; int updateFlag = UpdateForm(formID); //时根据包装线及执行顺序给 PLC 传输下一条的工单信息,更新标识位为 0, if (updateFlag > 0) { DataRow dr = GetForms(0); //传输数据 if (dr != null) { PlcWriteRepeat(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID PlcWriteRepeat(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量 PlcWriteRepeat(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体 PlcWriteRepeat(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托 PlcWriteRepeat(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量 PlcWriteRepeat(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号 1000.12 PlcWriteRepeat(PLC.dbGoodsCodes, dr["GOODSCODES"].ToString());//产品型号 1000.18 } result.Add(new JObject(new JProperty("PLC读出(核对)", "FormID["+ PLC.dbFormID + "] =" + s7.Read(PLC.dbFormID) + ";StackNum[" + PLC.dbStackNum + "] = " + s7.Read(PLC.dbStackNum) + ";GoodsType[" + PLC.dbGoodsType + "] = " + s7.Read(PLC.dbGoodsType) + ";PlateType[" + PLC.dbPlateType + "] = " + s7.Read(PLC.dbPlateType) + ";PlateNum[" + PLC.dbPlateNum + "] = " + s7.Read(PLC.dbPlateNum) + ";GoodsCode[" + PLC.dbGoodsCode + "] = " + s7.Read(PLC.dbGoodsCode) + ";GoodsCodes[" + PLC.dbGoodsCodes + "] = " + s7.Read(PLC.dbGoodsCodes, 14) ))); //======================================== context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson()); } else { DataRow dr = GetForms(formID); //传输数据 if (dr != null) { PlcWriteRepeat(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID PlcWriteRepeat(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量 PlcWriteRepeat(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体 PlcWriteRepeat(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托 PlcWriteRepeat(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量 PlcWriteRepeat(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号 1000.12 PlcWriteRepeat(PLC.dbGoodsCodes, dr["GOODSCODES"].ToString());//产品型号 1000.18 } result.Add(new JObject(new JProperty("PLC读出(核对)", "FormID["+ PLC.dbFormID + "] =" + s7.Read(PLC.dbFormID) + ";StackNum[" + PLC.dbStackNum + "] = " + s7.Read(PLC.dbStackNum) + ";GoodsType[" + PLC.dbGoodsType + "] = " + s7.Read(PLC.dbGoodsType) + ";PlateType[" + PLC.dbPlateType + "] = " + s7.Read(PLC.dbPlateType) + ";PlateNum[" + PLC.dbPlateNum + "] = " + s7.Read(PLC.dbPlateNum) + ";GoodsCode[" + PLC.dbGoodsCode + "] = " + s7.Read(PLC.dbGoodsCode) + ";GoodsCodes[" + PLC.dbGoodsCodes + "] = " + s7.Read(PLC.dbGoodsCodes, 14) ))); //======================================== context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson()); } } else { DataRow dr = GetForms(0); //传输数据 if (dr != null) { PlcWriteRepeat(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID PlcWriteRepeat(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量 PlcWriteRepeat(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体 PlcWriteRepeat(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托 PlcWriteRepeat(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量 PlcWriteRepeat(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号 1000.12 PlcWriteRepeat(PLC.dbGoodsCodes, dr["GOODSCODES"].ToString());//产品型号 1000.18 } result.Add(new JObject(new JProperty("PLC读出(核对)", "FormID["+ PLC.dbFormID + "] =" + s7.Read(PLC.dbFormID) + ";StackNum[" + PLC.dbStackNum + "] = " + s7.Read(PLC.dbStackNum) + ";GoodsType[" + PLC.dbGoodsType + "] = " + s7.Read(PLC.dbGoodsType) + ";PlateType[" + PLC.dbPlateType + "] = " + s7.Read(PLC.dbPlateType) + ";PlateNum[" + PLC.dbPlateNum + "] = " + s7.Read(PLC.dbPlateNum) + ";GoodsCode[" + PLC.dbGoodsCode + "] = " + s7.Read(PLC.dbGoodsCode) + ";GoodsCodes[" + PLC.dbGoodsCodes + "] = " + s7.Read(PLC.dbGoodsCodes, 14) ))); //======================================== context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson()); //包装工单 ID错误 //PlcWriteResult(2); } } PlcClose(); } context.Response.Write(System.Environment.NewLine + (new JsonResult(resultFlag ? JsonStatus.success : JsonStatus.error) { rows = result }.ToJson())); } //读取PLC标识 public int PlcReadReadFlag() { if (!resultFlag) return -1; try { int read = Convert.ToInt16(s7.Read(PLC.dbStatus)); result.Add(new JObject(new JProperty("PLC读取(读取标识:OK)", PLC.dbStatus + " = " + read.ToString()))); return read; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //读取包装工单ID public int PlcReadFormID() { if (!resultFlag) return 0; try { int formID = Convert.ToInt32(s7.Read(PLC.dbFormID)); result.Add(new JObject(new JProperty("PLC读取(工单ID:OK)", PLC.dbFormID + " = " + formID.ToString()))); return formID; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return 0; } } //写入执行结果 // 1 通过 2 条码格式错误 3 产品不可到达 4 MES异常 5 条码不一致 6 绑板完成 7 工号读取错误 //public int PlcWriteResult(Int16 flag) //{ // try // { // string message = ""; // switch(flag) // { // case 1:message = "扫码通过:OK";break; // case 2:message = "工单信息错误:ERR";break; // case 3:message = "MES异常:ERR";break; // default:message = "未定义:ERR";break; // } // s7.Write(PLC.dbStatus, flag); // result.Add(new JObject(new JProperty("PLC写入("+message+")", PLC.dbStatus + " = " + flag.ToString()))); // return 1; // } // catch (Exception e) // { // result.Add(new JObject(new JProperty("PLC错误信息:ERROR", e.Message))); // return -1; // } //} public int UpdateForm(int packingformid) { try { using (IDataAccess conn = DataAccess.Create()) { int results = conn.ExecuteNonQuery(@" update TP_MST_PACKINGFORM set sycnflag = 1 WHERE packingline = 2 and packingformid = @PACKINGFORMID@ and sycnflag = 0 ", new CDAParameter("PACKINGFORMID", packingformid) ); if(results > 0 ) { result.Add(new JObject(new JProperty("MES更新(同步状态:OK)", ""))); return results; } else { //resultFlag = false; result.Add(new JObject(new JProperty("MES更新(同步状态:ERR)","未更新或已更新!"))); return 0; } } } catch (Exception e) { resultFlag = false; result.Add(new JObject(new JProperty("系统异常", e.Message))); return 0; } } public DataRow GetForms(int formid) { try { using (IDataAccess conn = DataAccess.Create()) { string sql = null; if (formid > 0) { sql = @" SELECT pf.packingformid FORMID, g.SEATCOVERCODE GOODSCODE, pf.materialcode, d1.dictionaryvalue STACKNUM, CASE WHEN G.GOODSTYPEID = 18 THEN 1 WHEN G.GOODSTYPEID = 3 THEN 2 ELSE 3 END GOODSTYPE, CASE WHEN d.dictionaryvalue = '铁托' THEN 1 WHEN d.dictionaryvalue = '胶托' THEN 2 ELSE 3 END PLATETYPE, pf.PLATENUM, g.GOODSCODE GOODSCODES FROM TP_MST_PACKINGFORM pf left join tp_mst_goods g on g.goodsid = pf.goodsid left join tp_mst_datadictionary d on pf.platetype = d.dictionaryid and d.dictionarytype = 'TPC031' left join tp_mst_datadictionary d1 on pf.stacknum = d1.dictionaryid and d1.dictionarytype = 'TPC032' left join V_GOODS_MATERIALCODE gg ON gg.GOODSID = pf.GOODSID AND gg.MATERIALCODE = pf.MATERIALCODE WHERE pf.packingformid = "+formid+@" ORDER BY PF.CREATETIME ,PF.ORDERNO "; /*and pf.packingline = '2' and pf.sycnflag = 0 and pf.stacknum is not null and pf.platetype is not null and (pf.logoid not in(0,65) or (pf.logoid = 0 and gg.logoid <>65) )*/ } else { sql = @" SELECT pf.packingformid FORMID, g.SEATCOVERCODE GOODSCODE, pf.materialcode, d1.dictionaryvalue STACKNUM, CASE WHEN G.GOODSTYPEID = 18 THEN 1 WHEN G.GOODSTYPEID = 3 THEN 2 ELSE 3 END GOODSTYPE, CASE WHEN d.dictionaryvalue = '铁托' THEN 1 WHEN d.dictionaryvalue = '胶托' THEN 2 ELSE 3 END PLATETYPE, pf.PLATENUM, g.GOODSCODE GOODSCODES FROM TP_MST_PACKINGFORM pf left join tp_mst_goods g on g.goodsid = pf.goodsid left join tp_mst_datadictionary d on pf.platetype = d.dictionaryid and d.dictionarytype = 'TPC031' left join tp_mst_datadictionary d1 on pf.stacknum = d1.dictionaryid and d1.dictionarytype = 'TPC032' left join V_GOODS_MATERIALCODE gg ON gg.GOODSID = pf.GOODSID AND gg.MATERIALCODE = pf.MATERIALCODE WHERE pf.packingline = '2' and pf.sycnflag = 0 and pf.stacknum is not null and pf.platetype is not null and (pf.logoid not in(0,65) or (pf.logoid = 0 and gg.logoid <>65) ) ORDER BY PF.CREATETIME ,PF.ORDERNO "; } DataTable dt = conn.ExecuteDatatable(sql); if(dt.Rows.Count > 0 ) { JObject obj = new JObject(); obj.Add(new JProperty("FORMID", dt.Rows[0]["FORMID"].ToString())); obj.Add(new JProperty("PLATENUM", dt.Rows[0]["PLATENUM"].ToString())); obj.Add(new JProperty("STACKNUM", dt.Rows[0]["STACKNUM"].ToString())); obj.Add(new JProperty("PLATETYPE", dt.Rows[0]["PLATETYPE"].ToString())); obj.Add(new JProperty("GOODSTYPE", dt.Rows[0]["GOODSTYPE"].ToString())); obj.Add(new JProperty("GOODSCODE", dt.Rows[0]["GOODSCODE"].ToString())); obj.Add(new JProperty("GOODSCODES", dt.Rows[0]["GOODSCODES"].ToString())); result.Add(new JObject(new JProperty("MES读取(工单信息:OK)", obj))); return dt.Rows[0]; } else { //resultFlag = false; result.Add(new JObject(new JProperty("MES读取(工单信息:ERR)","工单未找到!"))); return null; } } } catch (Exception e) { resultFlag = false; result.Add(new JObject(new JProperty("系统异常", e.Message))); return null; } } public bool PlcWriteRepeat(string db,object value,int repeat = 5) { bool isSuccess = false; isSuccess = s7.Write(db, (T)(value)); result.Add(new JObject(new JProperty("PLC写入数据:", db + "=" + value.ToString()))); int i = 0; while ( !isSuccess && i(db, (T)(value)); result.Add(new JObject(new JProperty("PLC写入数据(重写第" + i.ToString() + "次):", db + "=" + value.ToString()))); i++; } return isSuccess; } public bool IsReusable { get { return false; } } }