%@ WebHandler Language="C#" Class="autoPackingForm6156" %>
using System;
using System.Web;
using System.Data;
using DK.XuWei.WebMes;
using Curtain.DataAccess;
using Newtonsoft.Json.Linq;
///
/// PLC对接使用
/// 轮询处理逻辑;
/// qq add 2025-08-06
///
public class autoPackingForm6156 : 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 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;
}
//主程序入口
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();
//传输数据
if (dr != null)
{
PlcWriteRepeat(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID
PlcWriteRepeat(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量
PlcWriteRepeat(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体
PlcWriteRepeat(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号
PlcWriteRepeat(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托
PlcWriteRepeat(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量
}
result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbFormID + " = " + s7.Read(PLC.dbFormID) + ";" + PLC.dbStackNum +
" = " + s7.Read(PLC.dbStackNum) + ";" + PLC.dbGoodsType + " = " + s7.Read(PLC.dbGoodsType)+ ";" + PLC.dbGoodsCode +
" = " + s7.Read(PLC.dbGoodsCode) + ";"+ PLC.dbPlateType + " = " + s7.Read(PLC.dbPlateType) +";" + PLC.dbPlateNum +
" = " + s7.Read(PLC.dbPlateNum) + ";")));
//========================================
context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
}
}
else
{
DataRow dr = GetForms();
//传输数据
if (dr != null)
{
PlcWriteRepeat(PLC.dbFormID, Convert.ToInt32(dr["FORMID"].ToString()));//工单ID
PlcWriteRepeat(PLC.dbStackNum, Convert.ToInt16(dr["STACKNUM"].ToString()));//码垛数量
PlcWriteRepeat(PLC.dbGoodsType, Convert.ToInt16(dr["GOODSTYPE"].ToString())); //产品类型 1:智能 2:连体
PlcWriteRepeat(PLC.dbGoodsCode, Convert.ToInt16(dr["GOODSCODE"].ToString()));//产品型号
PlcWriteRepeat(PLC.dbPlateType, Convert.ToInt16(dr["PLATETYPE"].ToString()));//托盘类型 1:铁托 2:胶托
PlcWriteRepeat(PLC.dbPlateNum, Convert.ToInt16(dr["PLATENUM"].ToString())); //产品数量
}
result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbFormID + " = " + s7.Read(PLC.dbFormID) + ";" + PLC.dbStackNum +
" = " + s7.Read(PLC.dbStackNum) + ";" + PLC.dbGoodsType + " = " + s7.Read(PLC.dbGoodsType)+ ";" + PLC.dbGoodsCode +
" = " + s7.Read(PLC.dbGoodsCode) + ";"+ PLC.dbPlateType + " = " + s7.Read(PLC.dbPlateType) +";" + PLC.dbPlateNum +
" = " + s7.Read(PLC.dbPlateNum) + ";")));
//========================================
context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
}
}
}
context.Response.Write(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(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(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(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 = 1 and packingformid = @PACKINGFORMID@
",
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()
{
try
{
using (IDataAccess conn = DataAccess.Create())
{
DataTable dt = conn.ExecuteDatatable(@"
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 goodstypecode,
CASE WHEN d.dictionaryvalue = '铁托' THEN 1
WHEN d.dictionaryvalue = '胶托' THEN 2 ELSE 3 END platetype,
pf.platenum
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'
WHERE
pf.packingline = '1'
and pf.sycnflag = 0
and pf.stacknum is not null
and pf.platetype is not null
ORDER BY PF.CREATETIME ,PF.ORDERNO
"
);
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]["goodstypecode"].ToString()));
obj.Add(new JProperty("GOODSCODE", dt.Rows[0]["goodscode"].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(string db,object value,int repeat = 5)
{
bool isSuccess = false;
isSuccess = s7.Write(db, (T)(value));
result.Add(new JObject(new JProperty("PLC写入数据:", db + "=" + value.ToString())));
int i = 0;
while ( !isSuccess && i(db, (T)(value));
result.Add(new JObject(new JProperty("PLC写入数据(重写第" + i.ToString() + "次):", db + "=" + value.ToString())));
i++;
}
return isSuccess;
}
public bool IsReusable
{
get
{
return false;
}
}
}