| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <%@ 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;
- }
- }
- }
|