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

PLC对接,包装二线及产品信息

qinqi 1 неделя назад
Родитель
Сommit
f8ff7146ef
2 измененных файлов с 413 добавлено и 0 удалено
  1. 192 0
      wwwroot/api/plc/plc_chengjianfl3125.ashx
  2. 221 0
      wwwroot/api/plc/plc_led_data_print3124.ashx

+ 192 - 0
wwwroot/api/plc/plc_chengjianfl3125.ashx

@@ -0,0 +1,192 @@
+<%@ WebHandler Language="C#" Class="plc_chengjianfl3125" %>
+
+using System;
+using System.Web;
+using DK.XuWei.WebMes;
+using Curtain.DataAccess;
+using Curtain.Log;
+using System.Data;
+using Newtonsoft.Json.Linq;
+
+
+/// <summary>
+/// 成检分流
+/// qq
+/// 2024-07-26
+/// </summary>
+public class plc_chengjianfl3125 : IHttpHandler
+{  
+    public static class PLC
+    {
+        public static string plcIp = "172.19.26.136";
+        public static int plcPort = 102;
+        public static string dbResult = "6.0";   //结果;扫码结果(1:通过,2:条码错误,3:产品不可到达,4:MES异常,0:复位)
+        public static string dbGoodsCode = "6.2";//产品编码(数值写入,产品型号)
+        public static string dbGoodsType = "6.4";//产品类型(1:智能、2:连体)
+        public static string dbGoodsSpec = "6.6";//出水距(1:水距305,2:水距400.3:220)
+        public static string dbBarcode = "6.10";  //产品条码 
+    }
+
+    //输入参数 【条码】
+    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() : "";
+
+        DataTable infoDt = new DataTable();
+        //写入PLC
+        //正式
+
+        if (context.Request["barcode"] is object)
+        {
+            //string barCode = context.Request["barcode"].ToString();
+            if (PlcOpen() == 1)
+            { 
+                DataTable goodsType = GetGoodinfo(barCode);
+
+                if (goodsType != null  && goodsType.Rows.Count > 0)
+                {
+                    bool goodscodeflag = PlcWriteRepeat<short>(PLC.dbGoodsCode, Convert.ToInt16(goodsType.Rows[0]["SEATCOVERCODE"].ToString()));
+                    bool goodstypeflag = PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(goodsType.Rows[0]["GOODSTYPENAME"].ToString()));
+                    bool goodsSpecflag = PlcWriteRepeat<short>(PLC.dbGoodsSpec, Convert.ToInt16(goodsType.Rows[0]["GOODSSPECIFICATION"].ToString()));
+                    bool barcodeflag = PlcWriteRepeat<string>(PLC.dbBarcode, barCode);
+                    bool resultflag = PlcWriteRepeat<short>(PLC.dbResult, Convert.ToInt16(1));
+                    result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbResult + " = " + s7.Read<short>(PLC.dbResult) + ";" + PLC.dbGoodsCode + " = " + s7.Read<short>(PLC.dbGoodsCode) + ";"+ PLC.dbGoodsType + " = " + s7.Read<short>(PLC.dbGoodsType) + ";" + PLC.dbBarcode + " = " + s7.Read<string>(PLC.dbBarcode, 11))));
+                }
+                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 ex )
+        {
+            result.Add(new JObject(new JProperty("系统异常:ERROR", ex.Message)));
+            throw ex ; 
+        }
+    }
+
+    
+
+    //读取产品信息
+    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
+                                    ,CASE
+                                        WHEN INSTR(G.GOODSSPECIFICATION, '305') = 1 THEN 1
+                                        WHEN INSTR(G.GOODSSPECIFICATION, '400') = 1 THEN 2
+                                        WHEN INSTR(G.GOODSSPECIFICATION, '220') = 1 THEN 3
+                                        WHEN INSTR(G.GOODSSPECIFICATION, '横排') = 1 THEN 4
+                                    END AS GOODSSPECIFICATION
+                                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 
+                                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())));
+                    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;
+        }
+    }
+
+}

+ 221 - 0
wwwroot/api/plc/plc_led_data_print3124.ashx

