DispatchSystem - 副本.cs 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using AGV_WPF_Global;
  11. using System.Data.OleDb;
  12. using System.Xml.Serialization;
  13. using System.IO;
  14. using System.Threading.Tasks;
  15. using AGV_WPF.Models;
  16. using HslCommunication;
  17. using AGV_WPF.Tools;
  18. using System.Diagnostics;
  19. namespace AGV_WPF
  20. {
  21. public partial class DispatchSystem : Form
  22. {
  23. DataTable dtConfig = new DataTable();
  24. HslCommunication.OperateResult result;
  25. int HeartBitCount = 1;
  26. private delegate void InvokeDelegate();
  27. Dictionary<int, string> checkPosDic = new Dictionary<int, string>();
  28. Dictionary<int, int> AGVIDWithPosByCheckPos = new Dictionary<int, int>();
  29. Dictionary<int, int> AGVIDWithRouteByCheckPos = new Dictionary<int, int>();
  30. Dictionary<string, byte> DBWithByteValue = new Dictionary<string, byte>();
  31. private static object portLock = new object();
  32. Stopwatch sw = new Stopwatch();
  33. Dictionary<int, CFDClass> dicReleaseArea = new Dictionary<int, CFDClass>();
  34. public int CurCFDRoute = -1;
  35. Dictionary<string, bool> InitOK = new Dictionary<string, bool>();
  36. public DispatchSystem()
  37. {
  38. InitializeComponent();
  39. }
  40. private void DispatchSystem_Load(object sender, EventArgs e)
  41. {
  42. LoadConfig();
  43. this.FormClosing += new FormClosingEventHandler(DispatchSystem_FormClosing);
  44. Thread thread2 = new Thread(HeartBit);
  45. thread2.Start();
  46. Thread thread = new Thread(Analzye);
  47. thread.Start();
  48. }
  49. void DispatchSystem_FormClosing(object sender, FormClosingEventArgs e)
  50. {
  51. e.Cancel = true;
  52. }
  53. /// <summary>
  54. /// 加载配置
  55. /// </summary>
  56. public void LoadConfig()
  57. {
  58. sw.Restart();
  59. tabControl1.TabPages.Clear();
  60. 物料设置ToolStripMenuItem.DropDownItems.Clear();
  61. checkPosDic.Clear();
  62. AGVIDWithPosByCheckPos.Clear();
  63. DBWithByteValue.Clear();
  64. HeartBitCount = 1;
  65. XmlSerializer deserializer1 = new XmlSerializer(GlobalPara.gs_configInfo.GetType());
  66. try
  67. {
  68. using (TextReader tr = new StreamReader("CConfig.xml"))
  69. {
  70. GlobalPara.gs_configInfo = (AGV_WPF.Tools.CConfigData)deserializer1.Deserialize(tr);
  71. }
  72. }
  73. catch (Exception ex)
  74. { }
  75. string currPath = System.AppDomain.CurrentDomain.BaseDirectory;
  76. string dataPath = currPath + @"data";
  77. string dataFile = dataPath + @"\基础配置表.xls";
  78. try
  79. {
  80. string confStr1 = @"Provider=Microsoft.ACE.OLEDB.12.0;";
  81. string confStr2 = @"Persist Security Info=false;";
  82. string confStr3 = @"Data Source=" + dataFile + ";";
  83. string confStr4 = @"Extended Properties=" + "'Excel 12.0;HDR=YES;IMEX=1'";
  84. string connStr = confStr1 + confStr2 + confStr3 + confStr4;
  85. OleDbConnection oleDC = new OleDbConnection(connStr);
  86. OleDbDataAdapter oleDDA1 = new OleDbDataAdapter("Select * From [参数配置$]", oleDC);
  87. oleDDA1.Fill(dtConfig);
  88. OleDbDataAdapter oleDDA2 = new OleDbDataAdapter("Select * From [物料类型$]", oleDC);
  89. DataTable temp = new DataTable();
  90. oleDDA2.Fill(temp);
  91. if (temp.Rows.Count > 0)
  92. {
  93. for (int i = 0; i < temp.Rows.Count; i++)
  94. {
  95. ToolStripMenuItem tsm = new ToolStripMenuItem(temp.Rows[i][1].ToString());
  96. 物料设置ToolStripMenuItem.DropDownItems.Add(tsm);
  97. tsm.Font = new Font("黑体", 15);
  98. if (GlobalPara.gs_configInfo.CurrentMatName == temp.Rows[i][1].ToString())
  99. {
  100. tsm.CheckState = CheckState.Checked;
  101. }
  102. tsm.Click += new EventHandler(tsm_Click);
  103. }
  104. }
  105. tabControl1.DrawMode = TabDrawMode.Normal;
  106. tabControl1.SizeMode = TabSizeMode.Fixed;
  107. tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
  108. GlobalPara.s7 = new HslCommunication.Profinet.Siemens.SiemensS7Net(HslCommunication.Profinet.Siemens.SiemensPLCS.S1500,
  109. dtConfig.Rows[0][1].ToString());
  110. GlobalPara.s7.Rack = (byte)Convert.ToInt16(dtConfig.Rows[2][1].ToString());
  111. GlobalPara.s7.Slot = (byte)Convert.ToInt16(dtConfig.Rows[3][1].ToString());
  112. GlobalPara.s7.ConnectTimeOut = 200;
  113. GlobalPara.s7.ReceiveTimeOut = 200;
  114. result = GlobalPara.s7.ConnectServer();
  115. sw.Stop();
  116. Console.WriteLine("配置初始化总共花费{0}s.", sw.Elapsed.TotalSeconds);
  117. sw.Restart();
  118. InitView();
  119. sw.Stop();
  120. Console.WriteLine("界面初始化总共花费{0}s.", sw.Elapsed.TotalSeconds);
  121. }
  122. catch (System.Exception ex)
  123. {
  124. MessageBox.Show(ex.StackTrace);
  125. }
  126. }
  127. void tsm_Click(object sender, EventArgs e)
  128. {
  129. for (int i = 0; i < 物料设置ToolStripMenuItem.DropDownItems.Count; i++)
  130. {
  131. if (((ToolStripMenuItem)物料设置ToolStripMenuItem.DropDownItems[i]).Text == ((ToolStripMenuItem)sender).Text
  132. && ((ToolStripMenuItem)sender).CheckState == CheckState.Checked)
  133. {
  134. return;
  135. }
  136. ((ToolStripMenuItem)物料设置ToolStripMenuItem.DropDownItems[i]).CheckState = CheckState.Unchecked;
  137. }
  138. ((ToolStripMenuItem)sender).Checked = true;
  139. GlobalPara.gs_configInfo.CurrentMatName = ((ToolStripMenuItem)sender).Text;
  140. XmlSerializer serializer2 = new XmlSerializer(GlobalPara.gs_configInfo.GetType());
  141. using (TextWriter tw = new StreamWriter("CConfig.xml"))
  142. {
  143. serializer2.Serialize(tw, GlobalPara.gs_configInfo);
  144. }
  145. this.DispatchSystem_Load(null, null);
  146. }
  147. void tsm_CheckedChanged(object sender, EventArgs e)
  148. {
  149. //throw new NotImplementedException();
  150. }
  151. void tsm_CheckStateChanged(object sender, EventArgs e)
  152. {
  153. //throw new NotImplementedException();
  154. }
  155. /// <summary>
  156. /// 界面初始化
  157. /// </summary>
  158. /// <param name="temp"></param>
  159. public void InitView()
  160. {
  161. string currPath = System.AppDomain.CurrentDomain.BaseDirectory;
  162. string dataPath = currPath + @"data\";
  163. string dataFile = dataPath + GlobalPara.gs_configInfo.CurrentMatName + ".xls";
  164. if (false == File.Exists(dataFile))
  165. {
  166. DirectoryInfo di = new DirectoryInfo(dataPath);
  167. foreach (FileInfo f in di.GetFiles())
  168. {
  169. if (f.Name != "基础配置表" && f.Extension.Contains(".xls"))
  170. {
  171. GlobalPara.gs_configInfo.CurrentMatName = f.Name.Replace(".xls", "");
  172. for (int i = 0; i < 物料设置ToolStripMenuItem.DropDownItems.Count; i++)
  173. {
  174. if (物料设置ToolStripMenuItem.DropDownItems[i].Text == GlobalPara.gs_configInfo.CurrentMatName)
  175. {
  176. ((ToolStripMenuItem)物料设置ToolStripMenuItem.DropDownItems[i]).CheckState = CheckState.Checked;
  177. }
  178. }
  179. dataFile = f.FullName;
  180. break;
  181. }
  182. }
  183. }
  184. try
  185. {
  186. string confStr1 = @"Provider=Microsoft.ACE.OLEDB.12.0;";
  187. string confStr2 = @"Persist Security Info=false;";
  188. string confStr3 = @"Data Source=" + dataFile + ";";
  189. string confStr4 = @"Extended Properties=" + "'Excel 12.0;HDR=YES;IMEX=1'";
  190. string connStr = confStr1 + confStr2 + confStr3 + confStr4;
  191. OleDbConnection oleDC = new OleDbConnection(connStr);
  192. OleDbDataAdapter oleDDA2 = new OleDbDataAdapter("Select * From [线体信息$]", oleDC);
  193. DataTable LineDt = new DataTable();
  194. oleDDA2.Fill(LineDt);
  195. Dictionary<Thread, object[]> listThread = new Dictionary<Thread, object[]>();
  196. for (int i = 0; i < LineDt.Rows.Count; i++)
  197. {
  198. string lineName = LineDt.Rows[i]["线体名称"].ToString();
  199. TabPage tb = new TabPage(lineName);
  200. tb.Font = new System.Drawing.Font("楷体", tb.Font.Size + 5);
  201. oleDDA2 = new OleDbDataAdapter("Select * From [" + lineName + "$]", oleDC);
  202. DataTable temp2 = new DataTable();
  203. temp2.TableName = lineName;
  204. oleDDA2.Fill(temp2);
  205. if (LineDt.Rows[i]["判断点"] != null && !string.IsNullOrEmpty(LineDt.Rows[i]["判断点"].ToString()) && LineDt.Rows[i]["判断点"].ToString() != "0")
  206. {
  207. if (!checkPosDic.ContainsKey(int.Parse(LineDt.Rows[i]["判断点"].ToString())))
  208. {
  209. checkPosDic.Add(int.Parse(LineDt.Rows[i]["判断点"].ToString()), lineName);
  210. }
  211. }
  212. int X = 10, Y = 10;
  213. List<Button> listBtn = new List<Button>();
  214. for (int j = 1; j <= temp2.Rows.Count; j++)
  215. {
  216. if (temp2.Columns.Contains("组号") && !dicReleaseArea.ContainsKey(int.Parse(temp2.Rows[j - 1]["组号"].ToString())))
  217. {
  218. dicReleaseArea.Add(int.Parse(temp2.Rows[j - 1]["组号"].ToString()), null);
  219. }
  220. if (temp2.Rows[j - 1]["放行DB"] != null && !string.IsNullOrEmpty(temp2.Rows[j - 1]["放行DB"].ToString()) && temp2.Rows[j - 1]["放行DB"].ToString() != "0")
  221. {
  222. if (!DBWithByteValue.ContainsKey((temp2.Rows[j - 1]["放行DB"].ToString())))
  223. {
  224. DBWithByteValue.Add((temp2.Rows[j - 1]["放行DB"].ToString()), (byte)255);
  225. }
  226. }
  227. Button btn = new Button();
  228. btn.Text = temp2.Rows[j - 1][1].ToString();
  229. btn.Location = new Point(X, Y);
  230. btn.Font = new Font("宋体", 15);
  231. btn.Size = new Size(100, 80);
  232. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  233. X += btn.Size.Width + 20;
  234. if (j % 6 == 0)
  235. {
  236. X = 10; Y += btn.Height + 20;
  237. }
  238. btn.Tag = temp2.Rows[j - 1][2].ToString();
  239. tb.Controls.Add(btn);
  240. listBtn.Add(btn);
  241. }
  242. Tools.TabPageTag tt = new Tools.TabPageTag();
  243. tt.Table = temp2.Copy();
  244. tt.ListBtn = new List<Button>();
  245. tt.ListBtn.AddRange(listBtn);
  246. tb.Tag = tt;
  247. tabControl1.TabPages.Add(tb);
  248. object[] obj = new object[3];
  249. obj[0] = listBtn; obj[1] = 0; obj[2] = lineName;
  250. listThread.Add(new Thread(new ParameterizedThreadStart(DockingOpeare0)), obj);//.Start(listBtn);
  251. // DockingOpeare0(obj);
  252. // if (lineName == "充放电测试") { tabControl1.SelectedTab = tb; }
  253. }
  254. foreach (Thread t in listThread.Keys)
  255. {
  256. t.Start(listThread[t]);
  257. }
  258. // Thread.Sleep(8000);
  259. new Thread(new ThreadStart(checkingOperate)).Start();
  260. new Thread(new ThreadStart(CFDCheck)).Start();
  261. // new Thread(new ThreadStart(checkingRelease)).Start();
  262. }
  263. catch (System.Exception ex)
  264. {
  265. MessageBox.Show(ex.StackTrace);
  266. }
  267. }
  268. /// <summary>
  269. /// 判断点发送5次仍未放行的问题
  270. /// </summary>
  271. public void checkingRelease()
  272. {
  273. while (true)
  274. {
  275. if (AGVIDWithRouteByCheckPos.Count > 0)
  276. {
  277. List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
  278. ParallelLoopResult result = Parallel.ForEach<int>(AGVIDWithRouteByCheckPos.Keys, new ParallelOptions { MaxDegreeOfParallelism = 2 }, pos =>
  279. {
  280. IEnumerable<AGVInfoModel> temp = listModel.Where(o => o.AGVNUM == pos && (o.RUNSTATUS_Cur == 0x40)
  281. && AGVIDWithRouteByCheckPos.ContainsKey(o.AGVNUM) && AGVIDWithPosByCheckPos.ContainsKey(o.AGVNUM));
  282. if (temp.Count() > 0)
  283. {
  284. lock (AGVIDWithRouteByCheckPos)
  285. {
  286. if (AGVIDWithRouteByCheckPos.ContainsKey(pos))
  287. {
  288. AGVIDWithRouteByCheckPos.Remove(pos);
  289. }
  290. }
  291. }
  292. else
  293. {
  294. temp = listModel.Where(o => o.AGVNUM == pos && (o.RUNSTATUS_Cur == 0x43 || o.ALARMCODE == "9")
  295. && AGVIDWithRouteByCheckPos.ContainsKey(o.AGVNUM) && AGVIDWithPosByCheckPos.ContainsKey(o.AGVNUM));
  296. if (temp.Count() > 0)
  297. {
  298. int agvNum = temp.ToList()[0].AGVNUM;
  299. int mark = temp.ToList()[0].MARKNUM;
  300. DispatcherTool.ReleaseAgvOnStop(mark, AGVIDWithRouteByCheckPos[agvNum], AGVIDWithPosByCheckPos[agvNum]);
  301. }
  302. }
  303. });
  304. if (result.IsCompleted)
  305. {
  306. Console.WriteLine("判断点放行任务总共花费{0}s.", sw.Elapsed.TotalSeconds);
  307. // new Thread(new ThreadStart(checkingRelease)).Start();
  308. }
  309. }
  310. else
  311. {
  312. // new Thread(new ThreadStart(checkingRelease)).Start();
  313. }
  314. Thread.Sleep(200);
  315. }
  316. }
  317. /// <summary>
  318. /// 判断点事件
  319. /// </summary>
  320. public void checkingOperate()
  321. {
  322. Stopwatch sw2 = new Stopwatch();
  323. List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
  324. ParallelLoopResult result = Parallel.ForEach<int>(checkPosDic.Keys, new ParallelOptions { MaxDegreeOfParallelism = 2 }, pos =>
  325. {
  326. IEnumerable<AGVInfoModel> temp = listModel.Where(o => o.MARKNUM == pos && (o.RUNSTATUS_Cur == 0x43 || o.ALARMCODE == "9") && !AGVIDWithPosByCheckPos.ContainsKey(o.AGVNUM));
  327. if (temp.Count() > 0)
  328. {
  329. int num = temp.ToList()[0].AGVNUM;
  330. if (!AGVIDWithPosByCheckPos.ContainsKey(num))
  331. {
  332. string lineName = checkPosDic[pos];
  333. for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
  334. {
  335. if (this.tabControl1.TabPages[i].Text == lineName && InitOK.ContainsKey(lineName)&&InitOK[lineName])
  336. {
  337. TabPageTag tag = this.tabControl1.TabPages[i].Tag as TabPageTag;
  338. DataRow[] drs = null;
  339. if (lineName != "充放电测试")
  340. {
  341. drs = tag.Table.Select("优先级>0", "优先级 asc");
  342. }
  343. else
  344. {
  345. if (CurCFDRoute < 0)
  346. {
  347. drs = tag.Table.Select("优先级>0", "优先级 asc");
  348. }
  349. else
  350. {
  351. drs = tag.Table.Select("优先级>0 and 放行路线='" + CurCFDRoute + "'", "优先级 asc");
  352. }
  353. }
  354. List<string> ChannelDB = new List<string>();
  355. bool b = false;
  356. bool b3 = false;
  357. for (int j = 0; j < drs.Length; j++)
  358. {
  359. if (tag.Table.Columns.Contains("通道DB"))
  360. {
  361. string releaseAddress = drs[j]["通道DB"].ToString();
  362. if (releaseAddress != "0")
  363. {
  364. if (!ChannelDB.Contains(releaseAddress))
  365. {
  366. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  367. if (resultRead.IsSuccess)
  368. {
  369. if (resultRead.Content == (byte)0)
  370. {
  371. b = true;
  372. }
  373. else
  374. {
  375. ChannelDB.Add(releaseAddress);
  376. continue;
  377. }
  378. }
  379. }
  380. else
  381. {
  382. continue;
  383. }
  384. }
  385. else { b = true; }
  386. }
  387. else
  388. {
  389. b = true;
  390. }
  391. if (b)
  392. {
  393. // IEnumerable<DataRow> query =
  394. //from data in tag.Table.AsEnumerable()
  395. //group data by data["路线"] into g
  396. //select g.First();
  397. Button btn = tag.ListBtn.Find(o => o.Tag.ToString() == drs[j]["工位地标"].ToString() && !AGVIDWithPosByCheckPos.ContainsValue(int.Parse(o.Tag.ToString()))
  398. && (o.BackColor == Color.FromKnownColor(KnownColor.Control)
  399. || o.BackColor == Color.FromKnownColor(KnownColor.Green)) && o.BackColor != Color.FromKnownColor(KnownColor.Blue));
  400. if (btn != null)
  401. {
  402. if (lineName == "充放电测试")
  403. {
  404. DataRow[] temp2 = tag.Table.Select("放行路线='" + drs[j]["放行路线"].ToString() + "'", "ID asc");
  405. if (temp2.Count() > 1)
  406. {
  407. int indexFirst = tag.Table.Rows.IndexOf(temp2[0]);
  408. if (indexFirst >= 0 && indexFirst != j)
  409. {
  410. Button btn2 = tag.ListBtn.Find(o => o.Tag.ToString() == tag.Table.Rows[indexFirst]["工位地标"].ToString() &&
  411. (o.BackColor == Color.Yellow || o.BackColor == Color.Blue));
  412. if (btn2 == null)
  413. {
  414. continue;
  415. }
  416. }
  417. else if (indexFirst >= 0 && indexFirst == j)
  418. {
  419. bool b2 = true;
  420. for (int a = 1; a < temp2.Length; a++)
  421. {
  422. Button btn2 = tag.ListBtn.Find(o => o.Tag.ToString() == temp2[a]["工位地标"].ToString()
  423. && o.BackColor == Color.FromKnownColor(KnownColor.Control));
  424. if (btn2 == null) { b2 = false; break; }
  425. }
  426. if (!b2)
  427. {
  428. continue;
  429. }
  430. }
  431. }
  432. }
  433. //找到目的站点,放行路线
  434. AGVIDWithPosByCheckPos.Add(num, int.Parse(drs[j]["工位地标"].ToString()));
  435. AGVIDWithRouteByCheckPos.Add(num, int.Parse(drs[j]["放行路线"].ToString()));
  436. DispatcherTool.ReleaseAgvOnStop(pos, int.Parse(drs[j]["放行路线"].ToString()), int.Parse(drs[j]["工位地标"].ToString()));
  437. if (btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  438. {
  439. btn.BackColor = Color.Blue;
  440. }
  441. if (lineName == "充放电测试")
  442. {
  443. CurCFDRoute = int.Parse(drs[j]["放行路线"].ToString());
  444. }
  445. b3 = true;
  446. break;
  447. }
  448. }
  449. else
  450. {
  451. if (lineName == "充放电测试")
  452. {
  453. CurCFDRoute = -1;
  454. }
  455. }
  456. }
  457. if (!b3)
  458. {
  459. if (lineName == "充放电测试")
  460. {
  461. CurCFDRoute = -1;
  462. }
  463. }
  464. }
  465. }
  466. }
  467. }
  468. });
  469. if (result.IsCompleted)
  470. {
  471. sw2.Stop();
  472. Console.WriteLine("判断点任务总共花费{0}s.", sw.Elapsed.TotalSeconds);
  473. new Thread(new ThreadStart(checkingOperate)).Start();
  474. }
  475. }
  476. public void CFDCheck()
  477. {
  478. ParallelLoopResult result2;
  479. try
  480. {
  481. List<int> list = dicReleaseArea.Keys.ToList();
  482. result2 = Parallel.ForEach<int>(list, new ParallelOptions { MaxDegreeOfParallelism = 2 }, key =>
  483. {
  484. if (dicReleaseArea[key] != null)
  485. {
  486. List<Button> listBtn = dicReleaseArea[key].btns;
  487. if (listBtn != null && listBtn.Count > 0)
  488. {
  489. var result = listBtn.AsParallel().Where(o => o.BackColor == Color.FromKnownColor(KnownColor.Control));
  490. if (result != null && result.Count() == listBtn.Count)
  491. {
  492. CFDClass cfd = dicReleaseArea[key];
  493. lock (dicReleaseArea)
  494. {
  495. dicReleaseArea[key] = null;
  496. }
  497. WriteFile("充放电记录", "(" + cfd.number + "," + cfd.numberInner + ") 组放行完成,已解锁");
  498. }
  499. }
  500. }
  501. });
  502. if (result2.IsCompleted)
  503. {
  504. new Thread(new ThreadStart(CFDCheck)).Start();
  505. }
  506. }
  507. catch (Exception ex)
  508. {
  509. DateTime time = DateTime.Now;
  510. //MessageBox.Show("线程异常:"+e.ExceptionObject);
  511. FileStream stream = File.Open(@"Exception\调度程序异常.txt", FileMode.Append, FileAccess.Write);
  512. StreamWriter writer = new StreamWriter(stream);
  513. if (ex != null)
  514. {
  515. string msg = string.Format("[{0}]Test:\r\nMessage:{1}\r\nStackTrace:\r\n{2}", time.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message, ex.StackTrace);
  516. writer.WriteLine(msg);
  517. writer.Close();
  518. stream.Close();
  519. }
  520. new Thread(new ThreadStart(CFDCheck)).Start();
  521. }
  522. }
  523. public void DockingOpeare0(object o)
  524. {
  525. object[] obj = (object[])o;
  526. DockingOpeare((List<Button>)obj[0], (int)obj[1],(string)obj[2]);
  527. }
  528. /// <summary>
  529. /// 工位事件
  530. /// </summary>
  531. /// <param name="listBtn"></param>
  532. public void DockingOpeare(List<Button> listBtn, int tag,string lineName)
  533. {
  534. ParallelLoopResult result2;
  535. try
  536. {
  537. Stopwatch sw2 = new Stopwatch();
  538. List<Button> list = (List<Button>)listBtn;
  539. // TabPageTag tag = this.tabControl1.TabPages[i].Tag as TabPageTag;
  540. DataTable dt = (list[0].Parent.Tag as TabPageTag).Table;
  541. List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
  542. List<int> list2 = AGVIDWithPosByCheckPos.Values.ToList();
  543. // while (true)
  544. {
  545. // for (int i = 0; i < list.Count; i++)
  546. result2 = Parallel.ForEach<Button>(list, new ParallelOptions { MaxDegreeOfParallelism = 2 }, btn =>
  547. {
  548. //if (list2.Contains((int)btn.Tag))
  549. //{
  550. // IEnumerable<AGVInfoModel> temp = listModel.Where(o => o.MARKNUM.ToString() == btn.Tag.ToString() && o.ConnectStatus == true);
  551. // btn.Invoke(new InvokeDelegate(delegate
  552. // {
  553. // if (btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  554. // {
  555. // btn.BackColor = Color.Blue;
  556. // }
  557. // }));
  558. //}
  559. //else
  560. {
  561. // Button btn = list[i];
  562. // if (btn.Tag != null)
  563. {
  564. IEnumerable<AGVInfoModel> temp = listModel.Where(o => o.MARKNUM.ToString() == btn.Tag.ToString() && o.ConnectStatus == true);
  565. IEnumerable<DataRow> query =
  566. from order in dt.AsEnumerable()
  567. where order["工位地标"].ToString() == btn.Tag.ToString()
  568. select order;
  569. if (temp.Count() > 0)
  570. {
  571. IEnumerable<AGVInfoModel> temp2 = temp.Where(o => ((o.RUNSTATUS_Cur == 0x43 || o.RUNSTATUS_Cur == 0x44)
  572. && o.ConnectStatus == true && o.AGVNUM != 0));
  573. if (temp2.Count() > 0)
  574. {
  575. List<AGVInfoModel> infos = temp2.ToList();
  576. btn.Invoke(new InvokeDelegate(delegate
  577. {
  578. if (btn.BackColor != Color.FromKnownColor(KnownColor.Yellow))
  579. {
  580. AGVInfoModel info = infos[0];
  581. if (AGVIDWithPosByCheckPos.ContainsKey(info.AGVNUM))
  582. {
  583. AGVIDWithPosByCheckPos.Remove(info.AGVNUM);
  584. }
  585. if (AGVIDWithRouteByCheckPos.ContainsKey(info.AGVNUM))
  586. {
  587. AGVIDWithRouteByCheckPos.Remove(info.AGVNUM);
  588. }
  589. if (result != null && result.IsSuccess)
  590. {
  591. OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)info.AGVNUM);
  592. OperateResult rs2 = null;
  593. if (info.ALARMCODE != "5")
  594. {
  595. rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.StopOnStation);
  596. }
  597. else //在工位上时 障碍物的状态也给停站状态
  598. {
  599. rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.StopOnStation);
  600. }
  601. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)(int.Parse(info.ALARMCODE) - 1));
  602. if (rs.IsSuccess && rs2.IsSuccess)
  603. {
  604. btn.BackColor = Color.Yellow;
  605. }
  606. }
  607. }
  608. }));
  609. if (result != null && result.IsSuccess)
  610. {
  611. string releaseAddress = query.ToList()[0]["放行DB"].ToString();
  612. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  613. if (resultRead.IsSuccess && DBWithByteValue.Keys.Contains(releaseAddress) &&
  614. DBWithByteValue[releaseAddress] != resultRead.Content)
  615. {
  616. if (resultRead.Content == (byte)GlobalPara.DBControlValues.OKRelease)
  617. {
  618. if (dt.TableName != "充放电测试")
  619. {
  620. DispatcherTool.ReleaseAgvOnStop(int.Parse(btn.Tag.ToString()),
  621. 0, int.Parse(query.ToList()[0]["目的地标"].ToString()));
  622. lock (portLock)
  623. {
  624. DBWithByteValue[releaseAddress] = resultRead.Content;
  625. }
  626. }
  627. else
  628. {
  629. WriteFile("充放电记录-信号切换", btn.Text + " 放行信号改变,原值为 " + DBWithByteValue[releaseAddress] + " ,新值为 " +
  630. resultRead.Content.ToString() + ",放行地址为 " + releaseAddress + ",地标为:" + btn.Tag.ToString());
  631. int number = int.Parse(query.ToList()[0]["组号"].ToString());
  632. int numberInner = int.Parse(query.ToList()[0]["组内号"].ToString());
  633. if (dicReleaseArea.ContainsKey(number))
  634. {
  635. if (dicReleaseArea[number] == null)
  636. {
  637. CFDClass cfd = new CFDClass();
  638. cfd.number = number;
  639. IEnumerable<int> query2 =
  640. from order in dt.AsEnumerable()
  641. where order["组内号"].ToString() == numberInner.ToString() && order["组号"].ToString() == number.ToString()
  642. select int.Parse(order["工位地标"].ToString());
  643. if (query2 != null && query2.Count() > 0)
  644. {
  645. cfd.btns = new List<Button>();
  646. foreach (int mark in query2.ToList())
  647. {
  648. cfd.btns.Add(list.AsParallel().First(o => o.Tag.ToString() == mark.ToString()));
  649. }
  650. }
  651. cfd.number = number;
  652. cfd.numberInner = int.Parse(query.ToList()[0]["组内号"].ToString());
  653. DispatcherTool.ReleaseAgvOnStop(int.Parse(btn.Tag.ToString()),
  654. 0, int.Parse(query.ToList()[0]["目的地标"].ToString()));
  655. lock (dicReleaseArea)
  656. {
  657. dicReleaseArea[number] = cfd;
  658. WriteFile("充放电记录", "(" + cfd.number + "," + cfd.numberInner + ") 组开始放行,已上锁,当前放行地标:" + btn.Tag.ToString());
  659. }
  660. lock (portLock)
  661. {
  662. DBWithByteValue[releaseAddress] = resultRead.Content;
  663. }
  664. }
  665. else
  666. {
  667. CFDClass cfd = dicReleaseArea[number];
  668. if (cfd.numberInner == numberInner && cfd.number == number)
  669. {
  670. DispatcherTool.ReleaseAgvOnStop(int.Parse(btn.Tag.ToString()),
  671. 0, int.Parse(query.ToList()[0]["目的地标"].ToString()));
  672. lock (portLock)
  673. {
  674. DBWithByteValue[releaseAddress] = resultRead.Content;
  675. }
  676. WriteFile("充放电记录", "(" + cfd.number + "," + cfd.numberInner + ") 组继续放行,已上锁,当前放行地标:" + btn.Tag.ToString());
  677. }
  678. }
  679. }
  680. }
  681. //走OK放行路线
  682. }
  683. else if (resultRead.Content == (byte)GlobalPara.DBControlValues.NGRelease)
  684. {
  685. if (dt.TableName != "充放电测试")
  686. {
  687. DispatcherTool.ReleaseAgvOnStop(int.Parse(btn.Tag.ToString()),
  688. int.Parse(query.ToList()[0]["NG路线"].ToString()));
  689. lock (portLock)
  690. {
  691. DBWithByteValue[releaseAddress] = resultRead.Content;
  692. }
  693. }
  694. else
  695. {
  696. int number = int.Parse(query.ToList()[0]["组号"].ToString());
  697. int numberInner = int.Parse(query.ToList()[0]["组内号"].ToString());
  698. if (dicReleaseArea.ContainsKey(number))
  699. {
  700. if (dicReleaseArea[number] == null)
  701. {
  702. CFDClass cfd = new CFDClass();
  703. cfd.number = number;
  704. IEnumerable<int> query2 =
  705. from order in dt.AsEnumerable()
  706. where order["组内号"].ToString() == numberInner.ToString() && order["组号"].ToString() == number.ToString()
  707. select int.Parse(order["工位地标"].ToString());
  708. if (query2 != null && query2.Count() > 0)
  709. {
  710. cfd.btns = new List<Button>();
  711. foreach (int mark in query2.ToList())
  712. {
  713. cfd.btns.Add(list.Find(o => o.Tag.ToString() == mark.ToString()));
  714. }
  715. }
  716. cfd.numberInner = int.Parse(query.ToList()[0]["组内号"].ToString());
  717. DispatcherTool.ReleaseAgvOnStop(int.Parse(btn.Tag.ToString()),
  718. int.Parse(query.ToList()[0]["NG路线"].ToString()));
  719. lock (dicReleaseArea)
  720. {
  721. dicReleaseArea[number] = cfd;
  722. }
  723. lock (portLock)
  724. {
  725. DBWithByteValue[releaseAddress] = resultRead.Content;
  726. }
  727. }
  728. else
  729. {
  730. CFDClass cfd = dicReleaseArea[number];
  731. if (cfd.numberInner == numberInner && cfd.number == number)
  732. {
  733. DispatcherTool.ReleaseAgvOnStop(int.Parse(btn.Tag.ToString()),
  734. int.Parse(query.ToList()[0]["NG路线"].ToString()));
  735. lock (portLock)
  736. {
  737. DBWithByteValue[releaseAddress] = resultRead.Content;
  738. }
  739. }
  740. }
  741. }
  742. }
  743. //走NG放行路线
  744. }
  745. else
  746. {
  747. if (resultRead.Content != (byte)GlobalPara.DBControlValues.StopImmediate)
  748. {
  749. DBWithByteValue[releaseAddress] = resultRead.Content;
  750. }
  751. }
  752. }
  753. }
  754. }
  755. else
  756. {
  757. IEnumerable<AGVInfoModel> temp3 = temp.Where(o => o.RUNSTATUS_Cur == 0x41 && o.ConnectStatus == true && o.AGVNUM != 0);
  758. if (temp3.Count() > 0)
  759. {
  760. AGVInfoModel info = temp3.ToList()[0];
  761. btn.Invoke(new InvokeDelegate(delegate
  762. {
  763. if (btn.BackColor != Color.FromKnownColor(KnownColor.Red))
  764. {
  765. if (AGVIDWithPosByCheckPos.ContainsKey(info.AGVNUM))
  766. {
  767. AGVIDWithPosByCheckPos.Remove(info.AGVNUM);
  768. }
  769. if (AGVIDWithRouteByCheckPos.ContainsKey(info.AGVNUM))
  770. {
  771. AGVIDWithRouteByCheckPos.Remove(info.AGVNUM);
  772. }
  773. if (result != null && result.IsSuccess)
  774. {
  775. OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)info.AGVNUM);
  776. OperateResult rs2 = null;
  777. if (info.ALARMCODE != "5")
  778. {
  779. rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Pause);
  780. }
  781. else
  782. {
  783. rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Running);
  784. }
  785. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)(int.Parse(info.ALARMCODE) - 1));
  786. if (btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  787. {
  788. if (rs.IsSuccess && rs2.IsSuccess)
  789. {
  790. btn.BackColor = Color.Red;
  791. }
  792. }
  793. }
  794. }
  795. }));
  796. if (info.ALARMCODE == "1")
  797. {
  798. if (result != null && result.IsSuccess)
  799. {
  800. string releaseAddress = query.ToList()[0]["放行DB"].ToString();
  801. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  802. if (resultRead.IsSuccess && DBWithByteValue.Keys.Contains(releaseAddress) &&
  803. DBWithByteValue[releaseAddress] != resultRead.Content)
  804. {
  805. if (resultRead.Content == (byte)GlobalPara.DBControlValues.StopRelease)
  806. {
  807. lock (portLock)
  808. {
  809. DBWithByteValue[releaseAddress] = resultRead.Content;
  810. }
  811. DispatcherTool.ControlAGVBy1071(info.AGVNUM, 0);
  812. //放行被暂停的车
  813. WriteFile("急停记录", btn.Text + "工位被放行,已生成消息,当前放行地标:" + btn.Tag.ToString() + ",车号为:" + info.AGVNUM);
  814. }
  815. else if (resultRead.Content == (byte)GlobalPara.DBControlValues.OKRelease)
  816. {
  817. lock (portLock)
  818. {
  819. DBWithByteValue[releaseAddress] = resultRead.Content;
  820. }
  821. DispatcherTool.ReleaseAgvOnStop(int.Parse(btn.Tag.ToString()),
  822. int.Parse(query.ToList()[0]["放行路线"].ToString()));
  823. //放行被暂停的车
  824. WriteFile("充电点记录", btn.Text + "工位被放行,已生成消息,当前放行地标:" + btn.Tag.ToString() + ",车号为:" + info.AGVNUM);
  825. }
  826. else
  827. {
  828. DBWithByteValue[releaseAddress] = resultRead.Content;
  829. }
  830. }
  831. }
  832. }
  833. }
  834. else
  835. {
  836. IEnumerable<AGVInfoModel> temp4 = temp.Where(o => o.RUNSTATUS_Cur == 0x40 && o.ConnectStatus == true && o.AGVNUM != 0);
  837. if (temp4.Count() > 0)
  838. {
  839. AGVInfoModel info = temp4.ToList()[0];
  840. btn.Invoke(new InvokeDelegate(delegate
  841. {
  842. if (btn.BackColor != Color.FromKnownColor(KnownColor.Green))
  843. {
  844. if (AGVIDWithPosByCheckPos.ContainsKey(info.AGVNUM))
  845. {
  846. AGVIDWithPosByCheckPos.Remove(info.AGVNUM);
  847. }
  848. if (AGVIDWithRouteByCheckPos.ContainsKey(info.AGVNUM))
  849. {
  850. AGVIDWithRouteByCheckPos.Remove(info.AGVNUM);
  851. }
  852. // if (info.AGVNUM != 0)
  853. //{
  854. if (result != null && result.IsSuccess)
  855. {
  856. OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)info.AGVNUM);
  857. OperateResult rs2 = null;
  858. rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Running);
  859. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)(int.Parse(info.ALARMCODE) - 1));
  860. //}
  861. if (btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  862. {
  863. if (rs.IsSuccess && rs2.IsSuccess)
  864. {
  865. btn.BackColor = Color.Green;
  866. }
  867. }
  868. }
  869. }
  870. }));
  871. if (query.ToList()[0]["放行DB"].ToString() == "DB200.2")
  872. {
  873. // MessageBox.Show(info.AGVNUM.ToString() + ":" + info.RUNSTATUS_Cur.ToString());
  874. }
  875. if (info.RUNSTATUS_Cur == 0x40 && info.ALARMCODE == "1")
  876. {
  877. if (result != null && result.IsSuccess)
  878. {
  879. string releaseAddress = query.ToList()[0]["放行DB"].ToString();
  880. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  881. if (resultRead.IsSuccess && DBWithByteValue.Keys.Contains(releaseAddress) &&
  882. DBWithByteValue[releaseAddress] != resultRead.Content)
  883. {
  884. if (resultRead.Content == (byte)GlobalPara.DBControlValues.StopImmediate)
  885. {
  886. lock (portLock)
  887. {
  888. DBWithByteValue[releaseAddress] = resultRead.Content;
  889. }
  890. DispatcherTool.ControlAGVBy1071(info.AGVNUM, 1);
  891. //立刻暂停行驶中的车
  892. WriteFile("急停记录", btn.Text + "工位被暂停,已生成消息,当前急停地标:" + btn.Tag.ToString() + ",车号为:" + info.AGVNUM);
  893. }
  894. else
  895. {
  896. DBWithByteValue[releaseAddress] = resultRead.Content;
  897. }
  898. }
  899. }
  900. }
  901. }
  902. else
  903. {
  904. btn.Invoke(new InvokeDelegate(delegate
  905. {
  906. if (tag == 1)
  907. {
  908. if (btn.BackColor != Color.FromKnownColor(KnownColor.Control))
  909. {
  910. OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)0);
  911. OperateResult rs2 =
  912. GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Unknow);
  913. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)0);
  914. if (btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  915. {
  916. if (rs.IsSuccess && rs2.IsSuccess)
  917. {
  918. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  919. }
  920. }
  921. }
  922. }
  923. else if (tag == 0)
  924. {
  925. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  926. if (result != null && result.IsSuccess)
  927. {
  928. GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)0);
  929. GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Unknow);
  930. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)0);
  931. }
  932. }
  933. }));
  934. if (result != null && result.IsSuccess)
  935. {
  936. string releaseAddress = query.ToList()[0]["放行DB"].ToString();
  937. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  938. if (resultRead.IsSuccess && DBWithByteValue.Keys.Contains(releaseAddress) &&
  939. DBWithByteValue[releaseAddress] != resultRead.Content)
  940. {
  941. {
  942. if (resultRead.Content != (byte)GlobalPara.DBControlValues.StopImmediate)
  943. {
  944. DBWithByteValue[releaseAddress] = resultRead.Content;
  945. }
  946. }
  947. }
  948. }
  949. }
  950. }
  951. }
  952. }
  953. else
  954. {
  955. btn.Invoke(new InvokeDelegate(delegate
  956. {
  957. if (tag == 1)
  958. {
  959. if (btn.BackColor != Color.FromKnownColor(KnownColor.Control) &&
  960. btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  961. {
  962. OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)0);
  963. OperateResult rs2 =
  964. GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Unknow);
  965. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)0);
  966. if (rs.IsSuccess && rs2.IsSuccess)
  967. {
  968. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  969. }
  970. }
  971. }
  972. else if (tag == 0)
  973. {
  974. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  975. if (result != null && result.IsSuccess)
  976. {
  977. GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)0);
  978. GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Unknow);
  979. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)0);
  980. }
  981. }
  982. }));
  983. if (result != null && result.IsSuccess)
  984. {
  985. string releaseAddress = query.ToList()[0]["放行DB"].ToString();
  986. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  987. if (resultRead.IsSuccess && DBWithByteValue.Keys.Contains(releaseAddress) &&
  988. DBWithByteValue[releaseAddress] != resultRead.Content)
  989. {
  990. if (resultRead.Content != (byte)GlobalPara.DBControlValues.StopImmediate)
  991. {
  992. DBWithByteValue[releaseAddress] = resultRead.Content;
  993. }
  994. }
  995. }
  996. }
  997. }
  998. }
  999. });
  1000. if (result2.IsCompleted)
  1001. {
  1002. sw2.Stop();
  1003. Console.WriteLine(dt.TableName + "任务总共花费{0}s.", sw.Elapsed.TotalSeconds);
  1004. object[] obj = new object[3];
  1005. obj[0] = listBtn; obj[1] = ++tag; obj[2] = lineName;
  1006. if (tag == 5)
  1007. {
  1008. if (!InitOK.ContainsKey(lineName))
  1009. {
  1010. InitOK.Add(lineName, true);
  1011. }
  1012. }
  1013. if (tag > 10)
  1014. {
  1015. tag = 11;
  1016. }
  1017. new Thread(new ParameterizedThreadStart(DockingOpeare0)).Start(obj);
  1018. // DockingOpeare(list);
  1019. }
  1020. }
  1021. }
  1022. catch (Exception ex)
  1023. {
  1024. DateTime time = DateTime.Now;
  1025. //MessageBox.Show("线程异常:"+e.ExceptionObject);
  1026. FileStream stream = File.Open(@"Exception\调度程序异常.txt", FileMode.Append, FileAccess.Write);
  1027. StreamWriter writer = new StreamWriter(stream);
  1028. if (ex != null)
  1029. {
  1030. string msg = string.Format("[{0}]Test:\r\nMessage:{1}\r\nStackTrace:\r\n{2}", time.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message, ex.StackTrace);
  1031. writer.WriteLine(msg);
  1032. writer.Close();
  1033. stream.Close();
  1034. }
  1035. object[] obj = new object[3];
  1036. obj[0] = listBtn; obj[1] = 0; obj[2] = lineName;
  1037. new Thread(new ParameterizedThreadStart(DockingOpeare0)).Start(obj);
  1038. }
  1039. }
  1040. void WriteFile(string fileName, string context)
  1041. {
  1042. DateTime time = DateTime.Now;
  1043. //MessageBox.Show("线程异常:"+e.ExceptionObject);
  1044. FileStream stream = File.Open(@"Exception\" + fileName + ".txt", FileMode.Append, FileAccess.Write);
  1045. StreamWriter writer = new StreamWriter(stream);
  1046. {
  1047. string msg = string.Format("[{0}]Test:\r\nMessage:{1}\r\n", time.ToString("yyyy-MM-dd HH:mm:ss"), context);
  1048. writer.WriteLine(msg);
  1049. writer.Close();
  1050. stream.Close();
  1051. }
  1052. }
  1053. void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
  1054. {
  1055. Graphics g = e.Graphics;
  1056. Font font = new Font("微软雅黑", 16f, System.Drawing.GraphicsUnit.Pixel);
  1057. SolidBrush brush = new SolidBrush(Color.Black);
  1058. if (e.Index == this.tabControl1.SelectedIndex)
  1059. {
  1060. brush = new SolidBrush(Color.Blue);
  1061. }
  1062. RectangleF rectangle = (RectangleF)(tabControl1.GetTabRect(e.Index));
  1063. RectangleF rectangle2 = new RectangleF(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
  1064. g.FillRectangle(new SolidBrush(SystemColors.ButtonHighlight), rectangle2);
  1065. StringFormat sformat = new StringFormat();
  1066. sformat.LineAlignment = StringAlignment.Center;
  1067. sformat.Alignment = StringAlignment.Center;
  1068. g.DrawString(((TabControl)sender).TabPages[e.Index].Text, font, brush, rectangle2, sformat);
  1069. }
  1070. /// <summary>
  1071. /// 发送心跳包
  1072. /// </summary>
  1073. private void HeartBit()
  1074. {
  1075. while (true)
  1076. {
  1077. //更新心跳包
  1078. if (result == null || !result.IsSuccess)
  1079. {
  1080. if (GlobalPara.s7 != null)
  1081. {
  1082. result = GlobalPara.s7.ConnectServer();
  1083. }
  1084. statusStrip1.Invoke(new InvokeDelegate(delegate
  1085. {
  1086. toolStripStatusLabel1.Text = GlobalPara.s7.IpAddress + ", " + "PLC连接失败";
  1087. }));
  1088. }
  1089. else
  1090. {
  1091. var tempResult = GlobalPara.s7.Write(dtConfig.Rows[1][1].ToString(), (byte)HeartBitCount);
  1092. if (tempResult.IsSuccess)
  1093. {
  1094. HeartBitCount++;
  1095. if (HeartBitCount > 0xFF)
  1096. {
  1097. HeartBitCount = 1;
  1098. }
  1099. statusStrip1.Invoke(new InvokeDelegate(delegate
  1100. {
  1101. toolStripStatusLabel1.Text = GlobalPara.s7.IpAddress + "," + " " + "PLC连接成功";
  1102. }
  1103. ));
  1104. }
  1105. else
  1106. {
  1107. this.Invoke(new InvokeDelegate(delegate
  1108. {
  1109. toolStripStatusLabel1.Text = GlobalPara.s7.IpAddress + "," + " " + "PLC连接失败";
  1110. }
  1111. ));
  1112. }
  1113. }
  1114. }
  1115. }
  1116. /// <summary>
  1117. /// 处理调度的协议
  1118. /// </summary>
  1119. private void Analzye()
  1120. {
  1121. while (true)
  1122. {
  1123. if (GlobalPara.dispatchList.Count > 0)
  1124. {
  1125. byte[] bytes = GlobalPara.dispatchList[0];
  1126. Console.WriteLine("调度模块收到数据:" + GlobalPara.ByteToHexString(bytes));
  1127. GlobalPara.dispatchList.Remove(bytes);
  1128. }
  1129. Thread.Sleep(20);
  1130. }
  1131. }
  1132. }
  1133. }