autoPacking6151_db.ashx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 static S7 s7 = new S7();
  27. public void ProcessRequest(HttpContext context)
  28. {
  29. context.Response.ContentType = "text/plain";
  30. if (context.Request["init"] is object)
  31. {
  32. PlcInit(context);
  33. }
  34. else
  35. {
  36. PlcRead(context);
  37. }
  38. }
  39. public void PlcInit(HttpContext context)
  40. {
  41. s7.Open(PLC.plcIp, PLC.plcPort);
  42. s7.Write<short>(PLC.dbRead, (Int16)1).ToString();
  43. s7.Write<string>(PLC.dbBarcode, "00000000000").ToString();
  44. s7.Write<string>(PLC.dbMaterialcode, "00000000000000").ToString();
  45. s7.Write<short>(PLC.dbGoodsId, (Int16)0).ToString();
  46. s7.Write<short>(PLC.dbLogoId, (Int16)0).ToString();
  47. s7.Write<short>(PLC.dbGoodsNum, (Int16)0).ToString();
  48. s7.Write<short>(PLC.dbResult, (Int16)0).ToString();
  49. for (int i = 0; i < PLC.dbBarcodeBlock.Length; i++)
  50. {
  51. s7.Write<string>(PLC.dbBarcodeBlock[i], "00000000000").ToString();
  52. }
  53. //测试条码
  54. s7.Write<string>(PLC.dbBarcodeBlock[0], "10016782613").ToString();
  55. //s7.Write<string>(PLC.dbBarcodeBlock[1], "00000000000").ToString();
  56. s7.Write<string>(PLC.dbBanMa, "00000000").ToString();
  57. s7.Write<string>(PLC.dbUsercode, "DONGKE").ToString();
  58. //s7.Close();
  59. context.Response.Write("INIT OK!");
  60. }
  61. public void PlcRead(HttpContext context)
  62. {
  63. s7.Open(PLC.plcIp, PLC.plcPort);
  64. context.Response.Write(PLC.plcIp + ":" + PLC.plcPort + "\n\n");
  65. if(context.Request["read"] is object)
  66. {
  67. Int16 readFlag = Convert.ToInt16(context.Request["read"].ToString());
  68. s7.Write<short>(PLC.dbRead, readFlag);
  69. }
  70. string read = s7.Read<short>(PLC.dbRead).ToString();
  71. context.Response.Write("读取标识:[" + PLC.dbRead + "]:" + read + "\n");
  72. string bcode = s7.Read<string>(PLC.dbBarcode, 11).ToString();
  73. context.Response.Write("条码:[" + PLC.dbBarcode + "]:" + bcode + "\n");
  74. string materialcode = s7.Read<string>(PLC.dbMaterialcode,14).ToString();
  75. context.Response.Write("物料码:[" + PLC.dbMaterialcode + "]:" + materialcode + "\n");
  76. string goodsid = s7.Read<short>(PLC.dbGoodsId).ToString();
  77. context.Response.Write("产品:[" + PLC.dbGoodsId + "]:" + goodsid + "\n");
  78. string logoid = s7.Read<short>(PLC.dbLogoId).ToString();
  79. context.Response.Write("商标:[" + PLC.dbLogoId + "]:" + logoid + "\n");
  80. string goodsnum = s7.Read<short>(PLC.dbGoodsNum).ToString();
  81. context.Response.Write("板数:[" + PLC.dbGoodsNum + "]:" + goodsnum + "\n");
  82. string result = s7.Read<short>(PLC.dbResult).ToString();
  83. context.Response.Write("结果:[" + PLC.dbResult + "]:" + result + "\n\n");
  84. for (int i = 0; i < PLC.dbBarcodeBlock.Length; i++)
  85. {
  86. string barcode = s7.Read<string>(PLC.dbBarcodeBlock[i], 11).ToString();
  87. context.Response.Write("条码" + (i + 1).ToString() + ":[" + PLC.dbBarcodeBlock[i] + "]:" + barcode + "\n");
  88. }
  89. string banma = s7.Read<string>(PLC.dbBanMa, 8).ToString();
  90. context.Response.Write("板码:[" + PLC.dbBanMa + "]:" + banma + "\n");
  91. string usercode = s7.Read<string>(PLC.dbUsercode, 6).ToString();
  92. context.Response.Write("工号:[" + PLC.dbUsercode + "]:" + usercode + "\n");
  93. //s7.Close();
  94. }
  95. public bool IsReusable
  96. {
  97. get
  98. {
  99. return false;
  100. }
  101. }
  102. }