PLCModuleLogic.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:PLCModuleLogic.cs
  5. * 2.功能描述:发送信息到智能设备。
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2018/10/18 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Data;
  12. using System.Net.Sockets;
  13. using System.Text;
  14. using Dongke.IBOSS.PRD.Basics.DataAccess;
  15. using Dongke.IBOSS.PRD.Service.DataModels;
  16. using Oracle.ManagedDataAccess.Client;
  17. using PCLCommunication;
  18. namespace Dongke.IBOSS.PRD.Service.SmartDeviceService
  19. {
  20. /// <summary>
  21. /// PLC相关处理
  22. /// </summary>
  23. public class PLCModuleLogic
  24. {
  25. /// <summary>
  26. /// 发送产品型号到PLC(坐便盖板)-恒洁三水
  27. /// </summary>
  28. /// <param name="barcode"></param>
  29. /// <param name="sUserInfo"></param>
  30. /// <returns></returns>
  31. public static int SendGoodsCodeToPLCBySeatCover(string barcode, SUserInfo sUserInfo)
  32. {
  33. //filePath = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("PathSetting", filePath);
  34. IDBConnection con = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, DataManager.ConnectionString);
  35. try
  36. {
  37. con.Open();
  38. string sqlString = "select u.plcid, p.ip, p.port,p.PLCDescription from tp_mst_user u " +
  39. "inner join tp_mst_plc p on p.plcid = u.plcid where u.userid = :userid and p.valueflag='1'";
  40. OracleParameter[] paras = new OracleParameter[]
  41. {
  42. new OracleParameter(":userid",OracleDbType.Int32, sUserInfo.UserID,ParameterDirection.Input),
  43. };
  44. DataTable plc = con.GetSqlResultToDt(sqlString, paras);
  45. if (plc == null || plc.Rows.Count == 0)
  46. {
  47. return 4;
  48. }
  49. string ip = plc.Rows[0]["ip"].ToString();
  50. int port = Convert.ToInt32(plc.Rows[0]["port"]);
  51. string dec = plc.Rows[0]["PLCDescription"].ToString();
  52. if (string.IsNullOrWhiteSpace(ip) || port == 0 || string.IsNullOrWhiteSpace(dec))
  53. {
  54. return 5;
  55. }
  56. sqlString = @"select FUN_CMN_GetBarCode(:barcode,null,:accountid) From DUAL";
  57. OracleParameter[] paras1 = new OracleParameter[]
  58. {
  59. new OracleParameter(":barcode",OracleDbType.Varchar2, barcode,ParameterDirection.Input),
  60. new OracleParameter(":accountid",OracleDbType.Int32, sUserInfo.AccountID,ParameterDirection.Input),
  61. };
  62. barcode = con.GetSqlResultToStr(sqlString, paras1);
  63. if (string.IsNullOrEmpty(barcode))
  64. {
  65. return 1;
  66. }
  67. sqlString = "SELECT g.goodscode, g.seatcovercode\n" +
  68. " FROM tp_pm_groutingdailydetail t\n" +
  69. " INNER JOIN tp_mst_goods g\n" +
  70. " ON g.goodsid = t.goodsid\n" +
  71. " WHERE t.barcode = :barcode";
  72. OracleParameter[] paras2 = new OracleParameter[]
  73. {
  74. new OracleParameter(":barcode",OracleDbType.Varchar2, barcode,ParameterDirection.Input),
  75. };
  76. DataTable code = con.GetSqlResultToDt(sqlString, paras2);
  77. if (code == null || code.Rows.Count == 0)
  78. {
  79. return 2;
  80. }
  81. object sccode = code.Rows[0]["seatcovercode"];
  82. if (sccode == null || sccode == DBNull.Value)
  83. {
  84. return 3;
  85. }
  86. //using (MelsecA1EAscii melsec = new MelsecA1EAscii(ip, port))
  87. using (MelsecA1EAsciiSocket melsec = new MelsecA1EAsciiSocket(ip, port))
  88. {
  89. int icode = Convert.ToInt32(sccode);
  90. string type = dec[0].ToString();
  91. int begin = Convert.ToInt32(dec.Substring(1));
  92. // 写入
  93. //bool result = melsec.SetData("D", 1000, 1, icode.ToString("X4"));
  94. bool result = melsec.SetData(type, begin, 1, icode.ToString("X4"));
  95. //// 写入 产品编码
  96. //byte[] byteCode = Encoding.ASCII.GetBytes(goodscode);
  97. //result = melsec.SetData("D", 1001, 10, byteCode);
  98. if (result)
  99. {
  100. return 0;
  101. }
  102. }
  103. return -1;
  104. }
  105. catch (Exception ex)
  106. {
  107. throw ex;
  108. }
  109. finally
  110. {
  111. if (con.ConnState == ConnectionState.Open)
  112. {
  113. con.Close();
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// 发送产品型号到PLC(坐便盖板)-恒洁潮州
  119. /// </summary>
  120. /// <param name="barcode"></param>
  121. /// <param name="sUserInfo"></param>
  122. /// <returns></returns>
  123. public static int SendBarcodeToPLCServer(string barcode, SUserInfo sUserInfo)
  124. {
  125. IDBConnection con = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, DataManager.ConnectionString);
  126. try
  127. {
  128. con.Open();
  129. string sqlString = "select u.plcid, p.ip, p.port,p.PLCDescription from tp_mst_user u " +
  130. "inner join tp_mst_plc p on p.plcid = u.plcid where u.userid = :userid and p.valueflag='1'";
  131. OracleParameter[] paras = new OracleParameter[]
  132. {
  133. new OracleParameter(":userid",OracleDbType.Int32, sUserInfo.UserID,ParameterDirection.Input),
  134. };
  135. DataTable plc = con.GetSqlResultToDt(sqlString, paras);
  136. if (plc == null || plc.Rows.Count == 0)
  137. {
  138. return 4;
  139. }
  140. string ip = plc.Rows[0]["ip"].ToString();
  141. int port = Convert.ToInt32(plc.Rows[0]["port"]);
  142. //string dec = plc.Rows[0]["PLCDescription"].ToString();
  143. if (string.IsNullOrWhiteSpace(ip) || port == 0
  144. //|| string.IsNullOrWhiteSpace(dec)
  145. )
  146. {
  147. return 5;
  148. }
  149. if (string.IsNullOrEmpty(barcode) || barcode.Length != 11)
  150. {
  151. return 1;
  152. }
  153. sqlString = "SELECT t.goodscode\n" +
  154. " FROM tp_pm_groutingdailydetail t\n" +
  155. //" INNER JOIN tp_mst_goods g\n" +
  156. //" ON g.goodsid = t.goodsid\n" +
  157. " WHERE t.barcode = :barcode";
  158. OracleParameter[] paras2 = new OracleParameter[]
  159. {
  160. new OracleParameter(":barcode", OracleDbType.Varchar2, barcode,ParameterDirection.Input),
  161. };
  162. object code = con.GetSqlResultToObj(sqlString, paras2);
  163. if (code == null || code == DBNull.Value)
  164. {
  165. return 2;
  166. }
  167. try
  168. {
  169. using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
  170. {
  171. socket.Connect(ip, port);
  172. //socket.SendTimeout = 3000;
  173. socket.Send(Encoding.ASCII.GetBytes("0001000B" + barcode));
  174. }
  175. }
  176. catch
  177. {
  178. return 3;
  179. }
  180. return 0;
  181. }
  182. catch (Exception ex)
  183. {
  184. throw ex;
  185. }
  186. finally
  187. {
  188. if (con.ConnState == ConnectionState.Open)
  189. {
  190. con.Close();
  191. }
  192. }
  193. }
  194. }
  195. }