autoPacking6151_db.ashx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <%@ WebHandler Language="C#" Class="autoPacking6151_db" %>
  2. using System;
  3. using System.Web;
  4. using DK.XuWei.WebMes;
  5. /// <summary>
  6. /// PLC数据块监控和初始化使用
  7. /// xuwei add 2023-12-31
  8. /// </summary>
  9. public class autoPacking6151_db : IHttpHandler
  10. {
  11. public static class PLC
  12. {
  13. public static string plcIp = "172.18.36.95";
  14. public static int plcPort = 102;
  15. public static string dbBarcode = "151.2"; //151.0 => 151.2
  16. public static string dbGoodsId = "151.14";
  17. public static string dbGoodsNum = "151.16";
  18. public static string dbResult = "151.18";
  19. public static string dbLogoId = "151.22";
  20. public static string dbMaterialcode = "151.30";
  21. public static string dbRead = "151.26";
  22. 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" };
  23. public static string dbBanMa = "150.362";//150.360 => 150.362
  24. }
  25. public void ProcessRequest(HttpContext context)
  26. {
  27. context.Response.ContentType = "text/plain";
  28. if (context.Request["init"] is object)
  29. {
  30. PlcInit(context);
  31. }
  32. else
  33. {
  34. PlcRead(context);
  35. }
  36. }
  37. public void PlcInit(HttpContext context)
  38. {
  39. SiemensS7.Open(PLC.plcIp, PLC.plcPort);
  40. SiemensS7.Write<short>(PLC.dbRead, (Int16)1).ToString();
  41. SiemensS7.Write<string>(PLC.dbBarcode, "00000000000").ToString();
  42. SiemensS7.Write<string>(PLC.dbMaterialcode, "00000000000000").ToString();
  43. SiemensS7.Write<short>(PLC.dbGoodsId, (Int16)0).ToString();
  44. SiemensS7.Write<short>(PLC.dbLogoId, (Int16)0).ToString();
  45. SiemensS7.Write<short>(PLC.dbGoodsNum, (Int16)0).ToString();
  46. SiemensS7.Write<short>(PLC.dbResult, (Int16)0).ToString();
  47. for (int i = 0; i < PLC.dbBarcodeBlock.Length; i++)
  48. {
  49. SiemensS7.Write<string>(PLC.dbBarcodeBlock[i], "00000000000").ToString();
  50. }
  51. SiemensS7.Write<string>(PLC.dbBanMa, "00000000").ToString();
  52. SiemensS7.Close();
  53. context.Response.Write("INIT OK!");
  54. }
  55. public void PlcRead(HttpContext context)
  56. {
  57. SiemensS7.Open(PLC.plcIp, PLC.plcPort);
  58. context.Response.Write(PLC.plcIp + ":" + PLC.plcPort + "\n\n");
  59. if(context.Request["read"] is object)
  60. {
  61. Int16 readFlag = Convert.ToInt16(context.Request["read"].ToString());
  62. SiemensS7.Write<short>(PLC.dbRead, readFlag);
  63. }
  64. string read = SiemensS7.Read<short>(PLC.dbRead).ToString();
  65. context.Response.Write("读取标识:[" + PLC.dbRead + "]:" + read + "\n");
  66. string bcode = SiemensS7.Read<string>(PLC.dbBarcode, 11).ToString();
  67. context.Response.Write("条码:[" + PLC.dbBarcode + "]:" + bcode + "\n");
  68. string materialcode = SiemensS7.Read<string>(PLC.dbMaterialcode,14).ToString();
  69. context.Response.Write("物料码:[" + PLC.dbMaterialcode + "]:" + materialcode + "\n");
  70. string goodsid = SiemensS7.Read<short>(PLC.dbGoodsId).ToString();
  71. context.Response.Write("产品:[" + PLC.dbGoodsId + "]:" + goodsid + "\n");
  72. string logoid = SiemensS7.Read<short>(PLC.dbLogoId).ToString();
  73. context.Response.Write("商标:[" + PLC.dbLogoId + "]:" + logoid + "\n");
  74. string goodsnum = SiemensS7.Read<short>(PLC.dbGoodsNum).ToString();
  75. context.Response.Write("板数:[" + PLC.dbGoodsNum + "]:" + goodsnum + "\n");
  76. string result = SiemensS7.Read<short>(PLC.dbResult).ToString();
  77. context.Response.Write("结果:[" + PLC.dbResult + "]:" + result + "\n\n");
  78. for (int i = 0; i < PLC.dbBarcodeBlock.Length; i++)
  79. {
  80. string barcode = SiemensS7.Read<string>(PLC.dbBarcodeBlock[i], 11).ToString();
  81. context.Response.Write("条码" + (i + 1).ToString() + ":[" + PLC.dbBarcodeBlock[i] + "]:" + barcode + "\n");
  82. }
  83. string banma = SiemensS7.Read<string>(PLC.dbBanMa, 8).ToString();
  84. SiemensS7.Close();
  85. context.Response.Write("板码:[" + PLC.dbBanMa + "]:" + banma + "\n");
  86. }
  87. public bool IsReusable
  88. {
  89. get
  90. {
  91. return false;
  92. }
  93. }
  94. }