@@ -0,0 +1,221 @@
+<%@ WebHandler Language="C#" Class="plc_led_data_print3124" %>
+
+using System;
+using System.Web;
+using System.Collections.Generic;
+using Curtain.Net.Sockets;
+using DK.XuWei.WebMes;
+using Curtain.DataAccess;
+using Curtain.Log;
+using System.Data;
+using Newtonsoft.Json.Linq;
+using System.Collections;
+
+/// <summary>
+/// 对接LED看板,用于成检显示产品属性信息,打印包装2线
+/// xuwei
+/// 2026-08-19
+/// </summary>
+public class plc_led_data_print3124 : IHttpHandler
+{
+    public struct LedData
+    {
+        public int goodscode;
+        public int goodsspecification;
+        public int goodstype;
+        public string barcode;
+    }
+
+    //输入参数 【条码】
+    public string barCode = "";
+
+    string procedure_no;
+    string fentiprocedure_no;
+    string procedure_in;
+    string road_no;
+    string rack;
+    string position;
+    //返回失败提示
+    public string message = "";
+
+    public void ProcessRequest(HttpContext context)
+    {
+        try
+        {
+            context.Response.ContentType = "text/plain";
+            //输入输出参数初始化 计件
+            barCode = context.Request["barcode"] is object ? context.Request["barcode"] : "";
+            procedure_no = context.Request["procedure_no"] is object ? context.Request["procedure_no"] : "1_1_3";
+            fentiprocedure_no = "1_1_4";
+            procedure_in = "1";
+            road_no = "0";
+            rack = "";
+            position = "";
+
+            DataTable infoDt = new DataTable();
+            //写入PLC
+            //正式
+            SiemensS7.Open("172.19.26.152", 102);
+
+            if (context.Request["barcode"] is object)
+            {
+                using (IDataAccess conn = DataAccess.Create())
+                {
+                    infoDt = conn.ExecuteDatatable(@"
+                            SELECT g.seatcovercode
+                                    ,CASE
+                                        WHEN instr(g.goodsspecification, '305') = 1 THEN 1
+                                        WHEN instr(g.goodsspecification, '400') = 1 THEN 2
+                                        WHEN instr(g.goodsspecification, '地排') = 1 THEN 3
+                                        WHEN instr(g.goodsspecification, '横排') = 1 THEN 4
+                                        WHEN instr(g.goodsspecification, '295') = 1 THEN 5
+                                        WHEN instr(g.goodsspecification, '220') = 1 THEN 6
+                                        WHEN instr(g.goodsspecification, '300') = 1 THEN 7
+                                    END AS goodsspecification
+                                    ,CASE
+                                        WHEN instr(gt.goodstypename, '连体') = 1 THEN 1
+                                        WHEN instr(gt.goodstypename, '智能') = 1 THEN 2
+                                    END AS goodstypename
+                                    ,CASE WHEN g.goodsid in (178,177,164,163,166,150,149,182,183,190,191,186,187) THEN '分体' 
+                                          when g.goodsid in(158,159,167) and gdd.logoid in (7,14) THEN '分体'
+                                          when g.goodsid in(188,189) and gdd.logoid in (7,17) THEN '分体'
+                                            ELSE '大件' END AS pritetype 
+                                    ,gdd.barcode
+                                    ,g.goodscode
+                                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
+
+                                WHERE gdd.barcode = @barcode@
+                            ",
+                        new CDAParameter("barcode", context.Request["barcode"])
+                    );
+                    if (infoDt != null && infoDt.Rows.Count > 0 && infoDt.Rows[0] != null)
+                    {
+
+                        SiemensS7.Write<short>("31.2", Convert.ToInt16(infoDt.Rows[0]["seatcovercode"]));
+                        SiemensS7.Write<short>("31.4", Convert.ToInt16(infoDt.Rows[0]["goodsspecification"]));
+                        SiemensS7.Write<short>("31.6", Convert.ToInt16(infoDt.Rows[0]["goodstypename"]));
+                        SiemensS7.Write<string>("31.10", infoDt.Rows[0]["barcode"].ToString());
+                        SiemensS7.Write<string>("31.24", infoDt.Rows[0]["goodscode"].ToString());
+
+                        //写标识位-扫描结果
+                        //写标识位-扫描结果
+                        SiemensS7.Write<short>("31.0", Convert.ToInt16(1));
+                    }
+
+                    context.Response.Write(SiemensS7.Read<short>("31.0"));
+                    context.Response.Write(SiemensS7.Read<short>("31.2"));
+                    context.Response.Write(SiemensS7.Read<short>("31.4"));
+                    context.Response.Write(SiemensS7.Read<short>("31.6"));
+                    context.Response.Write(SiemensS7.Read<string>("31.24", 11));
+
+                    if (infoDt != null && infoDt.Rows.Count > 0 && infoDt.Rows[0] != null)
+                    {
+                        if (Convert.ToInt16(infoDt.Rows[0]["seatcovercode"]) != Convert.ToInt16(SiemensS7.Read<short>("31.2"))
+                                || Convert.ToInt16(infoDt.Rows[0]["goodsspecification"]) != Convert.ToInt16(SiemensS7.Read<short>("31.4"))
+                                || Convert.ToInt16(infoDt.Rows[0]["goodstypename"]) != Convert.ToInt16(SiemensS7.Read<short>("31.6"))
+                                || Convert.ToInt16(1) != Convert.ToInt16(SiemensS7.Read<short>("31.0")))
+                        {
+                            SiemensS7.Write<short>("31.2", Convert.ToInt16(infoDt.Rows[0]["seatcovercode"]));
+                            SiemensS7.Write<short>("31.4", Convert.ToInt16(infoDt.Rows[0]["goodsspecification"]));
+                            SiemensS7.Write<short>("31.6", Convert.ToInt16(infoDt.Rows[0]["goodstypename"]));
+                            SiemensS7.Write<string>("31.10", infoDt.Rows[0]["barcode"].ToString());
+                            SiemensS7.Write<string>("31.24", infoDt.Rows[0]["goodscode"].ToString());
+
+                            //写标识位-扫描结果
+                            SiemensS7.Write<short>("31.0", Convert.ToInt16(1));
+                        }
+                    }
+                    Logger.Info("写入数据完成!barcode:" + context.Request["barcode"].ToString() + "五位数据位:" + SiemensS7.Read<short>("31.0") + "," + SiemensS7.Read<short>("31.2")
+                        + "," + SiemensS7.Read<short>("31.4") + "," + SiemensS7.Read<short>("31.6") + "," + SiemensS7.Read<short>("31.10") + "," + SiemensS7.Read<string>("31.24", 11) + ";数据:" + new JsonResult(infoDt).ToJson());
+                }
+                //调用标准计件方法===============================
+                if (infoDt != null && infoDt.Rows.Count > 0 && infoDt.Rows[0] != null)
+                {
+                    if (infoDt.Rows[0]["pritetype"].ToString() == "分体")
+                    {
+                        procedure_no = fentiprocedure_no;
+                    }
+
+                    WCF wcf = new WCF();
+                    wcf.Para.Add(new JProperty("barcode", barCode.ToString()));
+                    wcf.Para.Add(new JProperty("procedure_no", procedure_no));
+                    wcf.Para.Add(new JProperty("road_no", road_no));
+                    wcf.Para.Add(new JProperty("rack", rack));
+                    wcf.Para.Add(new JProperty("position", position));
+                    wcf.Para.Add(new JProperty("procedure_in", procedure_in));
+                    string jsonStr = wcf.Get("/DKService/ExHGS3QR/AddWorkInfoHGS3_QR");
+
+                    //这个接口很小的机率会带出html标签===============================================
+                    if (jsonStr.IndexOf("<html>") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf("<html>"));
+                    if (jsonStr.IndexOf("<!DOCTYPEhtml>") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf("<!DOCTYPEhtml>"));
+                    //xuwei fix 2022-05-28 有时会带出 DOCTYPEhtml标签
+                    jsonStr = jsonStr.Replace("<!DOCTYPEhtml>", "");
+
+                    jsonStr = jsonStr.Replace("\\u000d\\u000a", "")
+                            .Replace("\\n", "").Replace("\\", "").Replace(" ", "")
+                            .Replace("产品编码:", " 产品编码 ")
+                            .Replace("当前工序:", " 当前工序 ")
+                            .Replace("可到工序:", " 可到工序 ")
+                            .Replace("{\"d\":\"", "")
+                            .Replace("\"}\"}", "\"}")
+                            .Replace("<!DOCTYPEhtml>", "")
+                            ;
+
+                    Curtain.Log.Logger.Debug(context.Request.UserHostAddress + "\r\n" + context.Request.Url + "\r\n" + jsonStr);
+
+                    string apiResult = "";
+                    //处理输出结果 OK开头成功 NOK开头失败
+                    JObject json = JObject.Parse(jsonStr);
+                    bool apiStatus = false;
+                    apiStatus = Convert.ToBoolean(json["success"]);
+                    apiResult = jsonStr;
+                    //HttpContext.Current.Response.Write(apiResult);
+
+                    //记录接口调用日志
+                    Hashtable ht = new Hashtable();
+                    ht.Add("barcode", barCode.ToString());
+                    ht.Add("procedure_no", procedure_no);
+                    ht.Add("road_no", road_no);
+                    ht.Add("rack", rack);
+                    ht.Add("position", position);
+                    ht.Add("procedure_in", procedure_in);
+
+                    int apiId = Convert.ToInt32(procedure_no.Replace("_", "") + road_no + Math.Abs(Convert.ToInt32(procedure_in)));
+                    barCode = barCode.Replace("2c%", ",");
+                    int apiBarcodeCount = barCode.Split(',').Length;
+                    string apiUrl = "http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port.ToString() + "/api/DKService/ExHGS3QR/AddWorkInfoHGS3_QR/?" + JsonClient.ParaToString(ht);
+                    ApiLog.WriteApiLog("PLC_扫码", apiUrl, apiStatus, apiResult, apiId, apiBarcodeCount);
+                    //===============================================
+                }
+            }
+            else
+            {
+                //写标识位-扫描结果
+                SiemensS7.Write<short>("31.0", Convert.ToInt16(2));
+                context.Response.Write(SiemensS7.Read<short>("31.0"));
+                Logger.Info("无条码!");
+            }
+
+            context.Response.Write(new JsonResult(infoDt).ToJson());
+            SiemensS7.Close();
+        }
+        catch (Exception ex )
+        {
+            Logger.Info("异常!:"+ex);
+            throw ex ;
+        }
+    }
+
+    public bool IsReusable
+    {
+        get
+        {
+            return false;
+        }
+    }
+
+}