PLCWorkLogic..cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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.Collections;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Net.Sockets;
  15. using System.Text;
  16. using Curtain.Core;
  17. using Curtain.DataAccess;
  18. using Curtain.Framework.Json;
  19. using Curtain.Net.Sockets.PLC;
  20. using Curtain.Net.Sockets.PLC.Model.Siemens;
  21. using Dongke.IBOSS.PRD.Basics.BaseResources;
  22. using Dongke.IBOSS.PRD.Basics.DataAccess;
  23. using Dongke.IBOSS.PRD.Service.DataModels;
  24. using Dongke.IBOSS.PRD.WCF.DataModels;
  25. using Microsoft.Win32;
  26. using Newtonsoft.Json.Linq;
  27. namespace Dongke.IBOSS.PRD.Service.CMNModuleService
  28. {
  29. /// <summary>
  30. /// PLC相关处理
  31. /// </summary>
  32. public class PLCModuleLogic
  33. {
  34. #region 恒洁三水三车间工位对应PLC操作
  35. /// <summary>
  36. /// 恒洁三水三车间工位对应PLC操作 xuwei modify 2020-06-29
  37. /// </summary>
  38. /// <param name="sUserInfo"></param>
  39. /// <returns></returns>
  40. public static ServiceResultEntity DoPLCWorkByUser_HEGII_S3(SUserInfo sUserInfo, string barcode,
  41. int procedureid, int workStationType = 0, decimal weight = 0)
  42. {
  43. using (IDataAccess conn = DataAccess.Create())
  44. {
  45. //调用PLC的参数集合
  46. Hashtable plcPara = new Hashtable();
  47. try
  48. {
  49. //按当前登录用户在工位表中的当前工号查找工位类型
  50. workStationType = getWorkStationType(conn, sUserInfo.UserCode);
  51. //成检交接3211
  52. if (workStationType == 3211)
  53. {
  54. //读取产品型号
  55. object goodsModel = conn.ExecuteScalar(@"
  56. SELECT
  57. g.SEATCOVERCODE
  58. FROM
  59. TP_PM_GROUTINGDAILYDETAIL p
  60. LEFT JOIN TP_MST_GOODS g ON g.GOODSID = p.GOODSID
  61. WHERE
  62. p.VALUEFLAG = '1'
  63. AND p.BARCODE = @BARCODE@
  64. ",
  65. new CDAParameter("BARCODE", barcode)
  66. );
  67. plcPara.Add("usercode", sUserInfo.UserCode);
  68. plcPara.Add("barcode", barcode);
  69. plcPara.Add("procedureid", procedureid);
  70. plcPara.Add("goodsmodel", goodsModel);
  71. plcPara.Add("finishflag", 1);
  72. return plcWrite(conn, sUserInfo.UserCode, workStationType, plcPara);
  73. }
  74. // 成检二检3212
  75. // 读取plc重量 add by fubin 2020-7-20
  76. if (workStationType == 3212)
  77. {
  78. if (weight > 0)
  79. {
  80. try
  81. {
  82. conn.ExecuteNonQuery(@"
  83. INSERT INTO tp_pm_goodsweighing
  84. (barcode
  85. ,groutingdailydetailid
  86. ,smartdevicepointcode
  87. ,procedureid
  88. ,goodsweight
  89. ,accountid
  90. ,createuserid)
  91. SELECT @barcode@
  92. ,gd.groutingdailydetailid
  93. ,'3_2_2'
  94. ,@procedureid@
  95. ,@goodsweight@
  96. ,@accountid@
  97. ,@userid@
  98. FROM tp_pm_groutingdailydetail gd
  99. WHERE gd.barcode = @barcode@",
  100. new CDAParameter("barcode", barcode),
  101. new CDAParameter("procedureid", procedureid),
  102. new CDAParameter("goodsweight", weight),
  103. new CDAParameter("accountid", sUserInfo.AccountID),
  104. new CDAParameter("userid", sUserInfo.UserID)
  105. );
  106. }
  107. catch (Exception)
  108. {
  109. return new ServiceResultEntity()
  110. {
  111. Status = Constant.ServiceResultStatus.Other,
  112. Message = "重量写入失败!"
  113. };
  114. }
  115. plcPara.Add("finishflag", 1);
  116. return plcWrite(conn, sUserInfo.UserCode, workStationType, plcPara);
  117. }
  118. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success };
  119. }
  120. // 3#上水3320
  121. if (workStationType == 3320)
  122. {
  123. //读取产品型号
  124. object goodsModel = conn.ExecuteScalar(@"
  125. SELECT
  126. g.SEATCOVERCODE
  127. FROM
  128. TP_PM_GROUTINGDAILYDETAIL p
  129. LEFT JOIN TP_MST_GOODS g ON g.GOODSID = p.GOODSID
  130. WHERE
  131. p.VALUEFLAG = '1'
  132. AND p.BARCODE = @BARCODE@
  133. ",
  134. new CDAParameter("BARCODE", barcode)
  135. );
  136. if (goodsModel.ToInt32() == 0)
  137. {
  138. plcPara.Add("goodsmodel", 0);
  139. plcPara.Add("finishflag", 1);
  140. plcPara.Add("error", 1);
  141. }
  142. else
  143. {
  144. plcPara.Add("goodsmodel", goodsModel);
  145. plcPara.Add("finishflag", 1);
  146. plcPara.Add("error", 0);
  147. }
  148. return plcWrite(conn, sUserInfo.UserCode, workStationType, plcPara);
  149. }
  150. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
  151. }
  152. catch (Exception ex)
  153. {
  154. Curtain.Log.Logger.Error(ex);
  155. string plcParaStr = "";
  156. foreach (string key in plcPara.Keys) plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
  157. Curtain.Log.Logger.Debug("PLC写入失败!" + plcParaStr);
  158. return new ServiceResultEntity()
  159. {
  160. Status = Constant.ServiceResultStatus.Other,
  161. Message = "PLC写入失败!" + plcParaStr
  162. };
  163. }
  164. finally
  165. {
  166. conn?.Close();
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// 恒洁三水三车间成检二检获取PLC重量 fubin add 2020-07-20
  172. /// </summary>
  173. /// <param name="sUserInfo"></param>
  174. /// <returns></returns>
  175. public static ServiceResultEntity ReadPLCWeightByUser_HEGII_S3(SUserInfo sUserInfo)
  176. {
  177. using (IDataAccess conn = DataAccess.Create())
  178. {
  179. //调用PLC的参数集合
  180. Hashtable plcPara = new Hashtable();
  181. try
  182. {
  183. //按当前登录用户在工位表中的当前工号查找工位类型
  184. int workStationType = getWorkStationType(conn, sUserInfo.UserCode);
  185. // 成检二检3212
  186. // 读取plc重量 add by fubin 2020-7-20
  187. if (workStationType == 3212)
  188. {
  189. return plcRead(conn, sUserInfo.UserCode, workStationType);
  190. }
  191. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Result = 0 };
  192. }
  193. catch (Exception ex)
  194. {
  195. Curtain.Log.Logger.Error(ex);
  196. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Result = 0 };
  197. }
  198. finally
  199. {
  200. conn?.Close();
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// 获取工位类型ID xuwei add 2020-06-29
  206. /// </summary>
  207. /// <param name="conn"></param>
  208. /// <param name="userCode"></param>
  209. /// <returns></returns>
  210. private static int getWorkStationType(IDataAccess conn, string userCode)
  211. {
  212. return Convert.ToInt32(conn.ExecuteScalar(@"
  213. SELECT
  214. t.WORKSTATIONTYPEID
  215. FROM
  216. TP_MST_WORKSTATION w
  217. LEFT JOIN TP_SYS_WORKSTATIONTYPE t ON w.WORKSTATIONTYPEID = t.WORKSTATIONTYPEID
  218. WHERE
  219. w.USERCODE = @USERCODE@
  220. ",
  221. new CDAParameter("USERCODE", userCode)
  222. ));
  223. }
  224. /// <summary>
  225. /// 写PLC xuwei add 2020-06-29
  226. /// </summary>
  227. /// <param name="conn"></param>
  228. /// <param name="userCode"></param>
  229. /// <param name="workStationType"></param>
  230. /// <param name="plcPara"></param>
  231. /// <returns></returns>
  232. private static ServiceResultEntity plcWrite(IDataAccess conn, string userCode, int workStationType, Hashtable plcPara)
  233. {
  234. //读取工位配置信息
  235. DataTable dataTable = conn.ExecuteDatatable(@"
  236. SELECT
  237. *
  238. FROM
  239. TP_MST_WORKSTATION t
  240. WHERE
  241. t.WORKSTATIONTYPEID = @WORKSTATIONTYPE@
  242. AND USERCODE =@USERCODE@
  243. ",
  244. new CDAParameter("WORKSTATIONTYPE", workStationType),
  245. new CDAParameter("USERCODE", userCode)
  246. );
  247. if (dataTable == null || dataTable.Rows.Count == 0)
  248. {
  249. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
  250. }
  251. string ip = dataTable.Rows[0]["PLCIP"] + "";
  252. int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
  253. JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
  254. //写入PLC
  255. using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  256. {
  257. //测试静态数据 测试成功
  258. //plc.Connect(ip, port);
  259. //plc.Write<string>("D", "102.2", "2020070101");
  260. //plc.Write<short>("D", "102.14", Convert.ToInt16(10));
  261. //手动写
  262. //plc.Connect(ip, port);
  263. //plc.Write<string>("D", "102.2", plcPara["barcode"].ToString());
  264. //plc.Write<short>("D", "102.14", Convert.ToInt16(plcPara["goodsmodel"]));
  265. //自动按配置写
  266. plc.Connect(ip, port);
  267. for (int i = 0; i < plcObj.Count; i++)
  268. {
  269. if (plcObj[i]["type"].ToString().ToLower() == "string")
  270. plc.Write<string>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), plcPara[plcObj[i]["name"].ToString()].ToString());
  271. else if (plcObj[i]["type"].ToString().ToLower() == "int")
  272. plc.Write<short>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(plcPara[plcObj[i]["name"].ToString()]));
  273. }
  274. }
  275. //写入操作成功日志
  276. string plcParaStr = "";
  277. foreach (string key in plcPara.Keys) plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
  278. Curtain.Log.Logger.Debug("PLC写入成功!" + plcParaStr);
  279. //PLC操作成功
  280. return new ServiceResultEntity()
  281. {
  282. Status = Constant.ServiceResultStatus.Success,
  283. Message = "" //一定不可以有值,为空时才是提交成功
  284. };
  285. }
  286. /// <summary>
  287. /// 读取PLC
  288. /// </summary>
  289. /// <param name="conn"></param>
  290. /// <param name="workStationID"></param>
  291. /// <returns></returns>
  292. private static ServiceResultEntity plcRead(IDataAccess conn, string userCode, int workStationType)
  293. {
  294. try
  295. {
  296. //读取工位配置信息
  297. DataTable dataTable = conn.ExecuteDatatable(@"
  298. SELECT
  299. *
  300. FROM
  301. TP_MST_WORKSTATION t
  302. WHERE
  303. t.WORKSTATIONTYPEID = @WORKSTATIONTYPE@
  304. AND USERCODE =@USERCODE@
  305. ",
  306. new CDAParameter("WORKSTATIONTYPE", workStationType),
  307. new CDAParameter("USERCODE", userCode)
  308. );
  309. if (dataTable == null || dataTable.Rows.Count == 0)
  310. {
  311. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
  312. }
  313. string ip = dataTable.Rows[0]["PLCIP"] + "";
  314. int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
  315. JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECTREAD"].ToString());
  316. // 读取PLC
  317. using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  318. {
  319. //自动按配置写
  320. plc.Connect(ip, port);
  321. for (int i = 0; i < plcObj.Count; i++)
  322. {
  323. if (plcObj[i]["type"].ToString().ToLower() == "float")
  324. {
  325. PLCResult<float> result = plc.Read<float>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString());
  326. plcObj[i]["weight"] = result.Data;
  327. }
  328. }
  329. }
  330. //写入操作成功日志
  331. Curtain.Log.Logger.Debug("PLC读取成功!" + plcObj.ToJson());
  332. //PLC操作成功
  333. return new ServiceResultEntity()
  334. {
  335. Status = Constant.ServiceResultStatus.Success,
  336. Message = "", //一定不可以有值,为空时才是提交成功
  337. Result = plcObj
  338. };
  339. }
  340. catch (Exception ex)
  341. {
  342. throw ex;
  343. }
  344. }
  345. #endregion
  346. /// <summary>
  347. /// 写PLC,通过工位类型
  348. /// </summary>
  349. /// <param name="conn"></param>
  350. /// <param name="workStationType"></param>
  351. /// <param name="plcPara"></param>
  352. /// <returns></returns>
  353. public static ServiceResultEntity PlcWriteByWorkStationType(int workStationType, Hashtable plcPara)
  354. {
  355. using (IDataAccess conn = DataAccess.Create())
  356. {
  357. //读取工位配置信息
  358. DataTable dataTable = conn.ExecuteDatatable(@"
  359. SELECT
  360. *
  361. FROM
  362. TP_MST_WORKSTATION t
  363. WHERE
  364. t.WORKSTATIONTYPEID = @WORKSTATIONTYPE@
  365. ",
  366. new CDAParameter("WORKSTATIONTYPE", workStationType)
  367. );
  368. if (dataTable == null || dataTable.Rows.Count == 0)
  369. {
  370. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "无当前工位信息" };
  371. }
  372. string ip = dataTable.Rows[0]["PLCIP"] + "";
  373. int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
  374. JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
  375. //写入PLC
  376. using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  377. {
  378. //自动按配置写
  379. plc.Connect(ip, port);
  380. for (int i = 0; i < plcObj.Count; i++)
  381. {
  382. if (plcObj[i]["type"].ToString().ToLower() == "string")
  383. plc.Write<string>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), plcPara[plcObj[i]["name"].ToString()].ToString());
  384. else if (plcObj[i]["type"].ToString().ToLower() == "int")
  385. plc.Write<short>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(plcPara[plcObj[i]["name"].ToString()]));
  386. else if (plcObj[i]["type"].ToString().ToLower() == "bool")
  387. plc.Write<bool>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToBoolean(plcPara[plcObj[i]["name"].ToString()]));
  388. }
  389. }
  390. //写入操作成功日志
  391. string plcParaStr = "";
  392. foreach (string key in plcPara.Keys) plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
  393. Curtain.Log.Logger.Debug("PLC写入成功!" + plcParaStr);
  394. //PLC操作成功
  395. return new ServiceResultEntity()
  396. {
  397. Status = Constant.ServiceResultStatus.Success,
  398. Message = "" //一定不可以有值,为空时才是提交成功
  399. };
  400. }
  401. }
  402. }
  403. }