%@ WebHandler Language="C#" Class="autoPacking6151_db" %>
using System;
using System.Web;
using DK.XuWei.WebMes;
///
/// PLC数据块监控和初始化使用
/// xuwei add 2023-12-31
///
public class autoPacking6151_db : IHttpHandler
{
public static class PLC
{
public static string plcIp = "172.18.36.95";
public static int plcPort = 102;
public static string dbBarcode = "151.2"; //151.0 => 151.2
public static string dbGoodsId = "151.14";
public static string dbGoodsNum = "151.16";
public static string dbResult = "151.18";
public static string dbLogoId = "151.22";
public static string dbMaterialcode = "151.30";
public static string dbRead = "151.26";
public static string[] dbBarcodeBlock = { "150.2", "150.32", "150.62", "150.92", "150.122", "150.152", "150.182", "150.212", "150.242", "150.272", "150.302", "150.332" };
public static string dbBanMa = "150.362";//150.360 => 150.362
public static string dbUsercode = "150.376"; //150.374 => 150.376
}
public static S7 s7 = new S7();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
if (context.Request["init"] is object)
{
PlcInit(context);
}
else
{
PlcRead(context);
}
}
public void PlcInit(HttpContext context)
{
s7.Open(PLC.plcIp, PLC.plcPort);
s7.Write(PLC.dbRead, (Int16)1).ToString();
s7.Write(PLC.dbBarcode, "00000000000").ToString();
s7.Write(PLC.dbMaterialcode, "00000000000000").ToString();
s7.Write(PLC.dbGoodsId, (Int16)0).ToString();
s7.Write(PLC.dbLogoId, (Int16)0).ToString();
s7.Write(PLC.dbGoodsNum, (Int16)0).ToString();
s7.Write(PLC.dbResult, (Int16)0).ToString();
for (int i = 0; i < PLC.dbBarcodeBlock.Length; i++)
{
s7.Write(PLC.dbBarcodeBlock[i], "00000000000").ToString();
}
//测试条码
//s7.Write(PLC.dbBarcodeBlock[0], "10016782613").ToString();
//s7.Write(PLC.dbBarcodeBlock[1], "00000000000").ToString();
s7.Write(PLC.dbBanMa, "00000000").ToString();
s7.Write(PLC.dbUsercode, "DONGKE").ToString();
//s7.Close();
context.Response.Write("INIT OK!");
}
public void PlcRead(HttpContext context)
{
s7.Open(PLC.plcIp, PLC.plcPort);
context.Response.Write(PLC.plcIp + ":" + PLC.plcPort + "\n\n");
if(context.Request["read"] is object)
{
Int16 readFlag = Convert.ToInt16(context.Request["read"].ToString());
s7.Write(PLC.dbRead, readFlag);
}
string read = s7.Read(PLC.dbRead).ToString();
context.Response.Write("读取标识:[" + PLC.dbRead + "]:" + read + "\n");
string bcode = s7.Read(PLC.dbBarcode, 11).ToString();
context.Response.Write("条码:[" + PLC.dbBarcode + "]:" + bcode + "\n");
string materialcode = s7.Read(PLC.dbMaterialcode,14).ToString();
context.Response.Write("物料码:[" + PLC.dbMaterialcode + "]:" + materialcode + "\n");
string goodsid = s7.Read(PLC.dbGoodsId).ToString();
context.Response.Write("产品:[" + PLC.dbGoodsId + "]:" + goodsid + "\n");
string logoid = s7.Read(PLC.dbLogoId).ToString();
context.Response.Write("商标:[" + PLC.dbLogoId + "]:" + logoid + "\n");
string goodsnum = s7.Read(PLC.dbGoodsNum).ToString();
context.Response.Write("板数:[" + PLC.dbGoodsNum + "]:" + goodsnum + "\n");
string result = s7.Read(PLC.dbResult).ToString();
context.Response.Write("结果:[" + PLC.dbResult + "]:" + result + "\n\n");
for (int i = 0; i < PLC.dbBarcodeBlock.Length; i++)
{
string barcode = s7.Read(PLC.dbBarcodeBlock[i], 11).ToString();
context.Response.Write("条码" + (i + 1).ToString() + ":[" + PLC.dbBarcodeBlock[i] + "]:" + barcode + "\n");
}
string banma = s7.Read(PLC.dbBanMa, 8).ToString();
context.Response.Write("板码:[" + PLC.dbBanMa + "]:" + banma + "\n");
string usercode = s7.Read(PLC.dbUsercode, 6).ToString();
context.Response.Write("工号:[" + PLC.dbUsercode + "]:" + usercode + "\n");
//s7.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}