MainPage.xaml.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. 
  2. using CCDCount.DLL;
  3. using CCDCount.DLL.AlarmTools;
  4. using CCDCount.DLL.SqlDataClass;
  5. using CCDCount.DLL.Tools;
  6. using CCDCount.MODEL.AuditTrailModel;
  7. using CCDCount.MODEL.ConfigModel;
  8. using CCDCountWpf.Language;
  9. using LogClass;
  10. using MvCameraControl;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.Drawing;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Data;
  22. using System.Windows.Documents;
  23. using System.Windows.Input;
  24. using System.Windows.Interop;
  25. using System.Windows.Media;
  26. using System.Windows.Media.Imaging;
  27. using static System.Net.Mime.MediaTypeNames;
  28. namespace CCDCountWpf.WpfPage
  29. {
  30. /// <summary>
  31. /// MainPage.xaml 的交互逻辑
  32. /// </summary>
  33. public partial class MainPage : Page
  34. {
  35. #region 变量与实例
  36. #endregion
  37. public MainPage()
  38. {
  39. InitializeComponent();
  40. DataContext = ShowMessageBus.ShowBinding;
  41. InitFormationItems();
  42. }
  43. private void DataClear_Click(object sender, RoutedEventArgs e)
  44. {
  45. if(MessageBus.NowSettingLoadMainThread == null)
  46. return;
  47. MessageBus.NowSettingLoadMainThread.ClearHistoryActive();
  48. MessageBus.NowSettingLoadMainThread.ClearSendQueue();
  49. PlcSettingMessageBus.pLCManagement.ClearBottSpeed();
  50. }
  51. private void Page_Loaded(object sender, RoutedEventArgs e)
  52. {
  53. this.Width = double.NaN; // 等效于 Auto
  54. this.Height = double.NaN;
  55. LOG.log("生产界面初始化语言切换事件", 6);
  56. LanguageManager.LanguageChangeEvent += ChangeLanguage;
  57. LOG.log("生产界面运行一次语言切换", 6);
  58. ChangeLanguage(null, EventArgs.Empty);
  59. StopIdentifyBtn.IsEnabled = false;
  60. StopIdentifyBtn.Opacity = 0.5;
  61. LOG.log("运行状态初始化", 6);
  62. if(MessageBus.NowSettingLoadMainThread != null)
  63. {
  64. if (MessageBus.NowSettingLoadMainThread.CameraRunStatic)
  65. {
  66. StartIdentifyBtn.IsEnabled = false;
  67. StartIdentifyBtn.Opacity = 0.5;
  68. StopIdentifyBtn.IsEnabled = true;
  69. StopIdentifyBtn.Opacity = 1;
  70. }
  71. else
  72. {
  73. StartIdentifyBtn.IsEnabled = true;
  74. StartIdentifyBtn.Opacity = 1;
  75. StopIdentifyBtn.IsEnabled = false;
  76. StopIdentifyBtn.Opacity = 0.5;
  77. }
  78. }
  79. }
  80. /// <summary>
  81. /// 启动相机识别
  82. /// </summary>
  83. private bool RunCameraIdentify()
  84. {
  85. if(MessageBus.NowSettingLoadMainThread != null)
  86. {
  87. MessageBus.NowSettingLoadMainThread.BatchNumber = ShowMessageBus.ShowBinding.BatchNumber;
  88. if (MessageBus.NowSettingLoadMainThread.CameraConfigIsChange)
  89. {
  90. MessageBus.NowSettingLoadMainThread.ReLoadCameraConfig();
  91. }
  92. //启动单相机实例的全部线程
  93. if (!MessageBus.NowSettingLoadMainThread.StartMianThread())
  94. {
  95. FaultLog.RecordErrorMessage(MessageBus.NowSettingLoadMainThread.cameraConfig.DeviceName + "_" + MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum + "Failed to open", "CCDCountWpf:MainPage-RunCameraIdentify");
  96. //LOG.error(MessageBus.MainThreadS[i].cameraConfig.DeviceName + "_" + MessageBus.MainThreadS[i].cameraConfig.CameraSNNum + "启动失败");
  97. MessageBox.Show(MessageBus.NowSettingLoadMainThread.cameraConfig.DeviceName + "_" + MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum + "Failed to open");
  98. return false;
  99. }
  100. }
  101. MessageBus.NowLoadCammeraSN = MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum;
  102. return true;
  103. }
  104. /// <summary>
  105. /// 停止相机识别
  106. /// </summary>
  107. private void StopCameraIdentify()
  108. {
  109. if(MessageBus.NowSettingLoadMainThread != null)
  110. {
  111. //启动单相机实例的全部线程
  112. if (!MessageBus.NowSettingLoadMainThread.StopMianThread())
  113. {
  114. FaultLog.RecordErrorMessage(MessageBus.NowSettingLoadMainThread.cameraConfig.DeviceName + "_" + MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum + "Failed to close", "CCDCountWpf:MainPage-StopCameraIdentify");
  115. MessageBox.Show(MessageBus.NowSettingLoadMainThread.cameraConfig.DeviceName + "_" + MessageBus.NowSettingLoadMainThread.cameraConfig.CameraSNNum + "Failed to close");
  116. }
  117. }
  118. }
  119. private bool RunBatchIsRun =false;
  120. /// <summary>
  121. /// 运行批次记录
  122. /// </summary>
  123. /// <param name="BatchNumber"></param>
  124. private void RunBatchRecord(string BatchNumber)
  125. {
  126. if (RunBatchIsRun)
  127. {
  128. return;
  129. }
  130. List<BatchRecordModel> batchRecordModels = new List<BatchRecordModel>();
  131. Stopwatch stopwatch = new Stopwatch();
  132. BatchMessSqliteDataClass batchMessSqliteData = new BatchMessSqliteDataClass($"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData\\BatchData_{BatchNumber}.db");
  133. RunBatchIsRun = true;
  134. Task.Run(() =>
  135. {
  136. while (RunBatchIsRun)
  137. {
  138. try
  139. {
  140. stopwatch.Restart();
  141. DateTime RecordTime = DateTime.Now;
  142. var OutTimeBatch = batchRecordModels.Where(x => x.RecordTime.AddMinutes(1) < RecordTime).ToList();
  143. OutTimeBatch.ForEach(x => batchRecordModels.Remove(x));
  144. if(!PlcSettingMessageBus.pLCManagement.ReadBottingVibrationTableHighSpeedValue(out ushort BottingVibrationTableHighSpeedValue, out ushort FilterVibrationTableHighSpeedValue, out ushort CountVibrationTableHighSpeedValue))
  145. {
  146. continue ;
  147. }
  148. if(!PlcSettingMessageBus.pLCManagement.ReadBottingCount(out ushort BottingCount))
  149. {
  150. continue ;
  151. }
  152. ushort CountSPeed = (ushort)(batchRecordModels.Count() > 0 ? (BottingCount - batchRecordModels.Min(o => o.BottingCount)) > 0 ? BottingCount - batchRecordModels.Min(o => o.BottingCount) : 0 : 0);
  153. ShowMessageBus.ShowBinding.BottingSpeed = CountSPeed;
  154. BatchRecordModel batchRecordModel = new BatchRecordModel
  155. {
  156. BatchNunber = BatchNumber,
  157. MaterialCylinderVibrationTableSpeed = BottingVibrationTableHighSpeedValue,
  158. FilterVibrationTableSpeed = FilterVibrationTableHighSpeedValue,
  159. CountVibrationTableSpeed = CountVibrationTableHighSpeedValue,
  160. BottingCount = BottingCount,
  161. BottingSpeed = CountSPeed,
  162. RecordTime = RecordTime
  163. };
  164. batchMessSqliteData.InsertBatchMessage(batchRecordModel);
  165. batchRecordModels.Add(batchRecordModel);
  166. stopwatch.Stop();
  167. int SleepTime = 998 - (int)stopwatch.ElapsedMilliseconds;
  168. if (SleepTime > 0)
  169. {
  170. Thread.Sleep(SleepTime);
  171. }
  172. }
  173. catch(Exception ex)
  174. {
  175. LOG.error("MianPage-RunBatchRecord:Error:" + ex.Message);
  176. }
  177. }
  178. });
  179. }
  180. private void StopBatchRecord()
  181. {
  182. RunBatchIsRun = false;
  183. }
  184. private void StartIdentifyBtn_Click(object sender, RoutedEventArgs e)
  185. {
  186. if (String.IsNullOrWhiteSpace(ShowMessageBus.ShowBinding.BatchNumber))
  187. {
  188. MessageBox.Show("请输入批次号!");
  189. return;
  190. }
  191. RunBatchRecord(ShowMessageBus.ShowBinding.BatchNumber);
  192. if (!RunCameraIdentify())
  193. {
  194. return;
  195. }
  196. StartIdentifyBtn.IsEnabled = false;
  197. StartIdentifyBtn.Opacity = 0.5;
  198. StopIdentifyBtn.IsEnabled = true;
  199. StopIdentifyBtn.Opacity = 1;
  200. if (PlcSettingMessageBus.pLCManagement.IsConnect)
  201. {
  202. PlcSettingMessageBus.pLCManagement.MachineRunToTrue();
  203. PlcSettingMessageBus.pLCManagement.WriteCameraRunState(1);
  204. }
  205. }
  206. private void StopIdentifyBtn_Click(object sender, RoutedEventArgs e)
  207. {
  208. StopCameraIdentify();
  209. LOG.log("停止相机成功",6);
  210. StopBatchRecord();
  211. StartIdentifyBtn.IsEnabled = true;
  212. StartIdentifyBtn.Opacity = 1;
  213. StopIdentifyBtn.IsEnabled = false;
  214. StopIdentifyBtn.Opacity = 0.5;
  215. if (PlcSettingMessageBus.pLCManagement.IsConnect)
  216. {
  217. PlcSettingMessageBus.pLCManagement.MachineStopToTrue();
  218. PlcSettingMessageBus.pLCManagement.WriteCameraRunState(0);
  219. }
  220. }
  221. private void BottingMaterialCylinderVibrationTableHighSpeedValueUpUpBtn_Click(object sender, RoutedEventArgs e)
  222. {
  223. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  224. {
  225. MessageBox.Show("PLC Connecting,Please Wait");
  226. return;
  227. }
  228. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  229. {
  230. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed + 10) >
  231. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed)
  232. {
  233. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed += 10;
  234. }
  235. }
  236. }
  237. private void BottingMaterialCylinderVibrationTableHighSpeedValueUpBtn_Click(object sender, RoutedEventArgs e)
  238. {
  239. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  240. {
  241. MessageBox.Show("PLC Connecting,Please Wait");
  242. return;
  243. }
  244. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  245. {
  246. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed + 1) >
  247. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed)
  248. {
  249. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed += 1;
  250. }
  251. }
  252. }
  253. private void BottingMaterialCylinderVibrationTableHighSpeedValueDownBtn_Click(object sender, RoutedEventArgs e)
  254. {
  255. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  256. {
  257. MessageBox.Show("PLC Connecting,Please Wait");
  258. return;
  259. }
  260. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  261. {
  262. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed - 1) <
  263. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed)
  264. {
  265. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed -= 1;
  266. }
  267. }
  268. }
  269. private void BottingMaterialCylinderVibrationTableHighSpeedValueDownDownBtn_Click(object sender, RoutedEventArgs e)
  270. {
  271. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  272. {
  273. MessageBox.Show("PLC Connecting,Please Wait");
  274. return;
  275. }
  276. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  277. {
  278. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed - 10) <
  279. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed)
  280. {
  281. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed -= 10;
  282. }
  283. }
  284. }
  285. private void BottingFilterVibrationTableHighSpeedValueUpUpBtn_Click(object sender, RoutedEventArgs e)
  286. {
  287. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  288. {
  289. MessageBox.Show("PLC Connecting,Please Wait");
  290. return;
  291. }
  292. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  293. {
  294. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed + 10) >
  295. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed)
  296. {
  297. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed += 10;
  298. }
  299. }
  300. }
  301. private void BottingFilterVibrationTableHighSpeedValueUpBtn_Click(object sender, RoutedEventArgs e)
  302. {
  303. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  304. {
  305. MessageBox.Show("PLC Connecting,Please Wait");
  306. return;
  307. }
  308. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  309. {
  310. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed + 1) >
  311. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed)
  312. {
  313. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed += 1;
  314. }
  315. }
  316. }
  317. private void BottingFilterVibrationTableHighSpeedValueDownBtn_Click(object sender, RoutedEventArgs e)
  318. {
  319. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  320. {
  321. MessageBox.Show("PLC Connecting,Please Wait");
  322. return;
  323. }
  324. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  325. {
  326. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed - 1) <
  327. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed)
  328. {
  329. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed -= 1;
  330. }
  331. }
  332. }
  333. private void BottingFilterVibrationTableHighSpeedValueDownDownBtn_Click(object sender, RoutedEventArgs e)
  334. {
  335. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  336. {
  337. MessageBox.Show("PLC Connecting,Please Wait");
  338. return;
  339. }
  340. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  341. {
  342. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed - 10) <
  343. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed)
  344. {
  345. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed -= 10;
  346. }
  347. }
  348. }
  349. private void BottingCountVibrationTableHighSpeedValueUpUpBtn_Click(object sender, RoutedEventArgs e)
  350. {
  351. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  352. {
  353. MessageBox.Show("PLC Connecting,Please Wait");
  354. return;
  355. }
  356. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  357. {
  358. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed + 10) >
  359. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed)
  360. {
  361. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed += 10;
  362. }
  363. }
  364. }
  365. private void BottingCountVibrationTableHighSpeedValueUpBtn_Click(object sender, RoutedEventArgs e)
  366. {
  367. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  368. {
  369. MessageBox.Show("PLC Connecting,Please Wait");
  370. return;
  371. }
  372. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  373. {
  374. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed + 1) >
  375. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed)
  376. {
  377. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed += 1;
  378. }
  379. }
  380. }
  381. private void BottingCountVibrationTableHighSpeedValueDownBtn_Click(object sender, RoutedEventArgs e)
  382. {
  383. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  384. {
  385. MessageBox.Show("PLC Connecting,Please Wait");
  386. return;
  387. }
  388. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  389. {
  390. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed - 1) <
  391. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed)
  392. {
  393. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed -= 1;
  394. }
  395. }
  396. }
  397. private void BottingCountVibrationTableHighSpeedValueDownDownBtn_Click(object sender, RoutedEventArgs e)
  398. {
  399. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  400. {
  401. MessageBox.Show("PLC Connecting,Please Wait");
  402. return;
  403. }
  404. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  405. {
  406. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed - 10) <
  407. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed)
  408. {
  409. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed -= 10;
  410. }
  411. }
  412. }
  413. private void BottValueSetUpUpBtn_Click(object sender, RoutedEventArgs e)
  414. {
  415. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  416. {
  417. MessageBox.Show("PLC Connecting,Please Wait");
  418. return;
  419. }
  420. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  421. {
  422. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet + 5) >
  423. PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet)
  424. {
  425. PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet += 5;
  426. }
  427. }
  428. }
  429. private void BottValueSetUpBtn_Click(object sender, RoutedEventArgs e)
  430. {
  431. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  432. {
  433. MessageBox.Show("PLC Connecting,Please Wait");
  434. return;
  435. }
  436. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  437. {
  438. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet + 1) >
  439. PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet)
  440. {
  441. PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet += 1;
  442. }
  443. }
  444. }
  445. private void BottValueSetDownBtn_Click(object sender, RoutedEventArgs e)
  446. {
  447. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  448. {
  449. MessageBox.Show("PLC Connecting,Please Wait");
  450. return;
  451. }
  452. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  453. {
  454. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet - 1) <
  455. PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet)
  456. {
  457. PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet -= 1;
  458. }
  459. }
  460. }
  461. private void BottValueSetDownDownBtn_Click(object sender, RoutedEventArgs e)
  462. {
  463. if (SystemAlarm.CheckAlarmStatic(AlarmMessageList.PLC连接中))
  464. {
  465. MessageBox.Show("PLC Connecting,Please Wait");
  466. return;
  467. }
  468. if (PlcSettingMessageBus.pLCManagement != null && PlcSettingMessageBus.pLCManagement.IsConnect)
  469. {
  470. if ((ushort)(PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet - 5) <
  471. PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet)
  472. {
  473. PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet -= 5;
  474. }
  475. }
  476. }
  477. private void TextBox_KeyDown(object sender,KeyEventArgs e)
  478. {
  479. if (e.Key == Key.Enter)
  480. {
  481. TextBox textBox = sender as TextBox;
  482. if (textBox != null)
  483. {
  484. // 强制更新源数据
  485. BindingExpression bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
  486. bindingExpression.UpdateSource();
  487. }
  488. }
  489. }
  490. /// <summary>
  491. /// 初始化配方列表
  492. /// </summary>
  493. private void InitFormationItems()
  494. {
  495. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}Formulations";
  496. if (!Directory.Exists(folderPath))
  497. {
  498. // 创建文件夹
  499. Directory.CreateDirectory(folderPath);
  500. }
  501. try
  502. {
  503. // 获取文件列表(包含子目录)
  504. string[] files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);
  505. ShowMessageBus.ShowBinding.FormulationItems.Clear();
  506. foreach (string file in files)
  507. {
  508. ShowMessageBus.ShowBinding.FormulationItems.Add(Path.GetFileNameWithoutExtension(file));
  509. }
  510. if (ShowMessageBus.ShowBinding.FormulationName != null && ShowMessageBus.ShowBinding.FormulationName != string.Empty)
  511. {
  512. try
  513. {
  514. FormulationCoBox.SelectedItem = ShowMessageBus.ShowBinding.FormulationName;
  515. }
  516. catch { }
  517. }
  518. }
  519. catch
  520. { }
  521. }
  522. private void SaveFormulationBtn_Click(object sender, RoutedEventArgs e)
  523. {
  524. if (string.IsNullOrEmpty(ShowMessageBus.ShowBinding.FormulationName))
  525. {
  526. MessageBox.Show("请输入配方名称!");
  527. return;
  528. }
  529. var ParaValue = PlcSettingMessageBus.pLCManagement.ReadAllPara();
  530. FormulationConfigClass formulationConfigClass = FormulationClass.InitFormulation(
  531. MessageBus.NowSettingLoadMainThread.cameraConfig, MessageBus.NowSettingLoadMainThread.shuLiConfig,ParaValue);
  532. if (formulationConfigClass == null)
  533. {
  534. MessageBox.Show("获取参数失败,请检查与PLC的连接");
  535. return;
  536. }
  537. formulationConfigClass.FormulationName = ShowMessageBus.ShowBinding.FormulationName;
  538. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}Formulations";
  539. string FilePath = folderPath + "\\" + ShowMessageBus.ShowBinding.FormulationName + ".xml";
  540. if (File.Exists(FilePath))
  541. {
  542. var confirmResult = MessageBox.Show("配方已存在,是否覆盖?",
  543. "保存确认", MessageBoxButton.YesNo);
  544. if (confirmResult == MessageBoxResult.No)
  545. {
  546. return;
  547. }
  548. }
  549. if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
  550. {
  551. XmlStorage.SerializeToXml(formulationConfigClass, FilePath);
  552. InitFormationItems();
  553. MessageBus.NowSettingLoadMainThread.FormulationName = formulationConfigClass.FormulationName;
  554. MessageBus.NowSettingLoadMainThread.IsLoadFormulation = true;
  555. MessageBox.Show("保存成功");
  556. }
  557. }
  558. private void ReadFormulationBtn_Click(object sender, RoutedEventArgs e)
  559. {
  560. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}Formulations\\{FormulationCoBox.SelectedItem}.xml";
  561. if (!File.Exists(folderPath))
  562. {
  563. MessageBox.Show("指定文件不存在,请选择一个正确的配方");
  564. return;
  565. }
  566. try
  567. {
  568. FormulationConfigClass formulationConfigClass = null;
  569. formulationConfigClass = XmlStorage.DeserializeFromXml<FormulationConfigClass>(folderPath);
  570. LoadFormulation(formulationConfigClass);
  571. MessageBus.NowSettingLoadMainThread.FormulationName = formulationConfigClass.FormulationName;
  572. MessageBus.NowSettingLoadMainThread.IsLoadFormulation = true;
  573. MessageBox.Show("加载成功");
  574. }
  575. catch (Exception ex)
  576. {
  577. MessageBox.Show($"读取文件时出错:{ex.Message}");
  578. }
  579. }
  580. /// <summary>
  581. /// 加载配方
  582. /// </summary>
  583. /// <param name="FormulationConfig"></param>
  584. private void LoadFormulation(FormulationConfigClass FormulationConfig)
  585. {
  586. ShowMessageBus.ShowBinding.AcquistionLineRate = FormulationConfig.AcquistionLineRateValue.ToString();
  587. ShowMessageBus.ShowBinding.ExposureTimeValue = FormulationConfig.ExposureTimeValue.ToString();
  588. ShowMessageBus.ShowBinding.Channel = FormulationConfig.Channel.ToString();
  589. ShowMessageBus.ShowBinding.FormulationName = FormulationConfig.FormulationName;
  590. ShowMessageBus.ShowBinding.MAX_OBJECT_LENGTH = FormulationConfig.MAX_Object_LENGTH.ToString();
  591. ShowMessageBus.ShowBinding.MIN_OBJECT_LENGTH = FormulationConfig.MIN_Object_LENGTH.ToString();
  592. ShowMessageBus.ShowBinding.PandingCode = FormulationConfig.PandingCode.ToString();
  593. ShowMessageBus.ShowBinding.RegionThreshold = FormulationConfig.RegionThreshold.ToString();
  594. PlcSettingMessageBus.PlcMessageShowBindage.FillingModel =
  595. FormulationConfig.FillingMode;
  596. PlcSettingMessageBus.PlcMessageShowBindage.BeltsSpeed =
  597. FormulationConfig.BeltsSpeed;
  598. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_FillingSpeed =
  599. FormulationConfig.ShakeTable1H_FillingSpeed;
  600. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_FillingSpeed =
  601. FormulationConfig.ShakeTable2H_FillingSpeed;
  602. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_FillingSpeed =
  603. FormulationConfig.ShakeTable3H_FillingSpeed;
  604. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1M_FillingSpeed =
  605. FormulationConfig.ShakeTable1M_FillingSpeed;
  606. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2M_FillingSpeed =
  607. FormulationConfig.ShakeTable2M_FillingSpeed;
  608. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3M_FillingSpeed =
  609. FormulationConfig.ShakeTable3M_FillingSpeed;
  610. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1L_FillingSpeed =
  611. FormulationConfig.ShakeTable1L_FillingSpeed;
  612. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2L_FillingSpeed =
  613. FormulationConfig.ShakeTable2L_FillingSpeed;
  614. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3L_FillingSpeed =
  615. FormulationConfig.ShakeTable3L_FillingSpeed;
  616. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1H_CacheSpeed =
  617. FormulationConfig.ShakeTable1H_CacheSpeed;
  618. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2H_CacheSpeed =
  619. FormulationConfig.ShakeTable2H_CacheSpeed;
  620. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3H_CacheSpeed =
  621. FormulationConfig.ShakeTable3H_CacheSpeed;
  622. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1M_CacheSpeed =
  623. FormulationConfig.ShakeTable1M_CacheSpeed;
  624. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2M_CacheSpeed =
  625. FormulationConfig.ShakeTable1M_CacheSpeed;
  626. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3M_CacheSpeed =
  627. FormulationConfig.ShakeTable3M_CacheSpeed;
  628. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable1L_CacheSpeed =
  629. FormulationConfig.ShakeTable1L_CacheSpeed;
  630. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable2L_CacheSpeed =
  631. FormulationConfig.ShakeTable1L_CacheSpeed;
  632. PlcSettingMessageBus.PlcMessageShowBindage.ShakeTable3L_CacheSpeed =
  633. FormulationConfig.ShakeTable3L_CacheSpeed;
  634. PlcSettingMessageBus.PlcMessageShowBindage.FillingValveCloseSpeed =
  635. FormulationConfig.FillingValveCloseSpeed.ToString();
  636. PlcSettingMessageBus.PlcMessageShowBindage.FillingValveClosePosition =
  637. FormulationConfig.FillingValveClosePosition.ToString();
  638. PlcSettingMessageBus.PlcMessageShowBindage.ValveDelytime =
  639. FormulationConfig.ValveDelytime;
  640. PlcSettingMessageBus.PlcMessageShowBindage.ScrewJogSpeed =
  641. FormulationConfig.ScrewJogSpeed.ToString();
  642. PlcSettingMessageBus.PlcMessageShowBindage.BottValueSet =
  643. FormulationConfig.BottValueSet;
  644. PlcSettingMessageBus.PlcMessageShowBindage.FillingValveOpenSpeed =
  645. FormulationConfig.FillingValveOpenSpeed.ToString();
  646. PlcSettingMessageBus.PlcMessageShowBindage.FillingValveOpenPosition =
  647. FormulationConfig.FillingValveOpenPosition.ToString();
  648. PlcSettingMessageBus.PlcMessageShowBindage.CacheShutdownValue =
  649. FormulationConfig.CacheShutdownValue;
  650. }
  651. private void ChangeLanguage(object o, EventArgs e)
  652. {
  653. DataGridTextColumn MessageTypeColumn = FindName("DataGridMessageTypeColumn") as DataGridTextColumn;
  654. var MessageTypeHeard = MessageTypeColumn?.Header as TextBlock;
  655. //if (MessageTypeHeard.Text.ToString().Contains("类型"))
  656. //{
  657. // //修改绑定路径到本地化属性
  658. // MessageTypeColumn.Binding = new Binding("AlarmChType");
  659. //}
  660. //else if (MessageTypeHeard.Text.ToString().Contains("MessageType"))
  661. //{
  662. // MessageTypeColumn.Binding = new Binding("AlarmEnType");
  663. //}
  664. DataGridTextColumn MessageColumn = FindName("DataGridMessageColumn") as DataGridTextColumn;
  665. var MessageHeard = MessageColumn?.Header as TextBlock;
  666. if (MessageHeard.Text.ToString().Contains("信息"))
  667. {
  668. // 修改绑定路径到本地化属性
  669. MessageColumn.Binding = new Binding("AlarmChMess");
  670. }
  671. else if (MessageHeard.Text.ToString().Contains("Message"))
  672. {
  673. MessageColumn.Binding = new Binding("AlarmEnMess");
  674. }
  675. }
  676. private void BatchNumberTbx_GotFocus(object sender, RoutedEventArgs e)
  677. {
  678. OnScreenKeyboard.KeyBoardShow();
  679. }
  680. private void FormulationName_GotFocus(object sender, RoutedEventArgs e)
  681. {
  682. OnScreenKeyboard.KeyBoardShow();
  683. }
  684. }
  685. }