Browse Source

水效标-辅件PLC对接

qinqi 1 year ago
parent
commit
6401db2eb7
1 changed files with 187 additions and 0 deletions
  1. 187 0
      wwwroot/api/plc/plc_shuixiaobiao3123.ashx

+ 187 - 0
wwwroot/api/plc/plc_shuixiaobiao3123.ashx

@@ -0,0 +1,187 @@
+<%@ WebHandler Language="C#" Class="plc_shuixiaobiao3123" %>
+
+using System;
+using System.Web;
+using DK.XuWei.WebMes;
+using Curtain.DataAccess;
+using Curtain.Log;
+using System.Data;
+using Newtonsoft.Json.Linq;
+
+
+/// <summary>
+/// 对接水效标识检验3123
+/// qq
+/// 2024-12-30
+/// </summary>
+public class plc_shuixiaobiao3123 : IHttpHandler
+{
+    public static class PLC
+    {
+        public static string plcIp = "172.19.26.228";
+        public static int plcPort = 102;
+        public static string dbResult = "100.0";   //扫码结果(0无效1成功2失败)
+        public static string dbType = "100.2";  //1-100对应型号 辅件
+        public static string dbBarcode = "100.6";  //产品条码  
+        public static string dbGoddsType = "100.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);
+
+                if (goodsType != null  && goodsType.Rows.Count > 0)
+                {
+                    bool barcodeflag = PlcWriteRepeat<string>(PLC.dbBarcode, barCode);
+                    bool typeflag = PlcWriteRepeat<short>(PLC.dbType, Convert.ToInt16(goodsType.Rows[0]["ACCESSORIESCODE"].ToString()));
+                    bool goodstypeflag = PlcWriteRepeat<string>(PLC.dbGoddsType, goodsType.Rows[0]["GOODSCODE"].ToString()); 
+                   
+                    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.dbType + " = " + s7.Read<short>(PLC.dbType) + ";" +
+                        PLC.dbGoddsType + " = " + s7.Read<string>(PLC.dbGoddsType, 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)
+        {
+
+            throw;
+        }
+    }
+
+
+    //读取产品信息
+    public DataTable GetGoodinfo(string barcode)
+    {
+        try
+        {
+            using (IDataAccess conn = DataAccess.Create())
+            {
+                DataTable dt = conn.ExecuteDatatable(@"
+                     SELECT
+                                TPGL.BARCODE,
+                                TPGL.GOODSCODE,
+	                            TMA.GOODSID,
+	                            TMA.LOGOID,
+	                            TMA.ACCESSORIESNAME,
+	                            TMA.ACCESSORIESCODE,
+	                            TMD.DICTIONARYVALUE
+                            FROM
+	                            TP_PM_GROUTINGDAILYDETAIL TPGL
+	                            LEFT JOIN TP_MST_ACCESSORIES TMA ON TPGL.LOGOID = TMA.LOGOID 
+                                AND TMA.GOODSID = TPGL.GOODSID
+                                LEFT JOIN TP_MST_DATADICTIONARY TMD ON TMA.ACCESSORIESTYPEID = TMD.DICTIONARYID
+                            WHERE  TMA.VALUEFLAG = 1
+                                AND TMD.VALUEFLAG = 1 
+                                AND TMD.DICTIONARYVALUE = '水效标'
+                                AND TPGL.BARCODE = @BARCODE@
+				    ",
+                    new CDAParameter("BARCODE", barcode)
+                );
+                if (dt.Rows.Count > 0)
+                {
+
+                    result.Add(new JObject(new JProperty("MES读取(产品信息:OK)", barcode + ";辅件类型:" + Convert.ToInt16(dt.Rows[0]["ACCESSORIESCODE"])+ ";产品类型:" + dt.Rows[0]["GOODSCODE"].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;
+        }
+    }
+
+}