PLCWorkLogic..cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. 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. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "无当前工位信息" };
  478. }
  479. if (dataTable.Columns.Contains("PLC_FLAG"))
  480. {
  481. string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
  482. if (plc_flag != "1")
  483. {
  484. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  485. }
  486. }
  487. #endregion
  488. string ip = dataTable.Rows[0]["PLCIP"] + "";
  489. if (string.IsNullOrWhiteSpace(ip))
  490. {
  491. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  492. }
  493. int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
  494. JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
  495. //写入PLC
  496. using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  497. {
  498. //自动按配置写
  499. plc.Connect(ip, port);
  500. for (int i = 0; i < plcObj.Count; i++)
  501. {
  502. if (plcObj[i]["type"].ToString().ToLower() == "string")
  503. plc.Write<string>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), plcPara[plcObj[i]["name"].ToString()].ToString());
  504. else if (plcObj[i]["type"].ToString().ToLower() == "int")
  505. plc.Write<short>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(plcPara[plcObj[i]["name"].ToString()]));
  506. else if (plcObj[i]["type"].ToString().ToLower() == "bool")
  507. plc.Write<bool>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToBoolean(plcPara[plcObj[i]["name"].ToString()]));
  508. }
  509. }
  510. #if DEBUG
  511. //写入操作成功日志
  512. string plcParaStr = "";
  513. foreach (string key in plcPara.Keys)
  514. {
  515. plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
  516. }
  517. Curtain.Log.Logger.Debug($"PLC写入成功!{plcParaStr} {ip} {userCode} {plcObj.ToString()}");
  518. #endif
  519. //PLC操作成功
  520. return new ServiceResultEntity()
  521. {
  522. Status = Constant.ServiceResultStatus.Success,
  523. Message = "" //一定不可以有值,为空时才是提交成功
  524. };
  525. }
  526. }
  527. /// <summary>
  528. /// 写PLC 成型线放行
  529. /// </summary>
  530. /// <param name="workStationType"></param>
  531. /// <param name="plcPara"></param>
  532. /// <param name="userCode"></param>
  533. /// <returns></returns>
  534. public static ServiceResultEntity PlcWrite_GL(int workStationType, int gl_id,
  535. Hashtable plcPara, string userCode = null)
  536. {
  537. using (IDataAccess conn = DataAccess.Create())
  538. {
  539. List<CDAParameter> paras = new List<CDAParameter>();
  540. #region 读取工位配置信息
  541. string sql =
  542. "SELECT --t.*\n" + @"
  543. t.workstationtypeid,
  544. t.workstationname,
  545. t.procedureid,
  546. t.groutinglineid,
  547. t.usercode,
  548. t.pccode,
  549. t.plcip,
  550. t.plcport,
  551. t.plcobject,
  552. t.valueflag,
  553. t.plcobjectread,
  554. t.plc_flag" +
  555. " FROM tp_mst_workstation t\n" +
  556. " WHERE t.workstationtypeid = @workstationtype@\n" +
  557. " AND t.GROUTINGLINEID = @GROUTINGLINEID@\n"
  558. ;
  559. paras.Add(new CDAParameter("workstationtype", workStationType));
  560. paras.Add(new CDAParameter("GROUTINGLINEID", gl_id));
  561. if (!string.IsNullOrEmpty(userCode))
  562. {
  563. sql += " AND t.usercode = @usercode@";
  564. paras.Add(new CDAParameter("usercode", userCode));
  565. }
  566. DataTable dataTable = conn.ExecuteDatatable(sql, paras.ToArray());
  567. if (dataTable == null || dataTable.Rows.Count == 0)
  568. {
  569. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "无当前工位信息" };
  570. }
  571. if (dataTable.Columns.Contains("PLC_FLAG"))
  572. {
  573. string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
  574. if (plc_flag != "1")
  575. {
  576. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  577. }
  578. }
  579. #endregion
  580. string ip = dataTable.Rows[0]["PLCIP"] + "";
  581. if (string.IsNullOrWhiteSpace(ip))
  582. {
  583. return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
  584. }
  585. int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
  586. JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
  587. //写入PLC
  588. using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  589. {
  590. //自动按配置写
  591. plc.Connect(ip, port);
  592. for (int i = 0; i < plcObj.Count; i++)
  593. {
  594. if (plcObj[i]["type"].ToString().ToLower() == "string")
  595. plc.Write<string>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), plcPara[plcObj[i]["name"].ToString()].ToString());
  596. else if (plcObj[i]["type"].ToString().ToLower() == "int")
  597. plc.Write<short>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(plcPara[plcObj[i]["name"].ToString()]));
  598. else if (plcObj[i]["type"].ToString().ToLower() == "bool")
  599. plc.Write<bool>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToBoolean(plcPara[plcObj[i]["name"].ToString()]));
  600. }
  601. }
  602. #if DEBUG
  603. //写入操作成功日志
  604. string plcParaStr = "";
  605. foreach (string key in plcPara.Keys)
  606. {
  607. plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
  608. }
  609. Curtain.Log.Logger.Debug($"PLC写入成功!{plcParaStr} {ip} {userCode} {plcObj.ToString()}");
  610. #endif
  611. //PLC操作成功
  612. return new ServiceResultEntity()
  613. {
  614. Status = Constant.ServiceResultStatus.Success,
  615. Message = "" //一定不可以有值,为空时才是提交成功
  616. };
  617. }
  618. }
  619. }
  620. }