PLCWorkLogic..cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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.DataAccess;
  17. using Curtain.Extension.ExObjectConvert;
  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. DataTable dataTable = GetWorkStationInfo(conn, sUserInfo.UserCode);
  52. if (dataTable == null || dataTable.Rows.Count == 0)
  53. {
  54. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
  55. }
  56. if (dataTable.Columns.Contains("PLC_FLAG"))
  57. {
  58. string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
  59. if (plc_flag != "1")
  60. {
  61. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  62. }
  63. }
  64. workStationType = Convert.ToInt32(dataTable.Rows[0]["WORKSTATIONTYPEID"]);
  65. //成检交接3211
  66. if (workStationType == 3211)
  67. {
  68. //读取产品型号
  69. object goodsModel = conn.ExecuteScalar(@"
  70. SELECT
  71. g.SEATCOVERCODE
  72. FROM
  73. TP_PM_GROUTINGDAILYDETAIL p
  74. LEFT JOIN TP_MST_GOODS g ON g.GOODSID = p.GOODSID
  75. WHERE
  76. p.VALUEFLAG = '1'
  77. AND p.BARCODE = @BARCODE@
  78. ",
  79. new CDAParameter("BARCODE", barcode)
  80. );
  81. plcPara.Add("usercode", sUserInfo.UserCode);
  82. plcPara.Add("barcode", barcode);
  83. plcPara.Add("procedureid", procedureid);
  84. plcPara.Add("goodsmodel", goodsModel);
  85. plcPara.Add("finishflag", 1);
  86. return plcWrite(conn, sUserInfo.UserCode, workStationType, plcPara);
  87. }
  88. // 成检二检3212
  89. // 读取plc重量 add by fubin 2020-7-20
  90. if (workStationType == 3212)
  91. {
  92. if (weight > 0)
  93. {
  94. try
  95. {
  96. conn.ExecuteNonQuery(@"
  97. INSERT INTO tp_pm_goodsweighing
  98. (barcode
  99. ,groutingdailydetailid
  100. ,smartdevicepointcode
  101. ,procedureid
  102. ,goodsweight
  103. ,accountid
  104. ,createuserid)
  105. SELECT @barcode@
  106. ,gd.groutingdailydetailid
  107. ,'3_2_2'
  108. ,@procedureid@
  109. ,@goodsweight@
  110. ,@accountid@
  111. ,@userid@
  112. FROM tp_pm_groutingdailydetail gd
  113. WHERE gd.barcode = @barcode@",
  114. new CDAParameter("barcode", barcode),
  115. new CDAParameter("procedureid", procedureid),
  116. new CDAParameter("goodsweight", weight),
  117. new CDAParameter("accountid", sUserInfo.AccountID),
  118. new CDAParameter("userid", sUserInfo.UserID)
  119. );
  120. }
  121. catch (Exception)
  122. {
  123. return new ServiceResultEntity()
  124. {
  125. Status = Constant.ServiceResultStatus.Other,
  126. Message = "重量写入失败!"
  127. };
  128. }
  129. plcPara.Add("finishflag", 1);
  130. return plcWrite(conn, sUserInfo.UserCode, workStationType, plcPara);
  131. }
  132. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success };
  133. }
  134. // 3#上水3320
  135. if (workStationType == 3320)
  136. {
  137. //读取产品型号
  138. object goodsModel = conn.ExecuteScalar(@"
  139. SELECT
  140. g.SEATCOVERCODE
  141. FROM
  142. TP_PM_GROUTINGDAILYDETAIL p
  143. LEFT JOIN TP_MST_GOODS g ON g.GOODSID = p.GOODSID
  144. WHERE
  145. p.VALUEFLAG = '1'
  146. AND p.BARCODE = @BARCODE@
  147. ",
  148. new CDAParameter("BARCODE", barcode)
  149. );
  150. if (goodsModel.ToInt32() == 0)
  151. {
  152. plcPara.Add("goodsmodel", 0);
  153. plcPara.Add("finishflag", 1);
  154. plcPara.Add("error", 1);
  155. }
  156. else
  157. {
  158. plcPara.Add("goodsmodel", goodsModel);
  159. plcPara.Add("finishflag", 1);
  160. plcPara.Add("error", 0);
  161. }
  162. return plcWrite(conn, sUserInfo.UserCode, workStationType, plcPara);
  163. }
  164. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
  165. }
  166. catch (Exception ex)
  167. {
  168. Curtain.Log.Logger.Error(ex);
  169. string plcParaStr = "";
  170. foreach (string key in plcPara.Keys) plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
  171. #if DEBUG
  172. Curtain.Log.Logger.Debug("PLC写入失败!" + plcParaStr);
  173. #endif
  174. return new ServiceResultEntity()
  175. {
  176. Status = Constant.ServiceResultStatus.Other,
  177. Message = "PLC写入失败!" + plcParaStr
  178. };
  179. }
  180. finally
  181. {
  182. conn?.Close();
  183. }
  184. }
  185. }
  186. /// <summary>
  187. /// 恒洁三水三车间成检二检获取PLC重量 fubin add 2020-07-20
  188. /// </summary>
  189. /// <param name="sUserInfo"></param>
  190. /// <returns></returns>
  191. public static ServiceResultEntity ReadPLCWeightByUser_HEGII_S3(SUserInfo sUserInfo)
  192. {
  193. using (IDataAccess conn = DataAccess.Create())
  194. {
  195. //调用PLC的参数集合
  196. Hashtable plcPara = new Hashtable();
  197. try
  198. {
  199. //按当前登录用户在工位表中的当前工号查找工位类型
  200. int workStationType = getWorkStationType(conn, sUserInfo.UserCode);
  201. // 成检二检3212
  202. // 读取plc重量 add by fubin 2020-7-20
  203. if (workStationType == 3212)
  204. {
  205. return plcRead(conn, sUserInfo.UserCode, workStationType);
  206. }
  207. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Result = 0 };
  208. }
  209. catch (Exception ex)
  210. {
  211. Curtain.Log.Logger.Error(ex);
  212. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Result = 0 };
  213. }
  214. finally
  215. {
  216. conn?.Close();
  217. }
  218. }
  219. }
  220. /// <summary>
  221. /// 获取工位类型ID xuwei add 2020-06-29
  222. /// </summary>
  223. /// <param name="conn"></param>
  224. /// <param name="userCode"></param>
  225. /// <returns></returns>
  226. private static int getWorkStationType(IDataAccess conn, string userCode)
  227. {
  228. return Convert.ToInt32(conn.ExecuteScalar(@"
  229. SELECT
  230. t.WORKSTATIONTYPEID
  231. FROM
  232. TP_MST_WORKSTATION w
  233. LEFT JOIN TP_SYS_WORKSTATIONTYPE t ON w.WORKSTATIONTYPEID = t.WORKSTATIONTYPEID
  234. WHERE
  235. w.USERCODE = @USERCODE@
  236. ",
  237. new CDAParameter("USERCODE", userCode)
  238. ));
  239. }
  240. private static DataTable GetWorkStationInfo(IDataAccess conn, string userCode)
  241. {
  242. DataTable dataTable = conn.ExecuteDatatable(@"
  243. SELECT
  244. t.workstationid,
  245. t.workstationtypeid,
  246. t.workstationname,
  247. t.procedureid,
  248. t.groutinglineid,
  249. t.usercode,
  250. t.pccode,
  251. t.plcip,
  252. t.plcport,
  253. t.plcobject,
  254. t.valueflag,
  255. t.plcobjectread,
  256. t.plc_flag
  257. FROM
  258. TP_MST_WORKSTATION t
  259. WHERE
  260. t.USERCODE =@USERCODE@
  261. ",
  262. //new CDAParameter("WORKSTATIONTYPE", workStationType),
  263. new CDAParameter("USERCODE", userCode)
  264. );
  265. return dataTable;
  266. }
  267. /// <summary>
  268. /// 写PLC xuwei add 2020-06-29
  269. /// </summary>
  270. /// <param name="conn"></param>
  271. /// <param name="userCode"></param>
  272. /// <param name="workStationType"></param>
  273. /// <param name="plcPara"></param>
  274. /// <returns></returns>
  275. private static ServiceResultEntity plcWrite(IDataAccess conn, string userCode, int workStationType, Hashtable plcPara)
  276. {
  277. //读取工位配置信息
  278. DataTable dataTable = conn.ExecuteDatatable(@"
  279. SELECT --t.*
  280. t.workstationtypeid,
  281. t.workstationname,
  282. t.procedureid,
  283. t.groutinglineid,
  284. t.usercode,
  285. t.pccode,
  286. t.plcip,
  287. t.plcport,
  288. t.plcobject,
  289. t.valueflag,
  290. t.plcobjectread,
  291. t.plc_flag
  292. FROM
  293. TP_MST_WORKSTATION t
  294. WHERE
  295. t.WORKSTATIONTYPEID = @WORKSTATIONTYPE@
  296. AND t.USERCODE =@USERCODE@
  297. ",
  298. new CDAParameter("WORKSTATIONTYPE", workStationType),
  299. new CDAParameter("USERCODE", userCode)
  300. );
  301. if (dataTable == null || dataTable.Rows.Count == 0)
  302. {
  303. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
  304. }
  305. if (dataTable.Columns.Contains("PLC_FLAG"))
  306. {
  307. string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
  308. if (plc_flag != "1")
  309. {
  310. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  311. }
  312. }
  313. string ip = dataTable.Rows[0]["PLCIP"] + "";
  314. if (string.IsNullOrWhiteSpace(ip))
  315. {
  316. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  317. }
  318. int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
  319. JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
  320. //写入PLC
  321. using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  322. {
  323. //测试静态数据 测试成功
  324. //plc.Connect(ip, port);
  325. //plc.Write<string>("D", "102.2", "2020070101");
  326. //plc.Write<short>("D", "102.14", Convert.ToInt16(10));
  327. //手动写
  328. //plc.Connect(ip, port);
  329. //plc.Write<string>("D", "102.2", plcPara["barcode"].ToString());
  330. //plc.Write<short>("D", "102.14", Convert.ToInt16(plcPara["goodsmodel"]));
  331. //自动按配置写
  332. plc.Connect(ip, port);
  333. for (int i = 0; i < plcObj.Count; i++)
  334. {
  335. if (plcObj[i]["type"].ToString().ToLower() == "string")
  336. plc.Write<string>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), plcPara[plcObj[i]["name"].ToString()].ToString());
  337. else if (plcObj[i]["type"].ToString().ToLower() == "int")
  338. plc.Write<short>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(plcPara[plcObj[i]["name"].ToString()]));
  339. }
  340. }
  341. #if DEBUG
  342. //写入操作成功日志
  343. string plcParaStr = "";
  344. foreach (string key in plcPara.Keys) plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
  345. Curtain.Log.Logger.Debug("PLC写入成功!" + plcParaStr);
  346. #endif
  347. //PLC操作成功
  348. return new ServiceResultEntity()
  349. {
  350. Status = Constant.ServiceResultStatus.Success,
  351. Message = "" //一定不可以有值,为空时才是提交成功
  352. };
  353. }
  354. /// <summary>
  355. /// 读取PLC
  356. /// </summary>
  357. /// <param name="conn"></param>
  358. /// <param name="workStationID"></param>
  359. /// <returns></returns>
  360. private static ServiceResultEntity plcRead(IDataAccess conn, string userCode, int workStationType)
  361. {
  362. try
  363. {
  364. //读取工位配置信息
  365. DataTable dataTable = conn.ExecuteDatatable(@"
  366. SELECT --t.*
  367. t.workstationtypeid,
  368. t.workstationname,
  369. t.procedureid,
  370. t.groutinglineid,
  371. t.usercode,
  372. t.pccode,
  373. t.plcip,
  374. t.plcport,
  375. t.plcobject,
  376. t.valueflag,
  377. t.plcobjectread,
  378. t.plc_flag
  379. FROM
  380. TP_MST_WORKSTATION t
  381. WHERE
  382. t.WORKSTATIONTYPEID = @WORKSTATIONTYPE@
  383. AND t.USERCODE =@USERCODE@
  384. ",
  385. new CDAParameter("WORKSTATIONTYPE", workStationType),
  386. new CDAParameter("USERCODE", userCode)
  387. );
  388. if (dataTable == null || dataTable.Rows.Count == 0)
  389. {
  390. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
  391. }
  392. if (dataTable.Columns.Contains("PLC_FLAG"))
  393. {
  394. string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
  395. if (plc_flag != "1")
  396. {
  397. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  398. }
  399. }
  400. string ip = dataTable.Rows[0]["PLCIP"] + "";
  401. if (string.IsNullOrWhiteSpace(ip))
  402. {
  403. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  404. }
  405. int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
  406. JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECTREAD"].ToString());
  407. // 读取PLC
  408. using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  409. {
  410. //自动按配置写
  411. plc.Connect(ip, port);
  412. for (int i = 0; i < plcObj.Count; i++)
  413. {
  414. if (plcObj[i]["type"].ToString().ToLower() == "float")
  415. {
  416. PLCResult<float> result = plc.Read<float>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString());
  417. plcObj[i]["weight"] = result.Data;
  418. }
  419. }
  420. }
  421. #if DEBUG
  422. //写入操作成功日志
  423. Curtain.Log.Logger.Debug("PLC读取成功!" + plcObj.ToJson());
  424. #endif
  425. //PLC操作成功
  426. return new ServiceResultEntity()
  427. {
  428. Status = Constant.ServiceResultStatus.Success,
  429. Message = "", //一定不可以有值,为空时才是提交成功
  430. Result = plcObj
  431. };
  432. }
  433. catch (Exception ex)
  434. {
  435. throw ex;
  436. }
  437. }
  438. #endregion
  439. /// <summary>
  440. /// 写PLC2
  441. /// </summary>
  442. /// <param name="workStationType"></param>
  443. /// <param name="plcPara"></param>
  444. /// <param name="userCode"></param>
  445. /// <returns></returns>
  446. public static ServiceResultEntity PlcWrite2(int workStationType, Hashtable plcPara, string userCode = null)
  447. {
  448. using (IDataAccess conn = DataAccess.Create())
  449. {
  450. List<CDAParameter> paras = new List<CDAParameter>();
  451. #region 读取工位配置信息
  452. string sql =
  453. "SELECT --t.*\n" + @"
  454. t.workstationtypeid,
  455. t.workstationname,
  456. t.procedureid,
  457. t.groutinglineid,
  458. t.usercode,
  459. t.pccode,
  460. t.plcip,
  461. t.plcport,
  462. t.plcobject,
  463. t.valueflag,
  464. t.plcobjectread,
  465. t.plc_flag" +
  466. " FROM tp_mst_workstation t\n" +
  467. " WHERE t.workstationtypeid = @workstationtype@";
  468. paras.Add(new CDAParameter("workstationtype", workStationType));
  469. if (!string.IsNullOrEmpty(userCode))
  470. {
  471. sql += " AND t.usercode = @usercode@";
  472. paras.Add(new CDAParameter("usercode", userCode));
  473. }
  474. DataTable dataTable = conn.ExecuteDatatable(sql, paras.ToArray());
  475. if (dataTable == null || dataTable.Rows.Count == 0)
  476. {
  477. //xuwei modify 2022-02-18
  478. //return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "无当前工位信息" };
  479. return new ServiceResultEntity()
  480. {
  481. Status = Constant.ServiceResultStatus.Success,
  482. Message = "" //一定不可以有值,为空时才是提交成功
  483. };
  484. //xuwei end
  485. }
  486. if (dataTable.Columns.Contains("PLC_FLAG"))
  487. {
  488. string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
  489. if (plc_flag != "1")
  490. {
  491. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  492. }
  493. }
  494. #endregion
  495. string ip = dataTable.Rows[0]["PLCIP"] + "";
  496. if (string.IsNullOrWhiteSpace(ip))
  497. {
  498. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  499. }
  500. int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
  501. JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
  502. //xuwei modify 2021-11-23 写入PLC增加try catch 获得准确提示===================
  503. try
  504. {
  505. //写入PLC
  506. using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  507. {
  508. //自动按配置写
  509. plc.Connect(ip, port);
  510. for (int i = 0; i < plcObj.Count; i++)
  511. {
  512. if (plcObj[i]["type"].ToString().ToLower() == "string")
  513. plc.Write<string>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), plcPara[plcObj[i]["name"].ToString()].ToString());
  514. else if (plcObj[i]["type"].ToString().ToLower() == "int")
  515. plc.Write<short>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(plcPara[plcObj[i]["name"].ToString()]));
  516. else if (plcObj[i]["type"].ToString().ToLower() == "bool")
  517. plc.Write<bool>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToBoolean(plcPara[plcObj[i]["name"].ToString()]));
  518. }
  519. }
  520. //PLC操作成功
  521. return new ServiceResultEntity()
  522. {
  523. Status = Constant.ServiceResultStatus.Success,
  524. Message = "" //一定不可以有值,为空时才是提交成功
  525. };
  526. }
  527. catch
  528. {
  529. //PLC写入失败!
  530. return new ServiceResultEntity()
  531. {
  532. Status = Constant.ServiceResultStatus.Other,
  533. Message = "PLC(" + ip + ")写入错误!" //一定不可以有值,为空时才是提交成功
  534. };
  535. }
  536. //=======================================================================
  537. }
  538. }
  539. /// <summary>
  540. /// 写PLC 成型线放行
  541. /// </summary>
  542. /// <param name="workStationType"></param>
  543. /// <param name="plcPara"></param>
  544. /// <param name="userCode"></param>
  545. /// <returns></returns>
  546. public static ServiceResultEntity PlcWrite_GL(int workStationType, int gl_id,
  547. Hashtable plcPara, string userCode = null)
  548. {
  549. using (IDataAccess conn = DataAccess.Create())
  550. {
  551. List<CDAParameter> paras = new List<CDAParameter>();
  552. #region 读取工位配置信息
  553. string sql =
  554. "SELECT --t.*\n" + @"
  555. t.workstationtypeid,
  556. t.workstationname,
  557. t.procedureid,
  558. t.groutinglineid,
  559. t.usercode,
  560. t.pccode,
  561. t.plcip,
  562. t.plcport,
  563. t.plcobject,
  564. t.valueflag,
  565. t.plcobjectread,
  566. t.plc_flag" +
  567. " FROM tp_mst_workstation t\n" +
  568. " WHERE t.workstationtypeid = @workstationtype@\n" +
  569. " AND t.GROUTINGLINEID = @GROUTINGLINEID@\n"
  570. ;
  571. paras.Add(new CDAParameter("workstationtype", workStationType));
  572. paras.Add(new CDAParameter("GROUTINGLINEID", gl_id));
  573. if (!string.IsNullOrEmpty(userCode))
  574. {
  575. sql += " AND t.usercode = @usercode@";
  576. paras.Add(new CDAParameter("usercode", userCode));
  577. }
  578. DataTable dataTable = conn.ExecuteDatatable(sql, paras.ToArray());
  579. if (dataTable == null || dataTable.Rows.Count == 0)
  580. {
  581. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "无当前工位信息" };
  582. }
  583. if (dataTable.Columns.Contains("PLC_FLAG"))
  584. {
  585. string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
  586. if (plc_flag != "1")
  587. {
  588. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  589. }
  590. }
  591. #endregion
  592. string ip = dataTable.Rows[0]["PLCIP"] + "";
  593. if (string.IsNullOrWhiteSpace(ip))
  594. {
  595. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  596. }
  597. int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
  598. JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
  599. //写入PLC
  600. using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  601. {
  602. //自动按配置写
  603. plc.Connect(ip, port);
  604. for (int i = 0; i < plcObj.Count; i++)
  605. {
  606. if (plcObj[i]["type"].ToString().ToLower() == "string")
  607. plc.Write<string>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), plcPara[plcObj[i]["name"].ToString()].ToString());
  608. else if (plcObj[i]["type"].ToString().ToLower() == "int")
  609. plc.Write<short>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(plcPara[plcObj[i]["name"].ToString()]));
  610. else if (plcObj[i]["type"].ToString().ToLower() == "bool")
  611. plc.Write<bool>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToBoolean(plcPara[plcObj[i]["name"].ToString()]));
  612. }
  613. }
  614. #if DEBUG
  615. //写入操作成功日志
  616. string plcParaStr = "";
  617. foreach (string key in plcPara.Keys)
  618. {
  619. plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
  620. }
  621. Curtain.Log.Logger.Debug($"PLC写入成功!{plcParaStr} {ip} {userCode} {plcObj.ToString()}");
  622. #endif
  623. //PLC操作成功
  624. return new ServiceResultEntity()
  625. {
  626. Status = Constant.ServiceResultStatus.Success,
  627. Message = "" //一定不可以有值,为空时才是提交成功
  628. };
  629. }
  630. }
  631. }
  632. }