PLCModuleLogic.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:PLCModuleLogic.cs
  5. * 2.功能描述:PLC相关处理。
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2018/10/18 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Data;
  12. using Dongke.IBOSS.PRD.Basics.DataAccess;
  13. using Dongke.IBOSS.PRD.Service.DataModels;
  14. using Oracle.DataAccess.Client;
  15. using PCLCommunication;
  16. namespace Dongke.IBOSS.PRD.Service.PDAModuleLogic
  17. {
  18. /// <summary>
  19. /// PLC相关处理
  20. /// </summary>
  21. public class PLCModuleLogic
  22. {
  23. public static int SendGoodsCodeToPLCBySeatCover(string barcode, SUserInfo sUserInfo)
  24. {
  25. //filePath = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("PathSetting", filePath);
  26. IDBConnection con = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, DataManager.ConnectionString);
  27. try
  28. {
  29. con.Open();
  30. string sqlString = "select u.plcid, p.ip, p.port,p.PLCDescription from tp_mst_user u " +
  31. "inner join tp_mst_plc p on p.plcid = u.plcid where u.userid = :userid and p.valueflag='1'";
  32. OracleParameter[] paras = new OracleParameter[]
  33. {
  34. new OracleParameter(":userid",OracleDbType.Int32, sUserInfo.UserID,ParameterDirection.Input),
  35. };
  36. DataTable plc = con.GetSqlResultToDt(sqlString, paras);
  37. if (plc == null || plc.Rows.Count == 0)
  38. {
  39. return 4;
  40. }
  41. string ip = plc.Rows[0]["ip"].ToString();
  42. int port = Convert.ToInt32(plc.Rows[0]["port"]);
  43. string dec = plc.Rows[0]["PLCDescription"].ToString();
  44. if (string.IsNullOrWhiteSpace(ip) || port == 0 || string.IsNullOrWhiteSpace(dec))
  45. {
  46. return 5;
  47. }
  48. sqlString = @"select FUN_CMN_GetBarCode(:barcode,null,:accountid) From DUAL";
  49. OracleParameter[] paras1 = new OracleParameter[]
  50. {
  51. new OracleParameter(":barcode",OracleDbType.Varchar2, barcode,ParameterDirection.Input),
  52. new OracleParameter(":accountid",OracleDbType.Int32, sUserInfo.AccountID,ParameterDirection.Input),
  53. };
  54. barcode = con.GetSqlResultToStr(sqlString, paras1);
  55. if (string.IsNullOrEmpty(barcode))
  56. {
  57. return 1;
  58. }
  59. sqlString = "SELECT g.goodscode, g.seatcovercode\n" +
  60. " FROM tp_pm_groutingdailydetail t\n" +
  61. " INNER JOIN tp_mst_goods g\n" +
  62. " ON g.goodsid = t.goodsid\n" +
  63. " WHERE t.barcode = :barcode";
  64. OracleParameter[] paras2 = new OracleParameter[]
  65. {
  66. new OracleParameter(":barcode",OracleDbType.Varchar2, barcode,ParameterDirection.Input),
  67. };
  68. DataTable code = con.GetSqlResultToDt(sqlString, paras2);
  69. if (code == null || code.Rows.Count == 0)
  70. {
  71. return 2;
  72. }
  73. object sccode = code.Rows[0]["seatcovercode"];
  74. if (sccode == null || sccode == DBNull.Value)
  75. {
  76. return 3;
  77. }
  78. //using (MelsecA1EAscii melsec = new MelsecA1EAscii(ip, port))
  79. using (MelsecA1EAsciiSocket melsec = new MelsecA1EAsciiSocket(ip, port))
  80. {
  81. int icode = Convert.ToInt32(sccode);
  82. string type = dec[0].ToString();
  83. int begin = Convert.ToInt32(dec.Substring(1));
  84. // 写入
  85. //bool result = melsec.SetData("D", 1000, 1, icode.ToString("X4"));
  86. bool result = melsec.SetData(type, begin, 1, icode.ToString("X4"));
  87. //// 写入 产品编码
  88. //byte[] byteCode = Encoding.ASCII.GetBytes(goodscode);
  89. //result = melsec.SetData("D", 1001, 10, byteCode);
  90. if (result)
  91. {
  92. return 0;
  93. }
  94. }
  95. return -1;
  96. }
  97. catch (Exception ex)
  98. {
  99. throw ex;
  100. }
  101. finally
  102. {
  103. if (con.ConnState == ConnectionState.Open)
  104. {
  105. con.Close();
  106. }
  107. }
  108. }
  109. }
  110. }