| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- <%@ WebHandler Language="C#" Class="autoPackingForm6157" %>
- using System;
- using System.Web;
- using System.Data;
- using DK.XuWei.WebMes;
- using Curtain.DataAccess;
- using Newtonsoft.Json.Linq;
- /// <summary>
- /// PLC对接使用
- /// 轮询处理逻辑;
- /// qq add 2025-08-06
- /// </summary>
- 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<int>(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID
- PlcWriteRepeat<short>(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量
- PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体
- PlcWriteRepeat<short>(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托
- PlcWriteRepeat<short>(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量
- PlcWriteRepeat<short>(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号 1000.12
- PlcWriteRepeat<string>(PLC.dbGoodsCodes, dr["GOODSCODES"].ToString());//产品型号 1000.18
- }
- result.Add(new JObject(new JProperty("PLC读出(核对)",
- "FormID["+ PLC.dbFormID + "] =" + s7.Read<int>(PLC.dbFormID) +
- ";StackNum[" + PLC.dbStackNum + "] = " + s7.Read<short>(PLC.dbStackNum) +
- ";GoodsType[" + PLC.dbGoodsType + "] = " + s7.Read<short>(PLC.dbGoodsType) +
- ";PlateType[" + PLC.dbPlateType + "] = " + s7.Read<short>(PLC.dbPlateType) +
- ";PlateNum[" + PLC.dbPlateNum + "] = " + s7.Read<short>(PLC.dbPlateNum) +
- ";GoodsCode[" + PLC.dbGoodsCode + "] = " + s7.Read<short>(PLC.dbGoodsCode) +
- ";GoodsCodes[" + PLC.dbGoodsCodes + "] = " + s7.Read<string>(PLC.dbGoodsCodes, 14)
- )));
- //========================================
- context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
- }
- else
- {
- DataRow dr = GetForms(formID);
- //传输数据
- if (dr != null)
- {
- PlcWriteRepeat<int>(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID
- PlcWriteRepeat<short>(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量
- PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体
- PlcWriteRepeat<short>(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托
- PlcWriteRepeat<short>(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量
- PlcWriteRepeat<short>(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号 1000.12
- PlcWriteRepeat<string>(PLC.dbGoodsCodes, dr["GOODSCODES"].ToString());//产品型号 1000.18
- }
- result.Add(new JObject(new JProperty("PLC读出(核对)",
- "FormID["+ PLC.dbFormID + "] =" + s7.Read<int>(PLC.dbFormID) +
- ";StackNum[" + PLC.dbStackNum + "] = " + s7.Read<short>(PLC.dbStackNum) +
- ";GoodsType[" + PLC.dbGoodsType + "] = " + s7.Read<short>(PLC.dbGoodsType) +
- ";PlateType[" + PLC.dbPlateType + "] = " + s7.Read<short>(PLC.dbPlateType) +
- ";PlateNum[" + PLC.dbPlateNum + "] = " + s7.Read<short>(PLC.dbPlateNum) +
- ";GoodsCode[" + PLC.dbGoodsCode + "] = " + s7.Read<short>(PLC.dbGoodsCode) +
- ";GoodsCodes[" + PLC.dbGoodsCodes + "] = " + s7.Read<string>(PLC.dbGoodsCodes, 14)
- )));
- //========================================
- context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
- }
- }
- else
- {
- DataRow dr = GetForms(0);
- //传输数据
- if (dr != null)
- {
- PlcWriteRepeat<int>(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID
- PlcWriteRepeat<short>(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量
- PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体
- PlcWriteRepeat<short>(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托
- PlcWriteRepeat<short>(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量
- PlcWriteRepeat<short>(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号 1000.12
- PlcWriteRepeat<string>(PLC.dbGoodsCodes, dr["GOODSCODES"].ToString());//产品型号 1000.18
- }
- result.Add(new JObject(new JProperty("PLC读出(核对)",
- "FormID["+ PLC.dbFormID + "] =" + s7.Read<int>(PLC.dbFormID) +
- ";StackNum[" + PLC.dbStackNum + "] = " + s7.Read<short>(PLC.dbStackNum) +
- ";GoodsType[" + PLC.dbGoodsType + "] = " + s7.Read<short>(PLC.dbGoodsType) +
- ";PlateType[" + PLC.dbPlateType + "] = " + s7.Read<short>(PLC.dbPlateType) +
- ";PlateNum[" + PLC.dbPlateNum + "] = " + s7.Read<short>(PLC.dbPlateNum) +
- ";GoodsCode[" + PLC.dbGoodsCode + "] = " + s7.Read<short>(PLC.dbGoodsCode) +
- ";GoodsCodes[" + PLC.dbGoodsCodes + "] = " + s7.Read<string>(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<short>(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<int>(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<short>(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<T>(string db,object value,int repeat = 5)
- {
- bool isSuccess = false;
- isSuccess = s7.Write<T>(db, (T)(value));
- result.Add(new JObject(new JProperty("PLC写入数据:", db + "=" + value.ToString())));
- int i = 0;
- while ( !isSuccess && i<repeat )
- {
- s7.IS_OPEN = false;
- isSuccess = s7.Write<T>(db, (T)(value));
- result.Add(new JObject(new JProperty("PLC写入数据(重写第" + i.ToString() + "次):", db + "=" + value.ToString())));
- i++;
- }
- return isSuccess;
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
|