MXComponent_FEM02.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. using ProjectBase.Data.BaseDAL;
  2. using ProjectBase.Data.Logs;
  3. using ProjectBase.Data.Redis;
  4. using ProjectBase.Util;
  5. using SIMDP.BLL;
  6. using SIMDP.Device;
  7. using SIMDP.Model;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. namespace SIMDP.Project
  13. {
  14. public class MXComponent_FEM02 : PlcMXComponent
  15. {
  16. #region 设备空间 寄存器定义&继承成员
  17. private static string ip1 = "";
  18. private static int port1 = 0;
  19. private static string ip2 = "";
  20. private static int port2 = 0;
  21. private void InitScannerParams()
  22. {
  23. bool res1 = BLLFactory<BlPlcInfo>.Instance.GetScannerParams("Cammer1", out ip1, out port1);
  24. bool res2 = BLLFactory<BlPlcInfo>.Instance.GetScannerParams("Cammer2", out ip2, out port2);
  25. if (!res1 || !res2)
  26. {
  27. DevExpress.XtraEditors.XtraMessageBox.Show("当前扫码相机未配置,无法通信", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  28. }
  29. }
  30. public static bool SkipHeadScan = false;
  31. public static bool SkipFEMScan = false;
  32. public MXComponent_FEM02()
  33. {
  34. InitScannerParams();//初始化相机参数;
  35. FormCurrentTask.manualForceMatchSuccess += new FormCurrentTask.delegateResetVertify2(ManualForceMatchSuccess);
  36. FormCurrentTask.manualForceWrite2Tighting += new FormCurrentTask.delegateResetVertify3(ManualForceWrite2Tighting);
  37. }
  38. public override void actionConnected()
  39. {
  40. //复位PLC写入PLC的信号
  41. Write("SRS数据传输异常信号", false);
  42. Write("读取FEM二维码成功", false);
  43. Write("读取FEM二维码失败", false);
  44. Write("读取车头纸二维码成功", false);
  45. Write("读取车头纸二维码失败", false);
  46. Write("拧紧工位数据写入成功", false);
  47. Write("VIN匹配失败信号", false);
  48. Write("读取拧紧结果完成", false);
  49. }
  50. private bool pcHeartBeat = false;
  51. public override void actionTimer()
  52. {
  53. try
  54. {
  55. pcHeartBeat = !pcHeartBeat;
  56. if (!Write("PC心跳", pcHeartBeat))
  57. {
  58. isConnect = false;
  59. return;
  60. }
  61. //SRS报警
  62. if (SysEnvironment.warn_AS400)
  63. {
  64. Write("SRS数据传输异常信号", true);
  65. SysEnvironment.warn_AS400 = false;
  66. }
  67. foreach (MXComponentDataPointInfo p in monitorPoints)
  68. {
  69. if (!Read(p, out object value))
  70. {
  71. isConnect = false;
  72. return;
  73. }
  74. if (!value.Equals(p.value))
  75. {
  76. p.value = value;
  77. if (p.proc != null)
  78. p.proc.Invoke(this, new object[] { value });
  79. }
  80. }
  81. redis.Publish(SysEnvironment.OpcDataChannel, "");
  82. }
  83. catch (Exception ex)
  84. {
  85. isConnect = false;
  86. LogHelper.log.Error($"actionTimer()异常,即将重连PLC: {ex.Message}");
  87. }
  88. }
  89. internal class ProductionData
  90. {
  91. internal List<string> keys = new List<string>();
  92. internal Dictionary<string, string> dict = new Dictionary<string, string>();
  93. internal MoProductData model = new MoProductData();
  94. internal ProductionData()
  95. {
  96. model.RuleId = BLLFactory<BLL.BlRule>.Instance.GetRuleIdByName("FEM");
  97. keys.Add("型号");
  98. keys.Add("颜色");
  99. keys.Add("VIN");
  100. keys.Add("FEM");
  101. keys.Add("VIN2");
  102. keys.Add("力矩1");
  103. keys.Add("角度1");
  104. keys.Add("状态1");
  105. keys.Add("力矩2");
  106. keys.Add("角度2");
  107. keys.Add("状态2");
  108. keys.Add("力矩3");
  109. keys.Add("角度3");
  110. keys.Add("状态3");
  111. keys.Add("力矩4");
  112. keys.Add("角度4");
  113. keys.Add("状态4");
  114. keys.Add("生产完成");
  115. foreach (var key in keys)
  116. {
  117. dict.Add(key, "");
  118. }
  119. }
  120. internal string Dict2String()
  121. {
  122. StringBuilder builder = new StringBuilder();
  123. foreach (var d in dict)
  124. {
  125. if (builder.Length > 0)
  126. builder.Append(",");
  127. builder.Append(d.Value);
  128. }
  129. return builder.ToString();
  130. }
  131. internal MoProductData toModel()
  132. {
  133. model.DataValue = this.Dict2String();
  134. return model;
  135. }
  136. internal void LoadData(MoProductData data)
  137. {
  138. model.DataId = data.DataId;
  139. model.DataValue = data.DataValue;
  140. model.RuleId = data.RuleId;
  141. model.RuleTime = data.RuleTime;
  142. model.Batchid = data.Batchid;
  143. string[] values = model.DataValue.Split(',');
  144. if (values.Length > keys.Count)
  145. return;
  146. for (int i = 0; i < keys.Count; i++)
  147. {
  148. dict[keys[i]] = values[i];
  149. }
  150. }
  151. }
  152. public class ScanResClass
  153. {
  154. public string ScanResult { get; set; }
  155. public DateTime ScanTime { get; set; }
  156. }
  157. public static class SuperQueueScanner
  158. {
  159. internal static RedisHelper redis = new RedisHelper();
  160. internal static List<ScanResClass> HeadCodeList = new List<ScanResClass>();
  161. internal static int HeadCodeListMaxMun = 2;
  162. internal static string ScanHeadCode()
  163. {
  164. string scanRes = Scanner.CameraScanner(ip2, port2, 3).Trim();
  165. LogHelper.log.Info($"触发车头纸扫码:{scanRes}");
  166. HeadCodeList.Add(new ScanResClass { ScanResult = scanRes, ScanTime = DateTime.Now });
  167. while (HeadCodeList.Count > HeadCodeListMaxMun)
  168. {
  169. HeadCodeList.RemoveAt(0);
  170. }
  171. return scanRes;
  172. }
  173. internal static List<ScanResClass> FEMCodeList = new List<ScanResClass>();
  174. internal static int FEMCodeListMaxMun = 3;
  175. internal static string ScanFEMCode()
  176. {
  177. string scanRes = Scanner.CameraScanner(ip1, port1, 3).Trim();
  178. LogHelper.log.Info($"触发FEM扫码:{scanRes}");
  179. FEMCodeList.Add(new ScanResClass { ScanResult = scanRes, ScanTime = DateTime.Now });
  180. while (FEMCodeList.Count > FEMCodeListMaxMun)
  181. {
  182. FEMCodeList.RemoveAt(0);
  183. }
  184. return scanRes;
  185. }
  186. internal static int Scanning_Match_Result = -3;
  187. internal static void GenMatchResult()
  188. {
  189. LogHelper.log.Info($"匹配逻辑开始,车头扫码SkipHeadScan = {SkipHeadScan},FEM扫码SkipFEMScan = {SkipFEMScan}");
  190. try
  191. {
  192. if (SkipHeadScan && SkipFEMScan)
  193. {
  194. SuperQueueScanner.Scanning_Match_Result = -3;
  195. return;
  196. }
  197. else if (SkipHeadScan && !SkipFEMScan)
  198. {
  199. if (SysEnvironment.Global_FEM_Pointer == "")
  200. {
  201. SuperQueueScanner.Scanning_Match_Result = -3;
  202. return;
  203. }
  204. string str2 = SysEnvironment.Global_FEM_Pointer; //FEMCodeList[0].ScanResult;
  205. if (!SysEnvironment.CheckVINRule(str2))
  206. {
  207. SuperQueueScanner.Scanning_Match_Result = 0;
  208. return;
  209. }
  210. if (!string.Equals(str2, SysEnvironment.Global_Scanning_Pointer))
  211. {
  212. SuperQueueScanner.Scanning_Match_Result = 0;
  213. return;
  214. }
  215. }
  216. else if (!SkipHeadScan && SkipFEMScan)
  217. {
  218. if (SysEnvironment.Global_Head_Pointer == "")
  219. {
  220. SuperQueueScanner.Scanning_Match_Result = -3;
  221. return;
  222. }
  223. string str1 = SysEnvironment.Global_Head_Pointer; //HeadCodeList[0].ScanResult;
  224. if (!SysEnvironment.CheckVINRule(str1))
  225. {
  226. SuperQueueScanner.Scanning_Match_Result = 0;
  227. return;
  228. }
  229. if (!string.Equals(str1, SysEnvironment.Global_Scanning_Pointer))
  230. {
  231. SuperQueueScanner.Scanning_Match_Result = 0;
  232. return;
  233. }
  234. }
  235. else
  236. {
  237. if (SysEnvironment.Global_Head_Pointer == "" && SysEnvironment.Global_FEM_Pointer == "")
  238. {
  239. SuperQueueScanner.Scanning_Match_Result = -3;
  240. return;
  241. }
  242. else if (SysEnvironment.Global_FEM_Pointer == "")
  243. {
  244. SuperQueueScanner.Scanning_Match_Result = -2;//匹配中
  245. return;
  246. }
  247. else if (SysEnvironment.Global_Head_Pointer == "")
  248. {
  249. SuperQueueScanner.Scanning_Match_Result = -2;//匹配中
  250. return;
  251. }
  252. string str1 = SysEnvironment.Global_Head_Pointer;//HeadCodeList[0].ScanResult;
  253. string str2 = SysEnvironment.Global_FEM_Pointer;// FEMCodeList[0].ScanResult;
  254. if (!SysEnvironment.CheckVINRule(str1) || !SysEnvironment.CheckVINRule(str2))
  255. {
  256. SuperQueueScanner.Scanning_Match_Result = 0;
  257. return;
  258. }
  259. if (!string.Equals(str1, str2))
  260. {
  261. SuperQueueScanner.Scanning_Match_Result = 0;
  262. return;
  263. }
  264. if (!string.Equals(str1, SysEnvironment.Global_Scanning_Pointer))
  265. {
  266. SuperQueueScanner.Scanning_Match_Result = 0;
  267. return;
  268. }
  269. }
  270. //匹配成功,写入数据库扫码结果
  271. SuperQueueScanner.Scanning_Match_Result = 1;
  272. StoreScanningResult();
  273. }
  274. finally
  275. {
  276. LogHelper.log.Info($"匹配结果为{SuperQueueScanner.Scanning_Match_Result}");
  277. }
  278. }
  279. }
  280. private static void StoreScanningResult()
  281. {
  282. try
  283. {
  284. MoMtocData task = BLLFactory<BlMtocData>.Instance.FindPointerModel(SysEnvironment.Global_Scanning_Pointer);
  285. if (task == null)
  286. {
  287. //不用报警
  288. LogHelper.log.Error($"存储车号 {SysEnvironment.Global_Scanning_Pointer}扫码数据异常:找不到车号的MTOC数据 ");
  289. return;
  290. }
  291. ProductionData data = new ProductionData();
  292. data.dict["型号"] = task.Model.ToString();
  293. data.dict["颜色"] = task.Color.ToString();
  294. data.dict["VIN"] = task.Vin;
  295. data.dict["VIN2"] = task.Vin;
  296. data.dict["FEM"] = task.Vin;
  297. data.model.Batchid = task.Vin;
  298. data.model.RuleTime = DateTime.Now;
  299. BLLFactory<BlProductData>.Instance.Insert(data.toModel());
  300. LogHelper.log.Info($"存储车号 {SysEnvironment.Global_Scanning_Pointer}扫码数据成功");
  301. }
  302. catch (Exception ex)
  303. {
  304. //不用报警
  305. LogHelper.log.Error($"存储车号 {SysEnvironment.Global_Scanning_Pointer}扫码数据异常: {ex.Message} ");
  306. }
  307. }
  308. #endregion
  309. #region PLC信号响应函数
  310. public void 联机模式(object value)
  311. {
  312. bool signal = Convert.ToBoolean(value);
  313. LogHelper.log.Info($"signal = {signal}");
  314. if (signal)
  315. {
  316. // SysEnvironment.sign_OnlineMode = true;
  317. actionConnected();
  318. }
  319. else
  320. {
  321. // SysEnvironment.sign_OnlineMode = false;
  322. }
  323. }
  324. public void 连锁信号(object value)
  325. {
  326. bool signal = Convert.ToBoolean(value);
  327. LogHelper.log.Info($"signal = {signal}");
  328. if (signal)
  329. {
  330. //SuperQueueScanner.GenMatchResult();
  331. //TackleMatchResult();
  332. }
  333. else
  334. {
  335. //清除扫码工位变量
  336. SysEnvironment.Global_Head_Pointer = "";
  337. SysEnvironment.Global_FEM_Pointer = "";
  338. SuperQueueScanner.Scanning_Match_Result = -3;
  339. //清空写入过的车型
  340. List<MoMtocData> list = new List<MoMtocData>();
  341. MoMtocData model = new MoMtocData() { Vin = "", Model = 0, Color = 0, };
  342. for (int i = 0; i < 10; i++)
  343. {
  344. list.Add(model);
  345. }
  346. if (WriteFromD4001(list))
  347. {
  348. redis.Publish(SysEnvironment.redis_Tighting_Pointer, "");
  349. redis.Publish(SysEnvironment.redis_Tighting_Model, "");
  350. redis.Publish(SysEnvironment.redis_Tighting_Color, "");
  351. }
  352. }
  353. }
  354. /// <summary>
  355. /// 这个函数永远都在 刚刚给出匹配结果的时侯后调用
  356. /// </summary>
  357. private void TackleMatchResult()
  358. {
  359. redis.Publish(SysEnvironment.redis_ScanningMatchRes, SuperQueueScanner.Scanning_Match_Result);
  360. if (SuperQueueScanner.Scanning_Match_Result == 0)
  361. Write("VIN匹配失败信号", true);
  362. else if (SuperQueueScanner.Scanning_Match_Result == -2)
  363. Write("VIN匹配失败信号", true);
  364. else if (SuperQueueScanner.Scanning_Match_Result == -1)
  365. Write("VIN匹配失败信号", false);
  366. else if (SuperQueueScanner.Scanning_Match_Result == 1)
  367. Write("VIN匹配失败信号", false);
  368. }
  369. public void 扫描车头纸二维码请求(object value)
  370. {
  371. bool signal = Convert.ToBoolean(value);
  372. LogHelper.log.Info($"signal = {signal}");
  373. if (!signal)
  374. {
  375. Write("读取车头纸二维码成功", false);
  376. Write("读取车头纸二维码失败", false);
  377. return;
  378. }
  379. string res = SuperQueueScanner.ScanHeadCode();
  380. Write("读取车头纸二维码成功", true);
  381. Read("连锁信号", out bool bChain);
  382. if (bChain)
  383. {
  384. if (SysEnvironment.Global_Head_Pointer == "" && !SkipHeadScan)
  385. {
  386. SysEnvironment.Global_Head_Pointer = res;
  387. redis.Publish(SysEnvironment.redis_Scanning_HeadCode_Pointer, SysEnvironment.Global_Head_Pointer);
  388. LogHelper.log.Info($"车头指针跳至 {SysEnvironment.Global_Head_Pointer}");
  389. SuperQueueScanner.GenMatchResult();
  390. TackleMatchResult();
  391. }
  392. }
  393. else//切除状态下,扫码跳指针
  394. {
  395. MoveScanningPointer();
  396. }
  397. }
  398. public void 扫描FEM二维码请求(object value)
  399. {
  400. bool signal = Convert.ToBoolean(value);
  401. LogHelper.log.Info($"signal = {signal}");
  402. if (!signal)
  403. {
  404. Write("读取FEM二维码成功", false);
  405. Write("读取FEM二维码失败", false);
  406. return;
  407. }
  408. string res = SuperQueueScanner.ScanFEMCode();
  409. Write("读取FEM二维码成功", true);
  410. if (SysEnvironment.Global_FEM_Pointer == "" && !SkipFEMScan)
  411. {
  412. SysEnvironment.Global_FEM_Pointer = res;
  413. redis.Publish(SysEnvironment.redis_Scanning_FEMCode_Pointer, SysEnvironment.Global_FEM_Pointer);
  414. LogHelper.log.Info($"FEM指针跳至 {SysEnvironment.Global_FEM_Pointer}");
  415. SuperQueueScanner.GenMatchResult();
  416. TackleMatchResult();
  417. //手动匹配及FEM来慢的时侯,读取M1118
  418. Read("请求拧紧工位数据写入", out bool delayWrite);
  419. if (delayWrite) 请求拧紧工位数据写入(true);
  420. }
  421. }
  422. /// <summary>
  423. ///手动匹配会读取M1118(如果M1118已经发出的话)
  424. ///因此要匹配之前调好指针,这样进入下一次匹配时,才不至于又失败
  425. /// </summary>
  426. public void ManualForceMatchSuccess()
  427. {
  428. SuperQueueScanner.Scanning_Match_Result = 1;
  429. TackleMatchResult();
  430. //手动匹配及FEM来慢的时侯,读取M1118
  431. Read("请求拧紧工位数据写入", out bool delayWrite);
  432. if (delayWrite) 请求拧紧工位数据写入(true);
  433. }
  434. public void ManualForceReleaseEmpty()
  435. {
  436. //SuperQueueScanner.Scanning_Match_Result = -1;
  437. //TackleMatchResult();
  438. }
  439. public void ManualForceWrite2Tighting(short model, short color, string vin)
  440. {
  441. //手动写入不会跳任何指针【禁用此功能,使用匹配指针按钮即可】
  442. if (WriteD4001(model, color, vin))
  443. {
  444. Write("拧紧工位数据写入成功", true);
  445. SysEnvironment.Global_Tighting_Pointer = vin;
  446. SysEnvironment.Global_Tighting_Model = model;
  447. SysEnvironment.Global_Tighting_Color = color;
  448. redis.Publish(SysEnvironment.redis_Tighting_Pointer, vin);
  449. redis.Publish(SysEnvironment.redis_Tighting_Model, model);
  450. redis.Publish(SysEnvironment.redis_Tighting_Color, color);
  451. }
  452. Read("请求拧紧工位数据写入", out bool bwrite);
  453. if (!bwrite)
  454. Write("拧紧工位数据写入成功", false);
  455. }
  456. public void 请求拧紧工位数据写入(object value)
  457. {
  458. bool signal = Convert.ToBoolean(value);
  459. LogHelper.log.Info($"signal = {signal}");
  460. if (!signal)
  461. {
  462. Write("拧紧工位数据写入成功", false);
  463. return;
  464. }
  465. RefreshTightingUI(null);
  466. if (!WriteD4001(0, 0, "")) return;
  467. //匹配失败0、没匹配完-2 切除状态不匹配 -3
  468. if (SuperQueueScanner.Scanning_Match_Result < 1)
  469. {
  470. LogHelper.log.Error($"匹配失败,无法写入车型。当前指针 {SysEnvironment.Global_Scanning_Pointer}");
  471. return;
  472. }
  473. // == 1 进入匹配成功逻辑
  474. LogHelper.log.Info($"匹配成功,即将写入车型。当前指针 {SysEnvironment.Global_Scanning_Pointer}");
  475. List<MoMtocData> listMtoc = BLLFactory<BlMtocData>.Instance.FindFromThis(SysEnvironment.Global_Scanning_Pointer, 10);
  476. if (listMtoc == null || listMtoc.Count < 1)
  477. {
  478. LogHelper.log.Error($"找不到指针所指车型信息!!");
  479. return;
  480. }
  481. if (!WriteFromD4001(listMtoc))
  482. {
  483. return;
  484. }
  485. Write("拧紧工位数据写入成功", true);
  486. //230214 更新拧紧工位显示之前,进行TightingMonitor的数据更新
  487. UpdateFormMonitorTightenData();
  488. RefreshTightingUI(listMtoc[0]);
  489. //AS400指针下调
  490. //车头指针下调(最多两个,如果是第二个就不调)
  491. //FEM指针下调(最多三个,如果是第三个就不调)
  492. //匹配下一台车
  493. MoveScanningPointer();
  494. MoveHeadPointer();
  495. MoveFEMPointer();
  496. SuperQueueScanner.GenMatchResult();
  497. TackleMatchResult();
  498. }
  499. private bool WriteFromD4001(List<MoMtocData> list)
  500. {
  501. int length = list.Count;
  502. string startDevice_Type = "D4001";
  503. string startDevice_Color = "D6001";
  504. string startDevice_VIN = "D8010";
  505. short[] data1 = new short[length];
  506. short[] data2 = new short[length];
  507. short[] data3 = new short[length * 10];
  508. for (int i = 0; i < list.Count; i++)
  509. {
  510. data1[i] = Convert.ToInt16(list[i].Model);
  511. data2[i] = Convert.ToInt16(list[i].Color);
  512. int tmpCount = 0;
  513. short[] tmpShorts = String2Shorts(list[i].Vin);
  514. for (int j = i * 10; j < i * 10 + 10; j++)
  515. {
  516. data3[j] = tmpShorts[tmpCount];
  517. tmpCount++;
  518. }
  519. }
  520. int res1 = actEasyIF.WriteDeviceBlock2(startDevice_Type, length, ref data1[0]);
  521. int res2 = actEasyIF.WriteDeviceBlock2(startDevice_Color, length, ref data2[0]);
  522. int res3 = actEasyIF.WriteDeviceBlock2(startDevice_VIN, length * 10, ref data3[0]);
  523. return res1 == 0 && res2 == 0 && res3 == 0;
  524. }
  525. private bool WriteD4001(short model, short color, string vin)
  526. {
  527. string startDevice_Type = "D4001";
  528. string startDevice_Color = "D6001";
  529. string startDevice_VIN = "D8010";
  530. short[] data1 = new short[1] { model };
  531. short[] data2 = new short[1] { color };
  532. short[] data3 = new short[10];
  533. short[] tmpShorts = String2Shorts(vin);
  534. for (int i = 0; i < 10; i++)
  535. {
  536. data3[i] = tmpShorts[i];
  537. }
  538. int res1 = actEasyIF.WriteDeviceBlock2(startDevice_Type, 1, ref data1[0]);
  539. int res2 = actEasyIF.WriteDeviceBlock2(startDevice_Color, 1, ref data2[0]);
  540. int res3 = actEasyIF.WriteDeviceBlock2(startDevice_VIN, 10, ref data3[0]);
  541. return res1 == 0 && res2 == 0 && res3 == 0;
  542. }
  543. /// <summary>
  544. /// 更新本地拧紧工位信息
  545. /// </summary>
  546. /// <param name="modelScanned"></param>
  547. public void RefreshTightingUI(MoMtocData modelScanned)
  548. {
  549. if (modelScanned == null)
  550. {
  551. SysEnvironment.Global_Tighting_Pointer = "";
  552. SysEnvironment.Global_Tighting_Model = 0;
  553. SysEnvironment.Global_Tighting_Color = 0;
  554. redis.Publish(SysEnvironment.redis_Tighting_Pointer, "");
  555. redis.Publish(SysEnvironment.redis_Tighting_Model, "");
  556. redis.Publish(SysEnvironment.redis_Tighting_Color, "");
  557. }
  558. else
  559. {
  560. SysEnvironment.Global_Tighting_Pointer = modelScanned.Vin;
  561. SysEnvironment.Global_Tighting_Model = modelScanned.Model;
  562. SysEnvironment.Global_Tighting_Color = modelScanned.Color;
  563. redis.Publish(SysEnvironment.redis_Tighting_Pointer, modelScanned.Vin);
  564. redis.Publish(SysEnvironment.redis_Tighting_Model, modelScanned.Model);
  565. redis.Publish(SysEnvironment.redis_Tighting_Color, modelScanned.Color);
  566. }
  567. }
  568. public void MoveScanningPointer()
  569. {
  570. MoMtocData model2Scan = BLLFactory<BlMtocData>.Instance.FindPointerNextModel(SysEnvironment.Global_Scanning_Pointer);
  571. SysEnvironment.Global_Scanning_Pointer = model2Scan == null ? "" : model2Scan.Vin;
  572. redis.Publish(SysEnvironment.redis_Scanning_Pointer, SysEnvironment.Global_Scanning_Pointer);
  573. LogHelper.log.Info($"扫码工位跳指针到 {SysEnvironment.Global_Scanning_Pointer}");
  574. }
  575. public void MoveHeadPointer()
  576. {
  577. int index = SuperQueueScanner.HeadCodeList.FindIndex(t => t.ScanResult == SysEnvironment.Global_Head_Pointer);
  578. if (index + 1 >= SuperQueueScanner.HeadCodeList.Count) SysEnvironment.Global_Head_Pointer = "";
  579. else SysEnvironment.Global_Head_Pointer = SuperQueueScanner.HeadCodeList[index + 1].ScanResult;
  580. redis.Publish(SysEnvironment.redis_Scanning_HeadCode_Pointer, SysEnvironment.Global_Head_Pointer);
  581. LogHelper.log.Info($"车头指针跳至 {SysEnvironment.Global_Head_Pointer}");
  582. }
  583. public void MoveFEMPointer()
  584. {
  585. int index = SuperQueueScanner.FEMCodeList.FindIndex(t => t.ScanResult == SysEnvironment.Global_FEM_Pointer);
  586. if (index + 1 >= SuperQueueScanner.FEMCodeList.Count) SysEnvironment.Global_FEM_Pointer = "";
  587. else SysEnvironment.Global_FEM_Pointer = SuperQueueScanner.FEMCodeList[index + 1].ScanResult;
  588. redis.Publish(SysEnvironment.redis_Scanning_FEMCode_Pointer, SysEnvironment.Global_FEM_Pointer);
  589. LogHelper.log.Info($"FEM指针跳至 {SysEnvironment.Global_FEM_Pointer}");
  590. }
  591. public static short itightRes1 = 0;
  592. public static short itightRes2 = 0;
  593. public static short itightRes3 = 0;
  594. public static short itightRes4 = 0;
  595. public static float torque1 = 0.0f;
  596. public static float torque2 = 0.0f;
  597. public static float torque3 = 0.0f;
  598. public static float torque4 = 0.0f;
  599. public static float angle1 = 0.0f;
  600. public static float angle2 = 0.0f;
  601. public static float angle3 = 0.0f;
  602. public static float angle4 = 0.0f;
  603. public static short old_itightRes1 = 0;
  604. public static short old_itightRes2 = 0;
  605. public static short old_itightRes3 = 0;
  606. public static short old_itightRes4 = 0;
  607. public static float old_torque1 = 0.0f;
  608. public static float old_torque2 = 0.0f;
  609. public static float old_torque3 = 0.0f;
  610. public static float old_torque4 = 0.0f;
  611. public static float old_angle1 = 0.0f;
  612. public static float old_angle2 = 0.0f;
  613. public static float old_angle3 = 0.0f;
  614. public static float old_angle4 = 0.0f;
  615. public static string oldVIN = "";
  616. public static string newVIN = "";
  617. public void UpdateFormMonitorTightenData()
  618. {
  619. oldVIN = SysEnvironment.Global_Tighting_Pointer;//这个是发车后,刚刚拧紧完的 (搬出的)
  620. old_itightRes1 = itightRes1;
  621. old_itightRes2 = itightRes2;
  622. old_itightRes3 = itightRes3;
  623. old_itightRes4 = itightRes4;
  624. old_torque1 = torque1;
  625. old_torque2 = torque2;
  626. old_torque3 = torque3;
  627. old_torque4 = torque4;
  628. old_angle1 = angle1;
  629. old_angle2 = angle2;
  630. old_angle3 = angle3;
  631. old_angle4 = angle4;
  632. newVIN = SysEnvironment.Global_Scanning_Pointer; //这个是发车后,从扫码位置过来的,还没拧的
  633. itightRes1 = 0;
  634. itightRes2 = 0;
  635. itightRes3 = 0;
  636. itightRes4 = 0;
  637. torque1 = 0;
  638. torque2 = 0;
  639. torque3 = 0;
  640. torque4 = 0;
  641. angle1 = 0;
  642. angle2 = 0;
  643. angle3 = 0;
  644. angle4 = 0;
  645. }
  646. public void 读取拧紧结果请求(object value)
  647. {
  648. bool signal = Convert.ToBoolean(value);
  649. LogHelper.log.Info($"signal = {signal}");
  650. if (!signal)
  651. {
  652. Write("读取拧紧结果完成", false);
  653. return;
  654. }
  655. Read("拧紧结果1", out itightRes1);
  656. Read("拧紧结果2", out itightRes2);
  657. Read("拧紧结果3", out itightRes3);
  658. Read("拧紧结果4", out itightRes4);
  659. Read("拧紧力矩1", out torque1);
  660. Read("拧紧力矩2", out torque2);
  661. Read("拧紧力矩3", out torque3);
  662. Read("拧紧力矩4", out torque4);
  663. Read("拧紧角度1", out angle1);
  664. Read("拧紧角度2", out angle2);
  665. Read("拧紧角度3", out angle3);
  666. Read("拧紧角度4", out angle4);
  667. try
  668. {
  669. string curerntVIN = SysEnvironment.Global_Tighting_Pointer;
  670. int currentModel = SysEnvironment.Global_Tighting_Model;
  671. int curerntColor = SysEnvironment.Global_Tighting_Color;
  672. MoProductData model_data = BLLFactory<BlProductData>.Instance.Find2Bind(curerntVIN);
  673. if (model_data == null)
  674. {
  675. ProductionData data = new ProductionData();
  676. data.dict["型号"] = currentModel.ToString();
  677. data.dict["颜色"] = curerntColor.ToString();
  678. data.dict["VIN"] = curerntVIN;
  679. data.dict["FEM"] = curerntVIN;
  680. data.dict["VIN2"] = curerntVIN;
  681. data.dict["状态1"] = GetTightResult(itightRes1);
  682. data.dict["状态2"] = GetTightResult(itightRes2);
  683. data.dict["状态3"] = GetTightResult(itightRes3);
  684. data.dict["状态4"] = GetTightResult(itightRes4);
  685. data.dict["力矩1"] = torque1.ToString();
  686. data.dict["力矩2"] = torque2.ToString();
  687. data.dict["力矩3"] = torque3.ToString();
  688. data.dict["力矩4"] = torque4.ToString();
  689. data.dict["角度1"] = angle1.ToString();
  690. data.dict["角度2"] = angle2.ToString();
  691. data.dict["角度3"] = angle3.ToString();
  692. data.dict["角度4"] = angle4.ToString();
  693. data.dict["生产完成"] = "1";
  694. data.model.Batchid = curerntVIN;
  695. data.model.RuleTime = DateTime.Now;
  696. BLLFactory<BlProductData>.Instance.Insert(data.toModel());
  697. Write("读取拧紧结果完成", true);
  698. }
  699. else
  700. {
  701. ProductionData data = new ProductionData();
  702. data.LoadData(model_data);
  703. data.dict["状态1"] = GetTightResult(itightRes1);
  704. data.dict["状态2"] = GetTightResult(itightRes2);
  705. data.dict["状态3"] = GetTightResult(itightRes3);
  706. data.dict["状态4"] = GetTightResult(itightRes4);
  707. data.dict["力矩1"] = torque1.ToString();
  708. data.dict["力矩2"] = torque2.ToString();
  709. data.dict["力矩3"] = torque3.ToString();
  710. data.dict["力矩4"] = torque4.ToString();
  711. data.dict["角度1"] = angle1.ToString();
  712. data.dict["角度2"] = angle2.ToString();
  713. data.dict["角度3"] = angle3.ToString();
  714. data.dict["角度4"] = angle4.ToString();
  715. data.dict["生产完成"] = "1";
  716. data.model.RuleTime = DateTime.Now;
  717. BLLFactory<BlProductData>.Instance.Update(data.toModel(), data.model.DataId);
  718. Write("读取拧紧结果完成", true);
  719. }
  720. }
  721. catch (Exception ex)
  722. {
  723. //报警!!
  724. LogHelper.log.Error($"存储拧紧数据异常: {ex.Message} ");
  725. return;
  726. }
  727. }
  728. #endregion
  729. #region 私有方法
  730. private bool WriteDevice(List<MoMtocData> list, int length, bool writeD8000 = false)
  731. {
  732. string startDevice_Type = "D4001";
  733. string startDevice_Color = "D6001";
  734. string startDevice_VIN = "D8010";
  735. if (writeD8000)
  736. {
  737. startDevice_Type = "D4000";
  738. startDevice_Color = "D6000";
  739. startDevice_VIN = "D8000";
  740. }
  741. short[] data1 = new short[length];
  742. short[] data2 = new short[length];
  743. short[] data3 = new short[length * 10];
  744. for (int i = 0; i < list.Count; i++)
  745. {
  746. data1[i] = Convert.ToInt16(list[i].Model);
  747. data2[i] = Convert.ToInt16(list[i].Color);
  748. int tmpCount = 0;
  749. short[] tmpShorts = String2Shorts(list[i].Vin);
  750. for (int j = i * 10; j < i * 10 + 10; j++)
  751. {
  752. data3[j] = tmpShorts[tmpCount];
  753. tmpCount++;
  754. }
  755. }
  756. int res1 = actEasyIF.WriteDeviceBlock2(startDevice_Type, length, ref data1[0]);
  757. int res2 = actEasyIF.WriteDeviceBlock2(startDevice_Color, length, ref data2[0]);
  758. int res3 = actEasyIF.WriteDeviceBlock2(startDevice_VIN, length * 10, ref data3[0]);
  759. return res1 == 0 && res2 == 0 && res3 == 0;
  760. }
  761. private short[] String2Shorts(string ss, int minimumLength = 10)
  762. {
  763. char[] cc = ss.ToCharArray();
  764. short[] ii = new short[cc.Length];
  765. if (cc.Length < minimumLength)
  766. ii = new short[minimumLength];
  767. for (int i = 0; i < cc.Length; i += 2)
  768. {
  769. if (i + 1 == cc.Length)
  770. ii[i / 2] = (short)cc[i];
  771. else
  772. ii[i / 2] = (short)(cc[i + 1] * 256 + cc[i]);
  773. }
  774. return ii;
  775. }
  776. /// <summary>
  777. /// 清空PLC数据展示地址数据
  778. /// </summary>
  779. private void ClearPLC()
  780. {
  781. try
  782. {
  783. int length = 12;
  784. string startDevice_Type = "D4000";
  785. short[] data1 = new short[length];
  786. int res1 = actEasyIF.WriteDeviceBlock2(startDevice_Type, length, ref data1[0]);
  787. string startDevice_Color = "D6000";
  788. short[] data2 = new short[length];
  789. int res2 = actEasyIF.WriteDeviceBlock2(startDevice_Color, length, ref data2[0]);
  790. string startDevice_VIN = "D8000";
  791. //short[] data3 = Enumerable.Repeat<short>(Convert.ToInt16( '\0'), length).ToArray();;
  792. //int res3 = actEasyIF.WriteDeviceBlock2(startDevice_VIN, length, ref data3[0]);//多此一举
  793. short[] data3 = new short[length * 10];
  794. int res3 = actEasyIF.WriteDeviceBlock2(startDevice_VIN, length * 10, ref data3[0]);
  795. // SysEnvironment.SignTrrigerScanPLCData = res1 == 0 && res2 == 0 && res3 == 0;//用于监控节点界面刷新显示
  796. }
  797. catch (Exception ex)
  798. {
  799. LogHelper.log.Error($"ClearPLC()出错: {ex.Message}");
  800. }
  801. }
  802. private string GetTightResult(int ires)
  803. {
  804. switch (ires)
  805. {
  806. case 1:
  807. return "OK";
  808. case 2:
  809. return "NG";
  810. case 0:
  811. return "未拧紧";
  812. default:
  813. return ires.ToString();
  814. }
  815. }
  816. #endregion
  817. }
  818. }