F_Main.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration.Install;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.ServiceProcess;
  8. using System.Windows.Forms;
  9. using Curtain.DataAccess;
  10. using Curtain.Extension.ExObjectConvert;
  11. using Curtain.Extension.ExString;
  12. using Curtain.Helpers;
  13. using Curtain.Log;
  14. using Curtain.Net.Sockets.PLC.Model.Melsec;
  15. using Curtain.Net.Sockets.PLC.Model.Siemens;
  16. using Curtain.Systems.Cryptography;
  17. using Curtain.WCF.Proxy;
  18. using Curtain.Windows;
  19. using PLC_S;
  20. using PLC_S.Proxy;
  21. namespace PLC_WF
  22. {
  23. public partial class F_Main : Form
  24. {
  25. private PLC_S_S PLC_S_S = null;
  26. private readonly Logger logger = Logger.CreateLogger("WF");
  27. public F_Main()
  28. {
  29. InitializeComponent();
  30. logger.FileExistDays = 30;
  31. logger.FilePrefix = "";
  32. logger.FileSuffix = "WF";
  33. DataTable dataTable = new DataTable("PLC_S_T");
  34. dataTable.Columns.Add("code");
  35. dataTable.Columns.Add("name");
  36. dataTable.Columns.Add("description");
  37. //xuwei add 2023-07-24
  38. dataTable.Rows.Add("AA", "[AA]自动接口", "自动调用接口");
  39. //xuwei add 2024-01-02
  40. dataTable.Rows.Add("ETPS_EAA", "[EAA]自动轮询接口", "自动轮询调用接口");
  41. //xuwei add 2022-01-10
  42. dataTable.Rows.Add("GA", "[GA]自动", "自动识别模块");
  43. //xuwei add 2022-05-18
  44. dataTable.Rows.Add("EPTS_DA", "[DA]自动采集设备参数", "自动采集设备参数");
  45. // 新陶对接PLC温湿度数据
  46. dataTable.Rows.Add("EPTS_XT", "[XT]新陶", "3#新陶PLC温湿度数据");
  47. dataTable.Rows.Add("EPTS_XT_D", "[XT_D]新陶干燥房状态", "3#新陶4楼干燥房状态");
  48. // 5楼
  49. dataTable.Rows.Add("EPTS_HPGP", "[HPGP]高压注浆", "3#5F@高压成型");
  50. // 5楼
  51. dataTable.Rows.Add("EPTS_HPGP_FLAG", "[HPGP_FLAG]高压注浆线状态", "3#5F@高压成型注浆状态");
  52. dataTable.Rows.Add("GC_QR", "[GC_QR]产品型号【翻转用】", "3#5F@产品型号翻转用");
  53. // 5楼-4楼称重
  54. dataTable.Rows.Add("GW1", "[GW1]5F4F称重(1条码)", "3#5F4F@称重");
  55. // 对接乾润获取第二个条码
  56. dataTable.Rows.Add("GW2", "[GW2]5F4F称重(2条码)", "3#5F4F@称重");
  57. // 3楼
  58. dataTable.Rows.Add("GL_QR2", "[GL_QR2]3#刮登", "3#2F@刮登(乾润)");
  59. //防伪码验证 xuwei add 2021-12-30
  60. dataTable.Rows.Add("GL_QR2_SE", "[GL_QR2_SE]3#刮登(防伪码)", "3#2F@刮登(防伪码)(乾润)");
  61. dataTable.Rows.Add("GL_QR", "[GL_QR]毛坯库(入)", "3#3F@毛坯入库(乾润)");
  62. dataTable.Rows.Add("GP_QR1", "[GP_QR1]精坯库(入)前", "3#3F@精坯入库前(乾润)");
  63. dataTable.Rows.Add("GP_QR2", "[GP_QR2]精坯库(入)", "3#3F@精坯入库(乾润)");
  64. dataTable.Rows.Add("GP_JM3", "[GP_JM3]精坯库(出)", "3#3F@精坯出库(金马)");
  65. dataTable.Rows.Add("GC_DM", "[GC_DM]产品型号+条码【打磨前】", "3#3F@打磨前");
  66. dataTable.Rows.Add("EPTS_GP", "[GP]打磨计件", "3#3F@打磨计件");
  67. dataTable.Rows.Add("GLA_B", "[GLA_B]产品型号+重量+吹风计件【施釉前】", "3#3F@施釉前");
  68. dataTable.Rows.Add("GLA_A", "[GLA_A]产品重量+施釉房+施釉计件【施釉后】", "3#3F@施釉后");
  69. //防伪码绑定 xuwei add 2021-12-08
  70. dataTable.Rows.Add("GLA_AS", "[GLA_AS]产品重量+施釉房+施釉计件【施釉后防伪码】", "3#3F@施釉后防伪码");
  71. // 2楼
  72. dataTable.Rows.Add("GC_KL", "[GC_KL]产品型号+条码【登窑前】", "3#2F@登窑前");
  73. dataTable.Rows.Add("EPTS_KL", "[KL]登窑计件", "3#2F@登窑");
  74. dataTable.Rows.Add("EPTS_KUL", "[KUL]卸窑计件", "3#2F@卸窑");
  75. // 2楼 成检
  76. dataTable.Rows.Add("EPTS_GLK_A", "[GLK_A]轮巡测漏气", "3#2F@成检交接(一检)");
  77. dataTable.Rows.Add("GL_CF", "[GL_CF]产品等级【成检分线】", "3#2F@成检分线");
  78. dataTable.Rows.Add("GC", "[GC]产品型号【放水件、包盖子】", "3#1F@放水件、包盖子");
  79. // 1楼 成检
  80. dataTable.Rows.Add("GC_SS", "[GC_SS]产品型号+条码【试水前-测内漏】", "3#1F@试水前");
  81. dataTable.Rows.Add("EPTS_GLK_W", "[GLK_W]轮巡测内漏", "3#1F@成检(测内漏)");
  82. // 1楼 次品
  83. dataTable.Rows.Add("DA_IN", "[DA_IN]次品区(入)", "");
  84. dataTable.Rows.Add("EPTS_DA_OUT", "[DA_OUT]次品区(出)", "");
  85. // 1楼 包装
  86. //dataTable.Rows.Add("GC", "[GC]产品型号【放水件、包盖子】", "3#1F@包盖子");
  87. dataTable.Rows.Add("BP", "[BP]包装打印", "3#1F@包装打印");
  88. dataTable.Rows.Add("EPTS_PZ", "[PZ]包装码垛(产品型号、商标、高压标识)", "3#1F@包装码垛");
  89. dataTable.Rows.Add("EPTS_TK", "[TK]包装计件", "3#1F@包装计件");
  90. cboPLCST.DisplayMember = "name";
  91. cboPLCST.ValueMember = "code";
  92. this.txtPort.Tag = 1;
  93. cboPLCST.DataSource = dataTable;
  94. this.txtPort.Tag = null;
  95. GetSetting();
  96. this.btnGetDB_Click(null, null);
  97. this.btnGetMES_Click(null, null);
  98. this.btnLogLenR_Click(null, null);
  99. this.Text = $"{(PLC_S_S.DisplayName.IsNullOrWhiteSpace() ? PLC_S_S.ServiceName : PLC_S_S.DisplayName)}";
  100. }
  101. private void GetSetting()
  102. {
  103. PLC_S_S = PLC_S_S.Get();
  104. this.txtPort.Tag = 1;
  105. this.txtPort.Text = PLC_S_S.Port.ToString();
  106. if (!PLC_S_S.PLCServiceType.IsNullOrEmpty())
  107. {
  108. this.cboPLCST.SelectedValue = PLC_S_S.PLCServiceType;
  109. }
  110. this.txtPort.Tag = null;
  111. this.chkAutoStart.Checked = PLC_S_S.AutoStart;
  112. this.txtServiceName.Text = PLC_S_S.ServiceName;
  113. this.txtDisplayName.Text = PLC_S_S.DisplayName;
  114. this.txtDescription.Text = PLC_S_S.Description;
  115. this.txtInterval.Text = PLC_S_S.Interval + "";
  116. this.txtEPTS.Text = PLC_S_S.EPTS_CODE;
  117. }
  118. private void SetSetting()
  119. {
  120. //PLC_S_S PLC_S_S = PLC_S_S.Get();
  121. PLC_S_S.Port = this.txtPort.Text.ToInt32();
  122. PLC_S_S.PLCServiceType = this.cboPLCST.SelectedValue + "";
  123. PLC_S_S.AutoStart = this.chkAutoStart.Checked;
  124. PLC_S_S.ServiceName = this.txtServiceName.Text.Trim();
  125. PLC_S_S.DisplayName = this.txtDisplayName.Text.Trim();
  126. PLC_S_S.Description = this.txtDescription.Text.Trim();
  127. PLC_S_S.Interval = this.txtInterval.Text.Trim().ToInt32();
  128. PLC_S_S.EPTS_CODE = this.txtEPTS.Text.Trim();
  129. PLC_S_S.Set(PLC_S_S);
  130. }
  131. private void btnStart_Click(object sender, EventArgs e)
  132. {
  133. try
  134. {
  135. if (string.IsNullOrWhiteSpace(this.txtPort.Text))
  136. {
  137. MessageBox.Show("PLC服务【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  138. this.txtPort.Focus();
  139. return;
  140. }
  141. //if (string.IsNullOrWhiteSpace(this.txtServiceName.Text))
  142. //{
  143. // MessageBox.Show("PLC服务【服务名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  144. // this.txtServiceName.Focus();
  145. // return;
  146. //}
  147. //if (string.IsNullOrWhiteSpace(this.txtDisplayName.Text))
  148. //{
  149. // MessageBox.Show("PLC服务【显示名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  150. // this.txtDisplayName.Focus();
  151. // return;
  152. //}
  153. SetSetting();
  154. //if (PLC_S_S.PLCServiceType == "BP1")
  155. //{
  156. // string[] s = textBox1.Lines;
  157. // PLC_S.ServerModel.BarcodePrint1 bb = new PLC_S.ServerModel.BarcodePrint1();
  158. // bb.bs = s;
  159. // bb.Start(PLC_S_S.Port);
  160. // PLC_Server.ServerSocket = bb;
  161. //}
  162. //else
  163. //{
  164. // PLC_Server.Start();
  165. //}
  166. PLC_Server.Start();
  167. SetESControl();
  168. txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
  169. if (_fff != null)
  170. {
  171. _fff.Text = $"[{PLC_Server.PLC_S_S.DisplayName}]";
  172. (PLC_Server.ServerSocket as IShowFormLog).FormLogShow = _fff;
  173. _fff.ShowFormLog = (PLC_Server.ServerSocket as IShowFormLog);
  174. }
  175. }
  176. catch (Exception ex)
  177. {
  178. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  179. }
  180. }
  181. private void btnStop_Click(object sender, EventArgs e)
  182. {
  183. try
  184. {
  185. PLC_Server.Stop();
  186. SetNSControl();
  187. txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
  188. }
  189. catch (Exception ex)
  190. {
  191. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  192. }
  193. }
  194. private F_Log _fff = null;
  195. private void btnLog_Click(object sender, EventArgs e)
  196. {
  197. try
  198. {
  199. if (PLC_Server.ServerSocket is IShowFormLog showFormLog)
  200. {
  201. if (showFormLog == null)
  202. {
  203. _fff = new F_Log();
  204. _fff.Show();
  205. return;
  206. }
  207. F_Log f = showFormLog.FormLogShow as F_Log;
  208. _fff = f;
  209. if (f == null)
  210. {
  211. f = new F_Log();
  212. f.Text = $"{f.Text }[{PLC_Server.PLC_S_S.DisplayName}]";
  213. showFormLog.FormLogShow = f;
  214. f.ShowFormLog = showFormLog;
  215. }
  216. f.Show();
  217. }
  218. else
  219. {
  220. _fff = new F_Log();
  221. _fff.Show();
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  227. }
  228. }
  229. private void btnExePath_Click(object sender, EventArgs e)
  230. {
  231. System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory);
  232. }
  233. private void btnSetSetting_Click(object sender, EventArgs e)
  234. {
  235. try
  236. {
  237. SetSetting();
  238. }
  239. catch (Exception ex)
  240. {
  241. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  242. }
  243. }
  244. private void btnGetSetting_Click(object sender, EventArgs e)
  245. {
  246. try
  247. {
  248. GetSetting();
  249. }
  250. catch (Exception ex)
  251. {
  252. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  253. }
  254. }
  255. //private int PLC_S_T = 0;
  256. private void F_Main_Shown(object sender, EventArgs e)
  257. {
  258. try
  259. {
  260. this._windowState = this.WindowState;
  261. RefreshForm();
  262. }
  263. catch (Exception ex)
  264. {
  265. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  266. }
  267. }
  268. private void RefreshForm()
  269. {
  270. if (string.IsNullOrWhiteSpace(PLC_S_S.ServiceName))
  271. {
  272. // 新服务设置
  273. ////PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{PLC_S_S.Port}]";
  274. ////PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{PLC_S_S.Port}]";
  275. //PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{_SS_KEY}][{txtPort.Text}]";
  276. //PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{_SS_KEY}][{txtPort.Text}]";
  277. //this.txtServiceName.Text = PLC_S_S.ServiceName;
  278. //this.txtDisplayName.Text = PLC_S_S.DisplayName;
  279. SetNSControl();
  280. return;
  281. }
  282. ServiceControllerStatus? wsStatus = null;
  283. if (IsServiceExisted(PLC_S_S.ServiceName.ToLower()))
  284. {
  285. this.serviceController1.ServiceName = PLC_S_S.ServiceName;
  286. try
  287. {
  288. wsStatus = this.serviceController1.Status;
  289. }
  290. catch
  291. {
  292. // this.serviceController1.ServiceName = "";
  293. }
  294. }
  295. if (wsStatus == null)
  296. {
  297. // 非windows service
  298. if (this.chkAutoStart.Checked)
  299. {
  300. SetESControl();
  301. PLC_Server.Start();
  302. txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
  303. }
  304. else
  305. {
  306. SetNSControl();
  307. }
  308. }
  309. else
  310. {
  311. // windows service
  312. SetWSControl();
  313. this.txtStatus.Text = "WS:" + this.serviceController1.Status;
  314. }
  315. }
  316. /// <summary>
  317. /// WS 时,控件状态
  318. /// </summary>
  319. private void SetWSControl()
  320. {
  321. //PLC_S_T = 2;
  322. this.txtPort.ReadOnly = true;
  323. this.cboPLCST.Enabled = false;
  324. this.txtServiceName.ReadOnly = true;
  325. this.txtDisplayName.ReadOnly = true;
  326. this.txtDescription.ReadOnly = true;
  327. this.txtEPTS.ReadOnly = true;
  328. this.txtInterval.ReadOnly = true;
  329. this.btnStart.Enabled = false;
  330. this.btnStop.Enabled = false;
  331. this.btnLog.Enabled = false;
  332. this.btnWS_Insert.Enabled = false;
  333. this.btnWS_Delete.Enabled = true;
  334. this.btnWS_Start.Enabled = true;
  335. this.btnWS_Stop.Enabled = true;
  336. this.btnWS_ReStart.Enabled = true;
  337. this.btnWS_Refresh.Enabled = true;
  338. }
  339. /// <summary>
  340. /// es 时,控件状态
  341. /// </summary>
  342. private void SetESControl()
  343. {
  344. //PLC_S_T = 1;
  345. this.txtPort.ReadOnly = true;
  346. this.cboPLCST.Enabled = false;
  347. this.txtServiceName.ReadOnly = true;
  348. this.txtEPTS.ReadOnly = true;
  349. this.txtInterval.ReadOnly = true;
  350. this.txtDisplayName.ReadOnly = false;
  351. this.txtDescription.ReadOnly = false;
  352. this.btnStart.Enabled = false;
  353. this.btnStop.Enabled = true;
  354. this.btnLog.Enabled = true;
  355. this.btnWS_Insert.Enabled = false;
  356. this.btnWS_Delete.Enabled = false;
  357. this.btnWS_Start.Enabled = false;
  358. this.btnWS_Stop.Enabled = false;
  359. this.btnWS_ReStart.Enabled = false;
  360. this.btnWS_Refresh.Enabled = false;
  361. }
  362. /// <summary>
  363. /// 无服务时,控件状态
  364. /// </summary>
  365. private void SetNSControl()
  366. {
  367. //PLC_S_T = 0;
  368. this.txtPort.ReadOnly = false;
  369. this.cboPLCST.Enabled = true;
  370. this.txtServiceName.ReadOnly = false;
  371. this.txtEPTS.ReadOnly = false;
  372. this.txtInterval.ReadOnly = false;
  373. this.txtDisplayName.ReadOnly = false;
  374. this.txtDescription.ReadOnly = false;
  375. this.btnStart.Enabled = true;
  376. this.btnStop.Enabled = false;
  377. //this.btnLog.Enabled = false;
  378. this.btnWS_Insert.Enabled = true;
  379. this.btnWS_Delete.Enabled = false;
  380. this.btnWS_Start.Enabled = false;
  381. this.btnWS_Stop.Enabled = false;
  382. this.btnWS_ReStart.Enabled = false;
  383. this.btnWS_Refresh.Enabled = false;
  384. }
  385. private void F_Main_TextChanged(object sender, EventArgs e)
  386. {
  387. this.notifyIcon1.Text = this.Text;
  388. }
  389. private FormWindowState _windowState = FormWindowState.Normal;
  390. private bool _hasClose = false;
  391. private void F_Main_SizeChanged(object sender, EventArgs e)
  392. {
  393. if (this.WindowState == FormWindowState.Minimized)
  394. {
  395. //Visible = false;
  396. }
  397. else
  398. {
  399. this._windowState = this.WindowState;
  400. }
  401. }
  402. private void F_Main_FormClosing(object sender, FormClosingEventArgs e)
  403. {
  404. if (!_hasClose)
  405. {
  406. DialogResult dr = MessageBox.Show("是否退出?", this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button3);
  407. if (dr == DialogResult.Cancel)
  408. {
  409. e.Cancel = true;
  410. //this.WindowState = FormWindowState.Minimized;
  411. return;
  412. }
  413. if (dr == DialogResult.No)
  414. {
  415. e.Cancel = true;
  416. notifyIcon1.Visible = true;
  417. Visible = false;
  418. return;
  419. }
  420. }
  421. notifyIcon1.Visible = false;
  422. SetSetting();
  423. }
  424. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  425. {
  426. }
  427. private void notifyIcon1_Click(object sender, EventArgs e)
  428. {
  429. //if (!this.Visible)
  430. //{
  431. Visible = true;
  432. //this.TopMost = true;
  433. //this.WindowState = FormWindowState.Normal;
  434. WindowState = this._windowState;
  435. ShowInTaskbar = true;
  436. //}
  437. Activate();
  438. }
  439. private void txtPort_TextChanged(object sender, EventArgs e)
  440. {
  441. //if (this.txtPort.Tag == null)
  442. //{
  443. // PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{_SS_KEY}][{txtPort.Text}]";
  444. // PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{_SS_KEY}][{txtPort.Text}]";
  445. // this.txtServiceName.Text = PLC_S_S.ServiceName;
  446. // this.txtDisplayName.Text = PLC_S_S.DisplayName;
  447. //}
  448. }
  449. private void btnWS_Refresh_Click(object sender, EventArgs e)
  450. {
  451. try
  452. {
  453. RefreshWSStatus();
  454. }
  455. catch (Exception ex)
  456. {
  457. this.txtStatus.Text = null;
  458. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  459. }
  460. }
  461. private void RefreshWSStatus()
  462. {
  463. this.serviceController1.Refresh();
  464. this.txtStatus.Text = "WS:" + this.serviceController1.Status;
  465. Application.DoEvents();
  466. }
  467. private void txtDisplayName_TextChanged(object sender, EventArgs e)
  468. {
  469. this.Text = $"{PLC_S_S.DisplayName}";
  470. }
  471. private void txtPort_KeyPress(object sender, KeyPressEventArgs e)
  472. {
  473. if (char.IsControl(e.KeyChar) ||
  474. char.IsNumber(e.KeyChar)
  475. )
  476. {
  477. return;
  478. }
  479. e.Handled = true;
  480. }
  481. private void txtPort_KeyDown(object sender, KeyEventArgs e)
  482. {
  483. //if (char.IsControl(e.KeyData))
  484. //{
  485. // e.Handled = true;
  486. //}
  487. }
  488. private void btnWS_Insert_Click(object sender, EventArgs e)
  489. {
  490. try
  491. {
  492. if (string.IsNullOrWhiteSpace(this.txtPort.Text))
  493. {
  494. MessageBox.Show("PLC服务【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  495. this.txtPort.Focus();
  496. return;
  497. }
  498. if (string.IsNullOrWhiteSpace(this.txtServiceName.Text))
  499. {
  500. MessageBox.Show("PLC服务【服务名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  501. this.txtServiceName.Focus();
  502. return;
  503. }
  504. if (string.IsNullOrWhiteSpace(this.txtDisplayName.Text))
  505. {
  506. MessageBox.Show("PLC服务【显示名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  507. this.txtDisplayName.Focus();
  508. return;
  509. }
  510. SetSetting();
  511. string path = ApplicationInformation.GetAbsolutePath("PLC_WS.exe");
  512. InstallService(path);
  513. SetWSControl();
  514. this.serviceController1.ServiceName = PLC_S_S.ServiceName;
  515. if (PLC_S_S.AutoStart)
  516. {
  517. this.serviceController1.Start();
  518. RefreshWSStatus();
  519. this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
  520. }
  521. RefreshWSStatus();
  522. }
  523. catch (Exception ex)
  524. {
  525. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  526. }
  527. }
  528. private void btnWS_Delete_Click(object sender, EventArgs e)
  529. {
  530. try
  531. {
  532. if (IsServiceExisted(PLC_S_S.ServiceName.ToLower()))
  533. {
  534. string path = ApplicationInformation.GetAbsolutePath("PLC_WS.exe");
  535. UninstallService(path);
  536. }
  537. SetNSControl();
  538. this.txtStatus.Text = null;
  539. }
  540. catch (Exception ex)
  541. {
  542. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  543. }
  544. }
  545. private void btnWS_Start_Click(object sender, EventArgs e)
  546. {
  547. try
  548. {
  549. this.serviceController1.Start();
  550. RefreshWSStatus();
  551. this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
  552. RefreshWSStatus();
  553. }
  554. catch (Exception ex)
  555. {
  556. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  557. }
  558. }
  559. private void btnWS_Stop_Click(object sender, EventArgs e)
  560. {
  561. try
  562. {
  563. this.serviceController1.Stop();
  564. RefreshWSStatus();
  565. this.serviceController1.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
  566. RefreshWSStatus();
  567. }
  568. catch (Exception ex)
  569. {
  570. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  571. }
  572. }
  573. private void btnWS_ReStart_Click(object sender, EventArgs e)
  574. {
  575. try
  576. {
  577. if (this.serviceController1.Status == ServiceControllerStatus.Running)
  578. {
  579. this.serviceController1.Stop();
  580. RefreshWSStatus();
  581. this.serviceController1.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
  582. RefreshWSStatus();
  583. }
  584. this.serviceController1.Start();
  585. RefreshWSStatus();
  586. this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
  587. RefreshWSStatus();
  588. }
  589. catch (Exception ex)
  590. {
  591. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  592. }
  593. }
  594. /// <summary>
  595. /// 判断服务是否存在
  596. /// </summary>
  597. /// <param name="serviceName"></param>
  598. /// <returns></returns>
  599. private bool IsServiceExisted(string serviceName)
  600. {
  601. ServiceController[] services = ServiceController.GetServices();
  602. foreach (ServiceController sc in services)
  603. {
  604. if (sc.ServiceName.ToLower() == serviceName)
  605. {
  606. return true;
  607. }
  608. }
  609. return false;
  610. }
  611. /// <summary>
  612. /// 安装服务
  613. /// </summary>
  614. /// <param name="serviceFilePath"></param>
  615. private void InstallService(string serviceFilePath)
  616. {
  617. using (AssemblyInstaller installer = new AssemblyInstaller())
  618. {
  619. installer.UseNewContext = true;
  620. installer.Path = serviceFilePath;
  621. //IDictionary savedState = new Hashtable();
  622. //installer.Install(savedState);
  623. //installer.Commit(savedState);
  624. installer.Install(null);
  625. installer.Commit(null);
  626. }
  627. }
  628. /// <summary>
  629. /// 卸载服务
  630. /// </summary>
  631. /// <param name="serviceFilePath"></param>
  632. private void UninstallService(string serviceFilePath)
  633. {
  634. using (AssemblyInstaller installer = new AssemblyInstaller())
  635. {
  636. installer.UseNewContext = true;
  637. installer.Path = serviceFilePath;
  638. installer.Uninstall(null);
  639. }
  640. }
  641. /// <summary>
  642. /// 启动服务
  643. /// </summary>
  644. /// <param name="serviceName"></param>
  645. private void ServiceStart(string serviceName)
  646. {
  647. using (ServiceController control = new ServiceController(serviceName))
  648. {
  649. if (control.Status == ServiceControllerStatus.Stopped)
  650. {
  651. control.Start();
  652. }
  653. }
  654. }
  655. /// <summary>
  656. /// 停止服务
  657. /// </summary>
  658. /// <param name="serviceName"></param>
  659. private void ServiceStop(string serviceName)
  660. {
  661. using (ServiceController control = new ServiceController(serviceName))
  662. {
  663. if (control.Status == ServiceControllerStatus.Running)
  664. {
  665. control.Stop();
  666. }
  667. }
  668. }
  669. private void btnRefreshForm_Click(object sender, EventArgs e)
  670. {
  671. try
  672. {
  673. RefreshForm();
  674. }
  675. catch (Exception ex)
  676. {
  677. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  678. }
  679. }
  680. private void btnGetDB_Click(object sender, EventArgs e)
  681. {
  682. try
  683. {
  684. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
  685. string ip = ini.Read("Oracle", "Server");
  686. string port = ini.Read("Oracle", "Port");
  687. string sid = ini.Read("Oracle", "Database");
  688. string username = ini.Read("Oracle", "UserName");
  689. string password = ini.Read("Oracle", "Password");
  690. if (!string.IsNullOrWhiteSpace(ip))
  691. {
  692. this.txtOracleName.Text = ip;
  693. }
  694. if (!string.IsNullOrWhiteSpace(port))
  695. {
  696. this.txtOraclePort.Text = port;
  697. }
  698. this.txtOracleDatabaseName.Text = sid;
  699. this.txtOracleUserName.Text = username;
  700. if (!string.IsNullOrEmpty(password))
  701. {
  702. try
  703. {
  704. password = SymmetricCryptogram.Decrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
  705. this.txtOraclePassword.Text = password;
  706. }
  707. catch (Exception ex1)
  708. {
  709. this.txtOraclePassword.Text = null;
  710. MessageBox.Show("数据库【密码】解密错误" + System.Environment.NewLine + ex1.Message,
  711. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  712. logger.OutputError(ex1, "数据库【密码】解密错误");
  713. }
  714. }
  715. else
  716. {
  717. this.txtOraclePassword.Text = null;
  718. }
  719. }
  720. catch (Exception ex)
  721. {
  722. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  723. }
  724. }
  725. private void btnSetDB_Click(object sender, EventArgs e)
  726. {
  727. try
  728. {
  729. string ip = this.txtOracleName.Text.Trim();
  730. string port = this.txtOraclePort.Text.Trim();
  731. string sid = this.txtOracleDatabaseName.Text.Trim();
  732. string username = this.txtOracleUserName.Text.Trim();
  733. string password = this.txtOraclePassword.Text;
  734. if (!string.IsNullOrEmpty(password))
  735. {
  736. try
  737. {
  738. password = SymmetricCryptogram.Encrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
  739. }
  740. catch (Exception ex1)
  741. {
  742. MessageBox.Show("数据库【密码】加密错误" + System.Environment.NewLine + ex1.Message,
  743. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  744. logger.OutputError(ex1, "数据库【密码】加密错误");
  745. password = "";
  746. }
  747. }
  748. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
  749. ini.Write("Oracle", "Server", ip);
  750. ini.Write("Oracle", "Port", port);
  751. ini.Write("Oracle", "Database", sid);
  752. ini.Write("Oracle", "UserName", username);
  753. ini.Write("Oracle", "Password", password);
  754. }
  755. catch (Exception ex)
  756. {
  757. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  758. }
  759. }
  760. private void btnOracleTest_Click(object sender, EventArgs e)
  761. {
  762. try
  763. {
  764. string ip = this.txtOracleName.Text.Trim();
  765. string port = this.txtOraclePort.Text.Trim();
  766. string sid = this.txtOracleDatabaseName.Text.Trim();
  767. string username = this.txtOracleUserName.Text.Trim();
  768. string password = this.txtOraclePassword.Text;
  769. if (string.IsNullOrWhiteSpace(ip))
  770. {
  771. MessageBox.Show("数据库【地址】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  772. this.txtOracleName.Focus();
  773. return;
  774. }
  775. if (string.IsNullOrWhiteSpace(port))
  776. {
  777. MessageBox.Show("数据库【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  778. this.txtOraclePort.Focus();
  779. return;
  780. }
  781. if (string.IsNullOrWhiteSpace(sid))
  782. {
  783. MessageBox.Show("数据库【实例】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  784. this.txtOracleDatabaseName.Focus();
  785. return;
  786. }
  787. if (string.IsNullOrWhiteSpace(username))
  788. {
  789. MessageBox.Show("数据库【用户】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  790. this.txtOracleUserName.Focus();
  791. return;
  792. }
  793. if (string.IsNullOrWhiteSpace(password))
  794. {
  795. MessageBox.Show("数据库【密码】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  796. this.txtOraclePassword.Focus();
  797. return;
  798. }
  799. IDataAccess dataAccess = DataAccess.CreateByParameters(DataBaseType.Oracle, ip, port, sid, username, password);
  800. try
  801. {
  802. dataAccess.Open();
  803. dataAccess.ExecuteScalar("select sysdate from dual");
  804. }
  805. catch (Exception ex2)
  806. {
  807. MessageBox.Show("数据库【连接】错误" + System.Environment.NewLine + ex2.Message,
  808. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  809. logger.OutputError(ex2, "数据库【连接】错误");
  810. return;
  811. }
  812. DialogResult dr = MessageBox.Show("数据库【连接】成功" + System.Environment.NewLine + "【是】【否】保存设置?",
  813. this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
  814. if (dr == DialogResult.Yes)
  815. {
  816. try
  817. {
  818. password = SymmetricCryptogram.Encrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
  819. }
  820. catch (Exception ex1)
  821. {
  822. MessageBox.Show("数据库【密码】加密错误" + System.Environment.NewLine + ex1.Message,
  823. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  824. logger.OutputError(ex1, "数据库【密码】加密错误");
  825. return;
  826. }
  827. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
  828. ini.Write("Oracle", "Server", ip);
  829. ini.Write("Oracle", "Port", port);
  830. ini.Write("Oracle", "Database", sid);
  831. ini.Write("Oracle", "UserName", username);
  832. ini.Write("Oracle", "Password", password);
  833. }
  834. }
  835. catch (Exception ex)
  836. {
  837. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  838. }
  839. }
  840. private void txtStatus_TextChanged(object sender, EventArgs e)
  841. {
  842. string t = txtStatus.Text;
  843. if (t.Contains("启动") || t.Contains("Running"))
  844. {
  845. txtStatus.ForeColor = Color.Blue;
  846. }
  847. else if (t.Contains("停止") || t.Contains("Stopped"))
  848. {
  849. txtStatus.ForeColor = Color.Red;
  850. }
  851. else
  852. {
  853. txtStatus.ForeColor = Color.Yellow;
  854. }
  855. }
  856. private void btnGetMES_Click(object sender, EventArgs e)
  857. {
  858. try
  859. {
  860. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
  861. string ip = ini.Read("iBOSS.MES", "IP");
  862. string port = ini.Read("iBOSS.MES", "Port");
  863. string ServiceName = ini.Read("iBOSS.MES", "ServiceName");
  864. string account = ini.Read("iBOSS.MES", "AccountCode");
  865. string username = ini.Read("iBOSS.MES", "UserCode");
  866. string password = ini.Read("iBOSS.MES", "Password");
  867. if (!string.IsNullOrWhiteSpace(ip))
  868. {
  869. this.txtMES_IP.Text = ip;
  870. }
  871. this.txtMES_IP.Tag = ServiceName;
  872. if (!string.IsNullOrWhiteSpace(port))
  873. {
  874. this.txtMES_Port.Text = port;
  875. }
  876. this.txtMES_Account.Text = account;
  877. this.txtMES_User.Text = username;
  878. this.txtMES_Password.Text = password;
  879. }
  880. catch (Exception ex)
  881. {
  882. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  883. }
  884. }
  885. private void btnSetMES_Click(object sender, EventArgs e)
  886. {
  887. try
  888. {
  889. string ip = this.txtMES_IP.Text.Trim();
  890. string port = this.txtMES_Port.Text.Trim();
  891. string account = this.txtMES_Account.Text.Trim();
  892. string username = this.txtMES_User.Text.Trim();
  893. string password = this.txtMES_Password.Text;
  894. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
  895. ini.Write("iBOSS.MES", "IP", ip);
  896. ini.Write("iBOSS.MES", "Port", port);
  897. ini.Write("iBOSS.MES", "AccountCode", account);
  898. ini.Write("iBOSS.MES", "UserCode", username);
  899. ini.Write("iBOSS.MES", "Password", password);
  900. }
  901. catch (Exception ex)
  902. {
  903. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  904. }
  905. }
  906. private void btnMESTest_Click(object sender, EventArgs e)
  907. {
  908. try
  909. {
  910. string ip = this.txtMES_IP.Text.Trim();
  911. string ServiceName = this.txtMES_IP.Tag + "";
  912. string port = this.txtMES_Port.Text.Trim();
  913. string account = this.txtMES_Account.Text.Trim();
  914. string username = this.txtMES_User.Text.Trim();
  915. string password = this.txtMES_Password.Text;
  916. if (string.IsNullOrWhiteSpace(ip))
  917. {
  918. MessageBox.Show("MES【地址】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  919. this.txtMES_IP.Focus();
  920. return;
  921. }
  922. if (string.IsNullOrWhiteSpace(ServiceName))
  923. {
  924. MessageBox.Show("MES【服务】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  925. this.txtMES_IP.Focus();
  926. return;
  927. }
  928. if (string.IsNullOrWhiteSpace(port))
  929. {
  930. MessageBox.Show("MES【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  931. this.txtMES_Port.Focus();
  932. return;
  933. }
  934. if (string.IsNullOrWhiteSpace(account))
  935. {
  936. MessageBox.Show("MES【帐套】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  937. this.txtMES_Account.Focus();
  938. return;
  939. }
  940. if (string.IsNullOrWhiteSpace(username))
  941. {
  942. MessageBox.Show("MES【用户】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  943. this.txtMES_User.Focus();
  944. return;
  945. }
  946. //if (string.IsNullOrWhiteSpace(password))
  947. //{
  948. // MessageBox.Show("MES【密码】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  949. // this.txtMES_Password.Focus();
  950. // return;
  951. //}
  952. try
  953. {
  954. WCFProxySetting ps = new WCFProxySetting(ip, port, ServiceName);
  955. SmartDeviceProxy smartDevice = new SmartDeviceProxy(ps);
  956. string t = smartDevice.Invoke<string>(p => p.Test("text"));
  957. // TODO
  958. }
  959. catch (Exception ex1)
  960. {
  961. MessageBox.Show("MES【连接】失败" + System.Environment.NewLine + ex1.Message,
  962. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  963. logger.OutputError(ex1, "MES【连接】失败");
  964. return;
  965. }
  966. DialogResult dr = MessageBox.Show("MES【连接】成功" + System.Environment.NewLine + "【是】【否】保存设置?",
  967. this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
  968. if (dr == DialogResult.Yes)
  969. {
  970. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
  971. ini.Write("iBOSS.MES", "IP", ip);
  972. ini.Write("iBOSS.MES", "Port", port);
  973. ini.Write("iBOSS.MES", "AccountCode", account);
  974. ini.Write("iBOSS.MES", "UserCode", username);
  975. ini.Write("iBOSS.MES", "Password", password);
  976. }
  977. }
  978. catch (Exception ex)
  979. {
  980. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  981. }
  982. }
  983. private void txtStatus_KeyPress(object sender, KeyPressEventArgs e)
  984. {
  985. e.Handled = true;
  986. }
  987. private bool _isEPTS = false;
  988. private string _SS_KEY = null;
  989. private void cboPLCST_SelectedIndexChanged(object sender, EventArgs e)
  990. {
  991. try
  992. {
  993. string key = cboPLCST.SelectedValue + "";
  994. if (key.StartsWith("EPTS_"))
  995. {
  996. this.lblPort.Text = "设备";
  997. //_SS_KEY = key.Substring(5);
  998. _SS_KEY = key;
  999. }
  1000. else
  1001. {
  1002. this.lblPort.Text = "端口";
  1003. _SS_KEY = key;
  1004. }
  1005. if (this.txtPort.Tag != null)
  1006. {
  1007. return;
  1008. }
  1009. if (key.StartsWith("EPTS_"))
  1010. {
  1011. _isEPTS = true;
  1012. this.txtPort.Text = null;
  1013. }
  1014. else
  1015. {
  1016. if (_isEPTS)
  1017. {
  1018. _isEPTS = false;
  1019. this.txtPort.Text = null;
  1020. }
  1021. this.txtPort_TextChanged(null, null);
  1022. }
  1023. //if (cboPLCST.SelectedItem is DataRowView ddd)
  1024. //{
  1025. // this.txtDescription.Text = ddd["description"] + "";
  1026. //}
  1027. }
  1028. catch (Exception ex)
  1029. {
  1030. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  1031. }
  1032. }
  1033. public static int PLC_LOG_LEN = 5000;
  1034. private void btnLogLenR_Click(object sender, EventArgs e)
  1035. {
  1036. try
  1037. {
  1038. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
  1039. PLC_LOG_LEN = ini.Read("PLC_WF", "LOG_LEN").ToInt32();
  1040. if (PLC_LOG_LEN < 1000)
  1041. {
  1042. PLC_LOG_LEN = 1000;
  1043. }
  1044. txtLogLen.Text = PLC_LOG_LEN+"";
  1045. }
  1046. catch (Exception ex)
  1047. {
  1048. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  1049. }
  1050. }
  1051. private void btnLogLenW_Click(object sender, EventArgs e)
  1052. {
  1053. try
  1054. {
  1055. int len = txtLogLen.Text.ToInt32();
  1056. if (len < 1000)
  1057. {
  1058. len = 1000;
  1059. }
  1060. PLC_LOG_LEN = len;
  1061. txtLogLen.Text = PLC_LOG_LEN + "";
  1062. INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
  1063. ini.Write("PLC_WF", "LOG_LEN", txtLogLen.Text);
  1064. }
  1065. catch (Exception ex)
  1066. {
  1067. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  1068. }
  1069. }
  1070. private void btnPLC_CRW_Click(object sender, EventArgs e)
  1071. {
  1072. try
  1073. {
  1074. //string S_NAME = "SSS_1";
  1075. //Logger logger = Logger.CreateLogger(S_NAME+ "-S");
  1076. //Logger logger_t = Logger.CreateLogger(S_NAME + "-T");
  1077. //Logger logger_e = Logger.CreateLogger(S_NAME + "-E");
  1078. //Logger logger_e_out = Logger.CreateLogger(S_NAME + "-EO");
  1079. //logger.FileNameWithoutDate = true;
  1080. //logger.FileExistDays = 30;
  1081. //logger.LevelOneFile = true;
  1082. //logger.FilePrefix = null;
  1083. //logger.FileSuffix = logger.Name;
  1084. //logger_t.FileExistDays = 30;
  1085. //logger_t.LevelOneFile = true;
  1086. //logger_t.SubFolderFormat = logger_t.Name;
  1087. //logger_e.FileExistDays = 0;
  1088. //logger_e_out.FileExistDays = 0;
  1089. //logger_e_out.FilePrefix = null;
  1090. //logger_e_out.FileSuffix = S_NAME;
  1091. //logger_e_out.Directory = "..\\_PLC_Logs";
  1092. //logger.OutputTrace("S_start");
  1093. //logger_t.OutputTrace("S_start");
  1094. //logger_t.OutputWarn("S_start");
  1095. //logger_e.OutputWarn("S_start");
  1096. //logger_e_out.OutputWarn("S_start");
  1097. //logger_e.OutputError(null,"S_start");
  1098. //logger_e_out.OutputError(null,"S_start");
  1099. switch (cboPLCType.Text)
  1100. {
  1101. case "MelsecA1EAscii":
  1102. F_PLC_M<MelsecA1EAsciiModel> fm = new F_PLC_M<MelsecA1EAsciiModel>();
  1103. fm.Show();
  1104. break;
  1105. case "SiemensS7_1200":
  1106. F_PLC_M<SiemensS7_1200Model> fs = new F_PLC_M<SiemensS7_1200Model>();
  1107. fs.Show();
  1108. break;
  1109. case "SiemensS7_200Smart":
  1110. F_PLC_M<SiemensS7_200SmartModel> fss = new F_PLC_M<SiemensS7_200SmartModel>();
  1111. fss.Show();
  1112. break;
  1113. case "SimpleSocketClientModel":
  1114. F_PLC_M<SimpleSocketClientModel> fs1 = new F_PLC_M<SimpleSocketClientModel>();
  1115. fs1.Show();
  1116. break;
  1117. default:
  1118. break;
  1119. }
  1120. }
  1121. catch (Exception ex)
  1122. {
  1123. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  1124. }
  1125. }
  1126. //取得服务的启动类型的当前状态,返回启动类型的状态値
  1127. //2:自动,3:手动,4:禁用 ......
  1128. //sServiceName服务名称
  1129. //sState输出相关的状态代码,失败则为错误消息
  1130. public int GetWindowsServiceStartType(string sServiceName )
  1131. {
  1132. string sState = "";
  1133. try
  1134. {
  1135. System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo();
  1136. objProcessInf.FileName = "cmd.exe";
  1137. objProcessInf.UseShellExecute = false;
  1138. objProcessInf.RedirectStandardError = true;
  1139. objProcessInf.RedirectStandardInput = true;
  1140. objProcessInf.RedirectStandardOutput = true;
  1141. objProcessInf.CreateNoWindow = true;
  1142. objProcessInf.Arguments = "/c sc qc " + sServiceName;
  1143. System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(objProcessInf);
  1144. String sStateValue = objProcess.StandardOutput.ReadToEnd();
  1145. if (sStateValue.IndexOf("AUTO_START") > 0)
  1146. {
  1147. sState = "AUTO_START";
  1148. return 1;
  1149. }
  1150. if (sStateValue.IndexOf("DEMAND_START") > 0)
  1151. {
  1152. sState = "DEMAND_START";
  1153. return 2;
  1154. }
  1155. if (sStateValue.IndexOf("DISABLED") > 0)
  1156. {
  1157. sState = "DISABLED";
  1158. return 3;
  1159. }
  1160. return 0;
  1161. }
  1162. catch (Exception e)
  1163. {
  1164. sState = e.Message;
  1165. return -1;
  1166. }
  1167. }
  1168. void KillProcess(string MainWindowTitle)
  1169. {
  1170. //productName = productName.ToLower();
  1171. Process[] allProcess = Process.GetProcesses();
  1172. foreach (Process p in allProcess)
  1173. {
  1174. if(p.MainWindowTitle.StartsWith(MainWindowTitle, true, null))
  1175. //if (p.ProcessName.ToLower().StartsWith(processname))
  1176. {
  1177. try
  1178. {
  1179. for (int i = 0; i < p.Threads.Count; i++)
  1180. {
  1181. p.Threads[i].Dispose();
  1182. }
  1183. }
  1184. catch (Exception ex)
  1185. {
  1186. //Logger.Error(ex);
  1187. }
  1188. try
  1189. {
  1190. p.Kill();
  1191. }
  1192. catch (Exception ex)
  1193. {
  1194. //Logger.Error(ex);
  1195. }
  1196. //break;
  1197. }
  1198. }
  1199. }
  1200. private void btnStopAll_Click(object sender, EventArgs e)
  1201. {
  1202. //KillProcess("Finance");
  1203. //return;
  1204. try
  1205. {
  1206. List<string> mms = new List<string>();
  1207. // 获取系统服务列表
  1208. ServiceController[] services = ServiceController.GetServices();
  1209. // 遍历系统服务列表
  1210. foreach (ServiceController service in services)
  1211. {
  1212. // 服务名称
  1213. string name = service.ServiceName;
  1214. try
  1215. {
  1216. if (name.StartsWith("DK.PLC["))
  1217. {
  1218. if (service.Status == ServiceControllerStatus.Running)
  1219. {
  1220. //int ss = GetWindowsServiceStartType(name);
  1221. //if (ss == 1)
  1222. //{
  1223. // service.Start();
  1224. //}
  1225. service.Stop();
  1226. }
  1227. }
  1228. }
  1229. catch (Exception ex)
  1230. {
  1231. mms.Add(name + "[" + ex.Message + "]");
  1232. }
  1233. }
  1234. if (mms.Count > 0)
  1235. {
  1236. MessageBox.Show(string.Join("; ", mms), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  1237. }
  1238. }
  1239. catch (Exception ex)
  1240. {
  1241. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  1242. }
  1243. }
  1244. private void btnStartAll_Click(object sender, EventArgs e)
  1245. {
  1246. try
  1247. {
  1248. List<string> mms = new List<string>();
  1249. // 获取系统服务列表
  1250. ServiceController[] services = ServiceController.GetServices();
  1251. // 遍历系统服务列表
  1252. foreach (ServiceController service in services)
  1253. {
  1254. // 服务名称
  1255. string name = service.ServiceName;
  1256. try
  1257. {
  1258. if (name.StartsWith("DK.PLC["))
  1259. {
  1260. if (service.Status == ServiceControllerStatus.Stopped)
  1261. {
  1262. int ss = GetWindowsServiceStartType(name);
  1263. if (ss == 1)
  1264. {
  1265. service.Start();
  1266. }
  1267. }
  1268. }
  1269. }
  1270. catch (Exception ex)
  1271. {
  1272. mms.Add(name + "[" + ex.Message + "]");
  1273. }
  1274. }
  1275. if (mms.Count > 0)
  1276. {
  1277. MessageBox.Show(string.Join("; ", mms), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  1278. }
  1279. }
  1280. catch (Exception ex)
  1281. {
  1282. MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  1283. }
  1284. }
  1285. }
  1286. }