Просмотр исходного кода

成检新增扫码头(副品分流重烧)

qinqi 1 год назад
Родитель
Сommit
412ce1cc9d
1 измененных файлов с 240 добавлено и 0 удалено
  1. 240 0
      wwwroot/api/plc/plc_chengjian3122.ashx

+ 240 - 0
wwwroot/api/plc/plc_chengjian3122.ashx

@@ -0,0 +1,240 @@
+<%@ WebHandler Language="C#" Class="plc_chengjian3122" %>
+
+using System;
+using System.Web;
+using DK.XuWei.WebMes;
+using Curtain.DataAccess;
+using Curtain.Log;
+using System.Data;
+using Newtonsoft.Json.Linq;
+
+
+/// <summary>
+/// 对接3109后成检分流3122(副品和重烧分流)
+/// qq
+/// 2024-12-09
+/// </summary>
+public class plc_chengjian3122 : IHttpHandler
+{
+    public static class PLC
+    {
+        public static string plcIp = "172.19.26.230";
+        public static int plcPort = 102;
+        public static string dbResult = "200.0";   //结果;扫码结果(1:通过,2:条码错误,3:产品不可到达,4:MES异常,0:复位)
+        public static string dbBarcode = "200.4";  //产品条码 
+        public static string dbGoodsType = "200.16";//产品类型(1:智能、2:连体)
+        public static string dbGoddsLevel = "200.18";  //产品等级(4:正品:5:副品:6:重烧,7:次品)   5右转,6直走
+        public static string dbDefectType = "200.20";  //缺陷类型 
+    }
+
+    //输入参数 【条码】
+    public string barCode = "";
+
+    public JArray result = new JArray();
+    public static  S7 s7 = new S7();
+
+    //打开PLC
+    public int PlcOpen()
+    {
+        try
+        {
+            s7.Open(PLC.plcIp, PLC.plcPort);
+            result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接成功!")));
+        }
+        catch (Exception e)
+        {
+            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";
+        //输入产品条码参数
+        barCode = context.Request["barcode"] is object ? context.Request["barcode"].ToString() : "";
+
+        if (context.Request["barcode"] is object)
+        {
+            if (PlcOpen() == 1)
+            {
+                //获取产品信息
+                DataTable goodsType = GetGoodinfo(barCode);
+                //获取缺陷类别
+                DataTable defectType = GetDefectType(barCode);
+
+                if (goodsType != null  && goodsType.Rows.Count > 0)
+                {
+                    bool barcodeflag = PlcWriteRepeat<string>(PLC.dbBarcode, barCode);
+                    bool goodstypeflag = PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(goodsType.Rows[0]["GOODSTYPENAME"].ToString()));
+                    bool goodsLevelflag = PlcWriteRepeat<short>(PLC.dbGoddsLevel, Convert.ToInt16(goodsType.Rows[0]["GOODSLEVELTYPEID"].ToString()));
+                    if (defectType != null  && defectType.Rows.Count > 0)
+                    {
+                        bool defectTypeFlag = PlcWriteRepeat<short>(PLC.dbDefectType, Convert.ToInt16(defectType.Rows[0]["REMARKS"].ToString()));
+                    }
+                    else
+                    {
+                        bool defectTypeFlag = PlcWriteRepeat<short>(PLC.dbDefectType, Convert.ToInt16(0));
+                    }
+
+
+                    bool resultflag = PlcWriteRepeat<short>(PLC.dbResult, Convert.ToInt16(1));
+                    result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbResult + " = " + s7.Read<short>(PLC.dbResult) + ";" + 
+                        PLC.dbBarcode + " = " + s7.Read<string>(PLC.dbBarcode, 11) + ";"+
+                        PLC.dbGoodsType + " = " + s7.Read<short>(PLC.dbGoodsType) + ";" +
+                        PLC.dbGoddsLevel + " = " + s7.Read<short>(PLC.dbGoddsLevel) + ";" +
+                        PLC.dbDefectType + " = " + s7.Read<short>(PLC.dbDefectType))));
+                }
+                else
+                {
+                    s7.Write<short>(PLC.dbResult, Convert.ToInt16(2));
+                }
+
+            }
+            context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
+
+        }
+        else
+        {
+            context.Response.Write(new JsonResult(JsonStatus.error) { rows = new JObject(new JProperty("MES读取(条码信息:ERR)", "无条码")) }.ToJson());
+
+
+        }
+        //SiemensS7.Close();
+    }
+
+    //xuwei add begin 2024-01-19 增加多次写入容错方法===============
+    public bool PlcWriteRepeat<T>(string db,object value,int repeat = 5)
+    {
+        try
+        {
+            bool isSuccess = false;
+            isSuccess = s7.Write<T>(db, (T)(value));
+            result.Add(new JObject(new JProperty("PLC写入数据成功标识:", isSuccess)));
+            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;
+        }
+        catch (Exception)
+        {
+
+            throw;
+        }
+    }
+
+
+    //读取产品信息
+    public DataTable GetGoodinfo(string barcode)
+    {
+        try
+        {
+            using (IDataAccess conn = DataAccess.Create())
+            {
+                DataTable dt = conn.ExecuteDatatable(@"
+                    SELECT G.SEATCOVERCODE 
+                                ,CASE
+                                    WHEN INSTR(GT.GOODSTYPENAME, '智能') = 1 THEN 1
+                                    WHEN INSTR(GT.GOODSTYPENAME, '连体') = 1 THEN 2  
+                                END AS GOODSTYPENAME
+                                ,GDD.BARCODE
+                                ,G.GOODSCODE
+                                ,GL.GOODSLEVELTYPEID
+                            FROM TP_PM_GROUTINGDAILYDETAIL GDD
+                            LEFT JOIN TP_MST_GOODS G
+                            ON GDD.GOODSID = G.GOODSID
+                            LEFT JOIN TP_MST_GOODSTYPE GT  
+                            ON GT.GOODSTYPEID = G.GOODSTYPEID
+                            LEFT   JOIN TP_MST_GOODSLEVEL GL
+                            ON     GDD.GOODSLEVELTYPEID = GL.GOODSLEVELTYPEID
+                            WHERE GDD.BARCODE = @BARCODE@
+				    ",
+                    new CDAParameter("BARCODE", barcode)
+                );
+                if (dt.Rows.Count > 0)
+                {
+
+                    result.Add(new JObject(new JProperty("MES读取(产品信息:OK)", barcode + ";产品类型:" + dt.Rows[0]["GOODSTYPENAME"].ToString()+ ";产品等级:" + dt.Rows[0]["GOODSLEVELTYPEID"].ToString())));
+                    return dt;
+                }
+                else
+                {
+                    result.Add(new JObject(new JProperty("MES读取(用户信息:ERR)", "产品未找到!")));
+                    return null;
+                }
+            }
+        }
+        catch (Exception e)
+        {
+            result.Add(new JObject(new JProperty("系统异常:ERROR", e.Message)));
+            return null;
+        }
+    }
+    //读取缺陷类别    
+    public DataTable GetDefectType(string barcode)
+    {
+        try
+        {
+            using (IDataAccess conn = DataAccess.Create())
+            {
+                DataTable dt = conn.ExecuteDatatable(@" 
+                            SELECT D.DEFECTCODE, D.DEFECTNAME, DT.DEFECTTYPENAME, DT.DEFECTTYPEID, DT.REMARKS
+                                FROM   TP_PM_DEFECT PD
+                                LEFT   JOIN TP_MST_DEFECT D
+                                ON     PD.DEFECTID = D.DEFECTID
+                                LEFT   JOIN TP_MST_DEFECTTYPE DT
+                                ON     D.DEFECTTYPEID = DT.DEFECTTYPEID
+                                WHERE  D.VALUEFLAG = 1
+                                       AND PD.VALUEFLAG = 1
+                                       AND DT.DEFECTTYPEID <> 21
+                                       AND BARCODE = @BARCODE@
+                                ORDER  BY D.CREATETIME DESC
+                    ",
+                    new CDAParameter("BARCODE", barcode)
+                );
+                if (dt.Rows.Count > 0)
+                {
+
+                    result.Add(new JObject(new JProperty("MES读取(缺陷类别信息:OK)", barcode + ";缺陷类别:" + dt.Rows[0]["REMARKS"].ToString())));
+                    return dt;
+                }
+                else
+                {
+                    result.Add(new JObject(new JProperty("MES读取(缺陷类别信息:ERR)", "无缺陷类别!")));
+                    return null;
+                }
+            }
+        }
+        catch (Exception e)
+        {
+            result.Add(new JObject(new JProperty("系统异常:ERROR", e.Message)));
+            return null;
+        }
+    }
+
+    public bool IsReusable
+    {
+        get
+        {
+            return false;
+        }
+    }
+
+}