autoPacking6151_db.ashx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = "192.168.0.250";
  14. public static int plcPort = 102;
  15. public static string dbBarcode = "1.2"; //1.0 => 1.2
  16. public static string dbGoodsId = "1.14";
  17. public static string dbGoodsNum = "1.16";
  18. public static string dbResult = "1.18";
  19. public static string dbLogoId = "1.22";//替换产品等级
  20. public static string dbMaterialcode = "1.30";
  21. public static string dbRead = "1.26";
  22. public static string[] dbBarcodeBlock = { "2.2", "2.32", "2.62", "2.92", "2.122", "2.152", "2.182", "2.212", "2.242", "2.272", "2.302", "2.332" };
  23. public static string dbBanMa = "2.362";//2.360 => 2.362
  24. public static string dbUsercode = "2.376"; //150.374 => 150.376
  25. }
  26. public void ProcessRequest(HttpContext context)
  27. {
  28. context.Response.ContentType = "text/plain";
  29. if (context.Request["init"] is object)
  30. {
  31. PlcInit(context);
  32. }
  33. else
  34. {
  35. PlcRead(context);
  36. }
  37. }
  38. public void PlcInit(HttpContext context)
  39. {
  40. SiemensS7.Open(PLC.plcIp, PLC.plcPort);
  41. SiemensS7.Write<short>(PLC.dbRead, (Int16)1).ToString();
  42. SiemensS7.Write<string>(PLC.dbBarcode, "00000000000").ToString();
  43. SiemensS7.Write<string>(PLC.dbMaterialcode, "00000000000000").ToString();
  44. SiemensS7.Write<short>(PLC.dbGoodsId, (Int16)0).ToString();
  45. SiemensS7.Write<short>(PLC.dbLogoId, (Int16)0).ToString();
  46. SiemensS7.Write<short>(PLC.dbGoodsNum, (Int16)0).ToString();
  47. SiemensS7.Write<short>(PLC.dbResult, (Int16)0).ToString();
  48. for (int i = 0; i < PLC.dbBarcodeBlock.Length; i++)
  49. {
  50. SiemensS7.Write<string>(PLC.dbBarcodeBlock[i], "00000000000").ToString();
  51. }
  52. //测试条码
  53. //SiemensS7.Write<string>(PLC.dbBarcodeBlock[0], "20240102500").ToString();
  54. //SiemensS7.Write<string>(PLC.dbBarcodeBlock[1], "20240102501").ToString();
  55. SiemensS7.Write<string>(PLC.dbBanMa, "00000000").ToString();
  56. SiemensS7.Write<string>(PLC.dbUsercode, "DONGKE").ToString();
  57. SiemensS7.Close();
  58. context.Response.Write("INIT OK!");
  59. }
  60. public void PlcRead(HttpContext context)
  61. {
  62. SiemensS7.Open(PLC.plcIp, PLC.plcPort);
  63. context.Response.Write(PLC.plcIp + ":" + PLC.plcPort + "\n\n");
  64. if(context.Request["read"] is object)
  65. {
  66. Int16 readFlag = Convert.ToInt16(context.Request["read"].ToString());
  67. SiemensS7.Write<short>(PLC.dbRead, readFlag);
  68. }
  69. string read = SiemensS7.Read<short>(PLC.dbRead).ToString();
  70. context.Response.Write("读取标识:[" + PLC.dbRead + "]:" + read + "\n");
  71. string bcode = SiemensS7.Read<string>(PLC.dbBarcode, 11).ToString();
  72. context.Response.Write("条码:[" + PLC.dbBarcode + "]:" + bcode + "\n");
  73. string materialcode = SiemensS7.Read<string>(PLC.dbMaterialcode,14).ToString();
  74. context.Response.Write("物料码:[" + PLC.dbMaterialcode + "]:" + materialcode + "\n");
  75. string goodsid = SiemensS7.Read<short>(PLC.dbGoodsId).ToString();
  76. context.Response.Write("产品:[" + PLC.dbGoodsId + "]:" + goodsid + "\n");
  77. string logoid = SiemensS7.Read<short>(PLC.dbLogoId).ToString();
  78. context.Response.Write("商标:[" + PLC.dbLogoId + "]:" + logoid + "\n");
  79. string goodsnum = SiemensS7.Read<short>(PLC.dbGoodsNum).ToString();
  80. context.Response.Write("板数:[" + PLC.dbGoodsNum + "]:" + goodsnum + "\n");
  81. string result = SiemensS7.Read<short>(PLC.dbResult).ToString();
  82. context.Response.Write("结果:[" + PLC.dbResult + "]:" + result + "\n\n");
  83. for (int i = 0; i < PLC.dbBarcodeBlock.Length; i++)
  84. {
  85. string barcode = SiemensS7.Read<string>(PLC.dbBarcodeBlock[i], 11).ToString();
  86. context.Response.Write("条码" + (i + 1).ToString() + ":[" + PLC.dbBarcodeBlock[i] + "]:" + barcode + "\n");
  87. }
  88. string banma = SiemensS7.Read<string>(PLC.dbBanMa, 8).ToString();
  89. context.Response.Write("板码:[" + PLC.dbBanMa + "]:" + banma + "\n");
  90. string usercode = SiemensS7.Read<string>(PLC.dbUsercode, 6).ToString();
  91. context.Response.Write("工号:[" + PLC.dbUsercode + "]:" + usercode + "\n");
  92. SiemensS7.Close();
  93. }
  94. public bool IsReusable
  95. {
  96. get
  97. {
  98. return false;
  99. }
  100. }
  101. }