DispatchSystem - 副本 (2).cs 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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<string, CFDClass> dicReleaseArea = new Dictionary<string, CFDClass>();
  34. public int CurCFDRoute = -1;
  35. Dictionary<string, bool> InitOK = new Dictionary<string, bool>();
  36. List<int> CFDRoutes = new List<int>();
  37. int index = 0;
  38. int CFDWaitTimes = 15;
  39. int CFDCheckTimes = 0;
  40. Dictionary<string, List<string>> dicCheck = new Dictionary<string, List<string>>();
  41. DataTable DtTraffic = new DataTable();
  42. List<string> listTra = new List<string>();
  43. byte PLCHeartBitCount = 0;
  44. bool HeartBitFlag = true;
  45. OperateResult<byte> PLCResult;
  46. bool IsPLCChangeOff = false;
  47. DataTable LineDt = new DataTable();
  48. bool DockingOK = false;
  49. public DispatchSystem()
  50. {
  51. InitializeComponent();
  52. }
  53. private void DispatchSystem_Load(object sender, EventArgs e)
  54. {
  55. LoadConfig();
  56. this.FormClosing += new FormClosingEventHandler(DispatchSystem_FormClosing);
  57. Thread thread2 = new Thread(HeartBit);
  58. thread2.Start();
  59. Thread thread = new Thread(Analzye);
  60. thread.Start();
  61. }
  62. void DispatchSystem_FormClosing(object sender, FormClosingEventArgs e)
  63. {
  64. e.Cancel = true;
  65. }
  66. /// <summary>
  67. /// 加载配置
  68. /// </summary>
  69. public void LoadConfig()
  70. {
  71. sw.Restart();
  72. tabControl1.TabPages.Clear();
  73. 物料设置ToolStripMenuItem.DropDownItems.Clear();
  74. checkPosDic.Clear();
  75. AGVIDWithPosByCheckPos.Clear();
  76. DBWithByteValue.Clear();
  77. HeartBitCount = 1;
  78. XmlSerializer deserializer1 = new XmlSerializer(GlobalPara.gs_configInfo.GetType());
  79. try
  80. {
  81. using (TextReader tr = new StreamReader("CConfig.xml"))
  82. {
  83. GlobalPara.gs_configInfo = (AGV_WPF.Tools.CConfigData)deserializer1.Deserialize(tr);
  84. }
  85. }
  86. catch (Exception ex)
  87. { }
  88. string currPath = System.AppDomain.CurrentDomain.BaseDirectory;
  89. string dataPath = currPath + @"data";
  90. string dataFile = dataPath + @"\基础配置表.xls";
  91. try
  92. {
  93. string confStr1 = @"Provider=Microsoft.ACE.OLEDB.12.0;";
  94. string confStr2 = @"Persist Security Info=false;";
  95. string confStr3 = @"Data Source=" + dataFile + ";";
  96. string confStr4 = @"Extended Properties=" + "'Excel 12.0;HDR=YES;IMEX=1'";
  97. string connStr = confStr1 + confStr2 + confStr3 + confStr4;
  98. OleDbConnection oleDC = new OleDbConnection(connStr);
  99. OleDbDataAdapter oleDDA1 = new OleDbDataAdapter("Select * From [参数配置$]", oleDC);
  100. oleDDA1.Fill(dtConfig);
  101. OleDbDataAdapter oleDDA2 = new OleDbDataAdapter("Select * From [物料类型$]", oleDC);
  102. DataTable temp = new DataTable();
  103. oleDDA2.Fill(temp);
  104. if (temp.Rows.Count > 0)
  105. {
  106. for (int i = 0; i < temp.Rows.Count; i++)
  107. {
  108. ToolStripMenuItem tsm = new ToolStripMenuItem(temp.Rows[i][1].ToString());
  109. 物料设置ToolStripMenuItem.DropDownItems.Add(tsm);
  110. tsm.Font = new Font("黑体", 15);
  111. if (GlobalPara.gs_configInfo.CurrentMatName == temp.Rows[i][1].ToString())
  112. {
  113. tsm.CheckState = CheckState.Checked;
  114. }
  115. tsm.Click += new EventHandler(tsm_Click);
  116. }
  117. }
  118. tabControl1.DrawMode = TabDrawMode.Normal;
  119. tabControl1.SizeMode = TabSizeMode.Fixed;
  120. tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
  121. GlobalPara.s7 = new HslCommunication.Profinet.Siemens.SiemensS7Net(HslCommunication.Profinet.Siemens.SiemensPLCS.S1500,
  122. dtConfig.Rows[0][1].ToString());
  123. GlobalPara.s7.Rack = (byte)Convert.ToInt16(dtConfig.Rows[2][1].ToString());
  124. GlobalPara.s7.Slot = (byte)Convert.ToInt16(dtConfig.Rows[3][1].ToString());
  125. GlobalPara.s7.ConnectTimeOut = 10;
  126. GlobalPara.s7.ReceiveTimeOut = 10;
  127. result = GlobalPara.s7.ConnectServer();
  128. sw.Stop();
  129. Console.WriteLine("配置初始化总共花费{0}s.", sw.Elapsed.TotalSeconds);
  130. sw.Restart();
  131. InitView();
  132. sw.Stop();
  133. Console.WriteLine("界面初始化总共花费{0}s.", sw.Elapsed.TotalSeconds);
  134. }
  135. catch (System.Exception ex)
  136. {
  137. MessageBox.Show(ex.StackTrace);
  138. }
  139. }
  140. void tsm_Click(object sender, EventArgs e)
  141. {
  142. for (int i = 0; i < 物料设置ToolStripMenuItem.DropDownItems.Count; i++)
  143. {
  144. if (((ToolStripMenuItem)物料设置ToolStripMenuItem.DropDownItems[i]).Text == ((ToolStripMenuItem)sender).Text
  145. && ((ToolStripMenuItem)sender).CheckState == CheckState.Checked)
  146. {
  147. return;
  148. }
  149. ((ToolStripMenuItem)物料设置ToolStripMenuItem.DropDownItems[i]).CheckState = CheckState.Unchecked;
  150. }
  151. ((ToolStripMenuItem)sender).Checked = true;
  152. GlobalPara.gs_configInfo.CurrentMatName = ((ToolStripMenuItem)sender).Text;
  153. XmlSerializer serializer2 = new XmlSerializer(GlobalPara.gs_configInfo.GetType());
  154. using (TextWriter tw = new StreamWriter("CConfig.xml"))
  155. {
  156. serializer2.Serialize(tw, GlobalPara.gs_configInfo);
  157. }
  158. this.DispatchSystem_Load(null, null);
  159. }
  160. void tsm_CheckedChanged(object sender, EventArgs e)
  161. {
  162. //throw new NotImplementedException();
  163. }
  164. void tsm_CheckStateChanged(object sender, EventArgs e)
  165. {
  166. //throw new NotImplementedException();
  167. }
  168. /// <summary>
  169. /// 界面初始化
  170. /// </summary>
  171. /// <param name="temp"></param>
  172. public void InitView()
  173. {
  174. string currPath = System.AppDomain.CurrentDomain.BaseDirectory;
  175. string dataPath = currPath + @"data\";
  176. string dataFile = dataPath + GlobalPara.gs_configInfo.CurrentMatName + ".xls";
  177. if (false == File.Exists(dataFile))
  178. {
  179. DirectoryInfo di = new DirectoryInfo(dataPath);
  180. foreach (FileInfo f in di.GetFiles())
  181. {
  182. if (f.Name != "基础配置表" && f.Extension.Contains(".xls"))
  183. {
  184. GlobalPara.gs_configInfo.CurrentMatName = f.Name.Replace(".xls", "");
  185. for (int i = 0; i < 物料设置ToolStripMenuItem.DropDownItems.Count; i++)
  186. {
  187. if (物料设置ToolStripMenuItem.DropDownItems[i].Text == GlobalPara.gs_configInfo.CurrentMatName)
  188. {
  189. ((ToolStripMenuItem)物料设置ToolStripMenuItem.DropDownItems[i]).CheckState = CheckState.Checked;
  190. }
  191. }
  192. dataFile = f.FullName;
  193. break;
  194. }
  195. }
  196. }
  197. try
  198. {
  199. string confStr1 = @"Provider=Microsoft.ACE.OLEDB.12.0;";
  200. string confStr2 = @"Persist Security Info=false;";
  201. string confStr3 = @"Data Source=" + dataFile + ";";
  202. string confStr4 = @"Extended Properties=" + "'Excel 12.0;HDR=YES;IMEX=1'";
  203. string connStr = confStr1 + confStr2 + confStr3 + confStr4;
  204. OleDbConnection oleDC = new OleDbConnection(connStr);
  205. OleDbDataAdapter oleDDA2 = new OleDbDataAdapter("Select * From [线体信息$]", oleDC);
  206. oleDDA2.Fill(LineDt);
  207. Dictionary<Thread, object[]> listThread = new Dictionary<Thread, object[]>();
  208. for (int i = 0; i < LineDt.Rows.Count; i++)
  209. {
  210. string lineName = LineDt.Rows[i]["线体名称"].ToString();
  211. TabPage tb = new TabPage(lineName);
  212. tb.Font = new System.Drawing.Font("楷体", tb.Font.Size + 5);
  213. oleDDA2 = new OleDbDataAdapter("Select * From [" + lineName + "$]", oleDC);
  214. DataTable temp2 = new DataTable();
  215. temp2.TableName = lineName;
  216. oleDDA2.Fill(temp2);
  217. if (LineDt.Rows[i]["判断点"] != null && !string.IsNullOrEmpty(LineDt.Rows[i]["判断点"].ToString()) && LineDt.Rows[i]["判断点"].ToString() != "0")
  218. {
  219. if (!checkPosDic.ContainsKey(int.Parse(LineDt.Rows[i]["判断点"].ToString())))
  220. {
  221. checkPosDic.Add(int.Parse(LineDt.Rows[i]["判断点"].ToString()), lineName);
  222. }
  223. }
  224. int X = 10, Y = 10;
  225. List<Button> listBtn = new List<Button>();
  226. if (lineName == "充放电测试")
  227. {
  228. DataView dv = temp2.DefaultView;
  229. dv.Sort = " 优先级 asc";
  230. temp2 = dv.ToTable();
  231. }
  232. for (int j = 1; j <= temp2.Rows.Count; j++)
  233. {
  234. //if (temp2.Columns.Contains("组号") && !dicReleaseArea.ContainsKey(int.Parse(temp2.Rows[j - 1]["组号"].ToString())))
  235. //{
  236. // dicReleaseArea.Add(int.Parse(temp2.Rows[j - 1]["组号"].ToString()), null);
  237. //}
  238. if (temp2.Rows[j - 1]["放行DB"] != null && !string.IsNullOrEmpty(temp2.Rows[j - 1]["放行DB"].ToString()) && temp2.Rows[j - 1]["放行DB"].ToString() != "0")
  239. {
  240. if (!DBWithByteValue.ContainsKey((temp2.Rows[j - 1]["放行DB"].ToString())))
  241. {
  242. DBWithByteValue.Add((temp2.Rows[j - 1]["放行DB"].ToString()), (byte)255);
  243. }
  244. }
  245. if (temp2.Columns.Contains("判断地标"))
  246. {
  247. if (temp2.Rows[j - 1]["判断地标"] != null && !string.IsNullOrEmpty(temp2.Rows[j - 1]["判断地标"].ToString()))
  248. {
  249. if (!dicCheck.ContainsKey(temp2.Rows[j - 1]["工位地标"].ToString()))
  250. {
  251. dicCheck.Add(temp2.Rows[j - 1]["工位地标"].ToString(), temp2.Rows[j - 1]["判断地标"].ToString().Split(',').ToList());
  252. }
  253. }
  254. }
  255. Button btn = new Button();
  256. btn.Text = temp2.Rows[j - 1][1].ToString();
  257. btn.Location = new Point(X, Y);
  258. btn.Font = new Font("宋体", 15);
  259. btn.Size = new Size(100, 80);
  260. btn.Click
  261. += new EventHandler(btn_Click);
  262. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  263. X += btn.Size.Width + 20;
  264. if (j % 6 == 0)
  265. {
  266. X = 10; Y += btn.Height + 20;
  267. }
  268. btn.Tag = temp2.Rows[j - 1][2].ToString();
  269. tb.Controls.Add(btn);
  270. listBtn.Add(btn);
  271. if (lineName == "充放电测试")
  272. {
  273. if (!CFDRoutes.Contains(int.Parse(temp2.Rows[j - 1]["放行路线"].ToString())))
  274. {
  275. CFDRoutes.Add(int.Parse(temp2.Rows[j - 1]["放行路线"].ToString()));
  276. }
  277. }
  278. }
  279. Tools.TabPageTag tt = new Tools.TabPageTag();
  280. tt.Table = temp2.Copy();
  281. tt.ListBtn = new List<Button>();
  282. tt.ListBtn.AddRange(listBtn);
  283. tb.Tag = tt;
  284. tabControl1.TabPages.Add(tb);
  285. object[] obj = new object[3];
  286. obj[0] = listBtn; obj[1] = 0; obj[2] = lineName;
  287. if (lineName == "充放电测试")
  288. {
  289. CurCFDRoute = CFDRoutes[index];
  290. }
  291. listThread.Add(new Thread(new ParameterizedThreadStart(DockingOpeare0)), obj);//.Start(listBtn);
  292. // DockingOpeare0(obj);
  293. // if (lineName == "充放电测试") { tabControl1.SelectedTab = tb; }
  294. }
  295. OleDbDataAdapter oleDDA3 = new OleDbDataAdapter("Select * From [管制信息$]", oleDC);
  296. oleDDA3.Fill(DtTraffic);
  297. if (File.Exists(@"Exception\管制点放行信息.txt"))
  298. {
  299. string[] texts = File.ReadAllLines(@"Exception\管制点放行信息.txt");
  300. listTra.AddRange(texts.ToList());
  301. if (listTra.Count < DtTraffic.Rows.Count)
  302. {
  303. for (int i = 0; i < DtTraffic.Rows.Count - listTra.Count; i++)
  304. {
  305. listTra.Add("0");
  306. }
  307. }
  308. }
  309. else
  310. {
  311. if (DtTraffic.Rows.Count > 0)
  312. {
  313. listTra = new List<string>(DtTraffic.Rows.Count);
  314. for (int i = 0; i < DtTraffic.Rows.Count; i++)
  315. {
  316. listTra.Add("0");
  317. }
  318. }
  319. }
  320. foreach (Thread t in listThread.Keys)
  321. {
  322. t.Priority = ThreadPriority.Highest;
  323. t.Start(listThread[t]);
  324. }
  325. // Thread.Sleep(8000);
  326. //Thread t2 = new Thread(new ThreadStart(checkingOperate));
  327. //t2.Priority = ThreadPriority.BelowNormal;
  328. //t2.Start();
  329. // new Thread(new ThreadStart(CFDCheck)).Start();
  330. // new Thread(new ThreadStart(checkingRelease2)).Start();
  331. //Thread t3 = new Thread(new ThreadStart(TrafficByDispatch));
  332. //t3.Priority = ThreadPriority.AboveNormal;
  333. //t3.Start();
  334. Thread t4 = new Thread(new ThreadStart(PLCHeartBit));
  335. t4.Priority = ThreadPriority.Lowest;
  336. t4.Start();
  337. }
  338. catch (System.Exception ex)
  339. {
  340. MessageBox.Show(ex.StackTrace);
  341. }
  342. }
  343. void btn_Click(object sender, EventArgs e)
  344. {
  345. //throw new NotImplementedException();
  346. Button btn = (Button)sender;
  347. if (btn.BackColor == Color.Blue)
  348. {
  349. if (MessageBox.Show("该工位已被分配锁定,是否解除锁定?", "警告", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
  350. {
  351. btn.Invoke(new InvokeDelegate(delegate
  352. {
  353. if (AGVIDWithPosByCheckPos.ContainsValue(int.Parse(btn.Tag.ToString())))
  354. {
  355. int num = AGVIDWithPosByCheckPos.Keys.ToList()[AGVIDWithPosByCheckPos.Values.ToList().IndexOf(int.Parse(btn.Tag.ToString()))];
  356. if (AGVIDWithPosByCheckPos.ContainsKey(num))
  357. {
  358. lock (AGVIDWithPosByCheckPos)
  359. {
  360. AGVIDWithPosByCheckPos.Remove(num);
  361. }
  362. }
  363. if (AGVIDWithRouteByCheckPos.ContainsKey(num))
  364. {
  365. lock (AGVIDWithRouteByCheckPos)
  366. {
  367. AGVIDWithRouteByCheckPos.Remove(num);
  368. }
  369. }
  370. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  371. }
  372. else
  373. {
  374. int num = int.Parse(btn.Tag.ToString());
  375. if (AGVIDWithPosByCheckPos.Values.Contains(num))
  376. {
  377. lock (AGVIDWithPosByCheckPos)
  378. {
  379. var temp = AGVIDWithPosByCheckPos.First(o => o.Value == num);
  380. if (AGVIDWithPosByCheckPos.ContainsKey(temp.Key))
  381. {
  382. AGVIDWithPosByCheckPos.Remove(temp.Key);
  383. }
  384. }
  385. }
  386. if (AGVIDWithRouteByCheckPos.Values.Contains(num))
  387. {
  388. lock (AGVIDWithRouteByCheckPos)
  389. {
  390. var temp = AGVIDWithRouteByCheckPos.First(o => o.Value == num);
  391. if (AGVIDWithRouteByCheckPos.ContainsKey(temp.Key))
  392. {
  393. AGVIDWithRouteByCheckPos.Remove(temp.Key);
  394. }
  395. }
  396. }
  397. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  398. }
  399. }
  400. ));
  401. }
  402. else if (btn.BackColor == Color.Red)
  403. {
  404. if (MessageBox.Show("该工位AGV已故障,是否解除状态,并将AGV移到故障?", "警告", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
  405. {
  406. List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
  407. IEnumerable<AGVInfoModel> temp = listModel.Where(o => o.MARKNUM.ToString() == btn.Tag.ToString());
  408. if (temp.Count() > 0)
  409. {
  410. AGVInfoModel info = temp.ToList()[0];
  411. info.MARKNUM = 0;
  412. GlobalPara.AgvInfoDic[info.AGVNUM] = info;
  413. }
  414. btn.Invoke(new InvokeDelegate(delegate
  415. {
  416. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  417. }));
  418. }
  419. }
  420. }
  421. }
  422. /// <summary>
  423. /// 记录放行后的情况
  424. /// </summary>
  425. public void recordReleaseInfo()
  426. {
  427. if (AGVIDWithPosByCheckPos.Count > 0)
  428. {
  429. string[] textArray = new string[AGVIDWithPosByCheckPos.Count];
  430. int aryIndex = 0;
  431. foreach (int num in AGVIDWithPosByCheckPos.Keys)
  432. {
  433. if (AGVIDWithPosByCheckPos.ContainsKey(num))
  434. {
  435. textArray[aryIndex] = num + "," + AGVIDWithPosByCheckPos[num];
  436. }
  437. if (AGVIDWithRouteByCheckPos.ContainsKey(num))
  438. {
  439. textArray[aryIndex] += "," + AGVIDWithRouteByCheckPos[num];
  440. }
  441. ++aryIndex;
  442. }
  443. File.WriteAllLines(@"Exception\判断点放行信息.txt", textArray);
  444. }
  445. }
  446. /// <summary>
  447. /// 调度管制
  448. /// </summary>
  449. public void TrafficByDispatch()
  450. {
  451. if (DtTraffic.Rows.Count > 0)
  452. {
  453. bool b = true;
  454. while (b)
  455. {
  456. try
  457. {
  458. List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
  459. for (int i = 0; i < DtTraffic.Rows.Count; i++)
  460. {
  461. List<string> listIn = DtTraffic.Rows[i][0].ToString().Split(',').ToList();
  462. List<string> listTraffic = DtTraffic.Rows[i][1].ToString().Split(',').ToList();
  463. //List<AGVInfoModel> results = listModel.FindAll(o => listTra[i].Equals(o.MARKNUM.ToString()) && o.ConnectStatus == true);
  464. //if (results != null && results.Count > 0)
  465. //{
  466. //}
  467. //else
  468. // {
  469. List<AGVInfoModel> results = listModel.FindAll(o => listIn.Contains(o.MARKNUM.ToString()) && o.ConnectStatus == true);
  470. // }
  471. if (results != null && results.Count > 0)
  472. {
  473. AGVInfoModel result = results.OrderBy(o => o.LastModifyMarkTime).ToList()[0];
  474. List<AGVInfoModel> results2 = listModel.FindAll(o => listTraffic.Contains(o.MARKNUM.ToString()) && o.ConnectStatus == true);
  475. if (listIn.Count == 1 && results.Count > 1)
  476. {
  477. continue;
  478. }
  479. if (results == null || results2.Count <= 0)
  480. {
  481. List<AGVInfoModel> results3 = listModel.FindAll(o => result.MARKNUM == o.MARKNUM && o.RUNSTATUS_Cur != 0x43 && o.ConnectStatus == true);
  482. if (results3 != null && results3.Count <= 0)
  483. {
  484. int route = 0; int targetMark = 0;
  485. if (AGVIDWithPosByCheckPos.Keys.Contains(result.AGVNUM))
  486. {
  487. targetMark = AGVIDWithPosByCheckPos[result.AGVNUM];
  488. }
  489. if (AGVIDWithRouteByCheckPos.Keys.Contains(result.AGVNUM))
  490. {
  491. route = AGVIDWithRouteByCheckPos[result.AGVNUM];
  492. }
  493. DispatcherTool.ReleaseAgvOnStop(result.MARKNUM, route, targetMark, 1);
  494. if (listTra[i] != result.MARKNUM.ToString())
  495. {
  496. listTra[i] = result.MARKNUM.ToString();
  497. File.WriteAllLines(@"Exception\管制点放行信息.txt", listTra.ToArray());
  498. }
  499. WriteFile("管制记录" + DateTime.Now.ToString("yyyy-MM-dd"), result.AGVNUM + "号AGV符合放行条件,地标为:" + result.MARKNUM + ",路线为" + route + ",目的地标为" + targetMark);
  500. }
  501. }
  502. }
  503. }
  504. }
  505. catch (Exception)
  506. {
  507. b = false;
  508. //Thread.Sleep(200);
  509. //new Thread(new ThreadStart(TrafficByDispatch)).Start();
  510. }
  511. finally
  512. {
  513. if (!b)
  514. {
  515. Thread.Sleep(500);
  516. Thread t2 = new Thread(new ThreadStart(TrafficByDispatch));
  517. t2.Priority = ThreadPriority.AboveNormal;
  518. t2.Start();
  519. WriteFile("线程重启", "TrafficByDispatch报错,已重启!");
  520. }
  521. }
  522. Thread.Sleep(500);
  523. }
  524. }
  525. }
  526. /// <summary>
  527. /// 收到PLC放行信号继续发送放行
  528. /// </summary>
  529. public void checkingRelease()
  530. {
  531. bool b = true;
  532. while (b)
  533. {
  534. try
  535. {
  536. if (!GlobalPara.IsOneKeySleep)
  537. {
  538. if (result != null && result.IsSuccess)
  539. {
  540. for (int j = 0; j < tabControl1.TabPages.Count; j++)
  541. {
  542. if (tabControl1.TabPages[j].Tag != null)
  543. {
  544. TabPageTag tt = (TabPageTag)tabControl1.TabPages[j].Tag;
  545. for (int k = 0; k < tt.Table.Rows.Count; k++)
  546. {
  547. DataRow dr = tt.Table.Rows[k];
  548. string releaseAddress = dr["放行DB"].ToString();
  549. if (!string.IsNullOrEmpty(releaseAddress))
  550. {
  551. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  552. if (resultRead != null && result.IsSuccess)
  553. {
  554. if (resultRead.Content == (byte)GlobalPara.DBControlValues.OKRelease)
  555. {
  556. DispatcherTool.ReleaseAgvOnStop(int.Parse(dr["工位地标"].ToString()), 0, int.Parse(dr["目的地标"].ToString()));
  557. }
  558. else if (resultRead.Content == (byte)GlobalPara.DBControlValues.NGRelease)
  559. {
  560. DispatcherTool.ReleaseAgvOnStop(int.Parse(dr["工位地标"].ToString()), int.Parse(dr["NG路线"].ToString()));
  561. }
  562. }
  563. }
  564. Thread.Sleep(10);
  565. }
  566. }
  567. }
  568. }
  569. }
  570. }
  571. catch (Exception)
  572. {
  573. b = false;
  574. //Thread.Sleep(500);
  575. //new Thread(new ThreadStart(checkingRelease)).Start();
  576. }
  577. finally
  578. {
  579. if (!b)
  580. {
  581. Thread.Sleep(500);
  582. Thread t2 = new Thread(new ThreadStart(checkingRelease));
  583. t2.Priority = ThreadPriority.BelowNormal;
  584. t2.Start();
  585. WriteFile("线程重启", "checkingRelease报错,已重启!");
  586. }
  587. }
  588. Thread.Sleep(500);
  589. }
  590. }
  591. /// <summary>
  592. /// 收到PLC放行信号继续发送放行
  593. /// </summary>
  594. public void checkingRelease2()
  595. {
  596. bool b = true;
  597. while (b)
  598. {
  599. try
  600. {
  601. if (result != null && result.IsSuccess)
  602. {
  603. List<string> keys = DBWithByteValue.Keys.ToList();
  604. for (int i = 0; i < keys.Count; i++)
  605. {
  606. if (DBWithByteValue[keys[i]] == (byte)GlobalPara.DBControlValues.OKRelease ||
  607. DBWithByteValue[keys[i]] == (byte)GlobalPara.DBControlValues.NGRelease)
  608. {
  609. for (int j = 0; j < tabControl1.TabPages.Count; j++)
  610. {
  611. if (tabControl1.TabPages[j].Tag != null)
  612. {
  613. TabPageTag tt = (TabPageTag)tabControl1.TabPages[j].Tag;
  614. DataRow[] drs = tt.Table.Select("放行DB='" + keys[i] + "'");
  615. if (drs != null && drs.Length > 0)
  616. {
  617. if (DBWithByteValue[keys[i]] == (byte)GlobalPara.DBControlValues.OKRelease)
  618. {
  619. DispatcherTool.ReleaseAgvOnStop(int.Parse(drs[0]["工位地标"].ToString()), 0, int.Parse(drs[0]["目的地标"].ToString()));
  620. break;
  621. }
  622. else if (DBWithByteValue[keys[i]] == (byte)GlobalPara.DBControlValues.NGRelease)
  623. {
  624. DispatcherTool.ReleaseAgvOnStop(int.Parse(drs[0]["工位地标"].ToString()), int.Parse(drs[0]["NG路线"].ToString()));
  625. break;
  626. }
  627. }
  628. }
  629. }
  630. }
  631. }
  632. }
  633. }
  634. catch (Exception)
  635. {
  636. b = false;
  637. //Thread.Sleep(500);
  638. //new Thread(new ThreadStart(checkingRelease2)).Start();
  639. }
  640. finally
  641. {
  642. if (!b)
  643. {
  644. Thread.Sleep(500);
  645. new Thread(new ThreadStart(checkingRelease2)).Start();
  646. WriteFile("线程重启", "checkingRelease报错,已重启!");
  647. }
  648. }
  649. Thread.Sleep(500);
  650. }
  651. }
  652. /// <summary>
  653. /// 判断点事件
  654. /// </summary>
  655. public void checkingOperate()
  656. {
  657. bool bb = false;
  658. Stopwatch sw2 = new Stopwatch();
  659. List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
  660. ParallelLoopResult result;
  661. try
  662. {
  663. result = Parallel.ForEach<int>(checkPosDic.Keys, new ParallelOptions { MaxDegreeOfParallelism = 2 }, pos =>
  664. {
  665. if (!GlobalPara.IsOneKeySleep)
  666. {
  667. IEnumerable<AGVInfoModel> temp = listModel.Where(o => o.MARKNUM == pos && (o.RUNSTATUS_Cur == 0x43 || o.RUNSTATUS_Cur == 0x47 || o.ALARMCODE == "9") && !AGVIDWithPosByCheckPos.ContainsKey(o.AGVNUM) && o.ConnectStatus == true);
  668. if (temp.Count() > 0)
  669. {
  670. int num = temp.ToList()[0].AGVNUM;
  671. if (!AGVIDWithPosByCheckPos.ContainsKey(num))
  672. {
  673. string lineName = checkPosDic[pos];
  674. for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
  675. {
  676. if (this.tabControl1.TabPages[i].Text == lineName && InitOK.ContainsKey(lineName) && InitOK[lineName])
  677. {
  678. TabPageTag tag = this.tabControl1.TabPages[i].Tag as TabPageTag;
  679. DataRow[] drs = null;
  680. {
  681. drs = tag.Table.Select("优先级>0", "优先级 asc");
  682. }
  683. for (int j = 0; j < drs.Length; j++)
  684. {
  685. // IEnumerable<DataRow> query =
  686. //from data in tag.Table.AsEnumerable()
  687. //group data by data["路线"] into g
  688. //select g.First();
  689. Button btn = tag.ListBtn.Find(o => o.Tag.ToString() == drs[j]["工位地标"].ToString() && !AGVIDWithPosByCheckPos.ContainsValue(int.Parse(o.Tag.ToString()))
  690. && (o.BackColor == Color.FromKnownColor(KnownColor.Control) || o.BackColor == Color.FromKnownColor(KnownColor.Green)
  691. ) && o.BackColor != Color.FromKnownColor(KnownColor.Blue));
  692. if (btn != null)
  693. {
  694. //找到目的站点,放行路线
  695. bool b = true;
  696. if (dicCheck.ContainsKey(drs[j]["工位地标"].ToString()))
  697. {
  698. List<string> tempList = dicCheck[drs[j]["工位地标"].ToString()];
  699. for (int a = 0; a < tempList.Count; a++)
  700. {
  701. int iindex = listModel.FindIndex(o => o.MARKNUM.ToString() == tempList[a]);
  702. if (iindex > -1)
  703. {
  704. b = false;
  705. break;
  706. }
  707. }
  708. }
  709. if (b)
  710. {
  711. AGVIDWithPosByCheckPos.Add(num, int.Parse(drs[j]["工位地标"].ToString()));
  712. AGVIDWithRouteByCheckPos.Add(num, int.Parse(drs[j]["放行路线"].ToString()));
  713. recordReleaseInfo();
  714. DispatcherTool.ReleaseAgvOnStop(pos, int.Parse(drs[j]["放行路线"].ToString()), int.Parse(drs[j]["工位地标"].ToString()));
  715. WriteFile("判断点记录-" + DateTime.Now.ToString("yyyy-MM-dd"), "(判断点地标:" + pos + ",放行路线:" + drs[j]["放行路线"].ToString() + ",目的地标:" + int.Parse(drs[j]["工位地标"].ToString()) + ",车号:" + num + ") ");
  716. btn.Invoke(new InvokeDelegate(delegate
  717. {
  718. if (btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  719. {
  720. btn.BackColor = Color.Blue;
  721. if (btn.Text.Contains("("))
  722. {
  723. int textIndex = btn.Text.LastIndexOf("(");
  724. string orignName = btn.Text.Substring(0, textIndex).Replace("\n", "");
  725. btn.Text = orignName + "\n" + "(" + num + ")";
  726. }
  727. else
  728. {
  729. btn.Text = btn.Text.Replace("\n", "") + "\n" + "(" + num + ")";
  730. }
  731. }
  732. }));
  733. break;
  734. }
  735. }
  736. }
  737. }
  738. }
  739. }
  740. }
  741. }
  742. });
  743. if (result.IsCompleted)
  744. {
  745. sw2.Stop();
  746. Console.WriteLine("判断点任务总共花费{0}s.", sw.Elapsed.TotalSeconds);
  747. Thread.Sleep(500);
  748. Thread t2 = new Thread(new ThreadStart(checkingOperate));
  749. t2.Priority = ThreadPriority.BelowNormal;
  750. t2 .Start();
  751. }
  752. }
  753. catch (Exception ex)
  754. {
  755. DateTime time = DateTime.Now;
  756. //MessageBox.Show("线程异常:"+e.ExceptionObject);
  757. FileStream stream = File.Open(@"Exception\调度程序异常.txt", FileMode.Append, FileAccess.Write);
  758. StreamWriter writer = new StreamWriter(stream);
  759. if (ex != null)
  760. {
  761. 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);
  762. writer.WriteLine(msg);
  763. writer.Close();
  764. stream.Close();
  765. }
  766. bb = true;
  767. //Thread.Sleep(500);
  768. //new Thread(new ThreadStart(checkingOperate)).Start();
  769. }
  770. finally
  771. {
  772. if (bb)
  773. {
  774. Thread.Sleep(500);
  775. Thread t2 = new Thread(new ThreadStart(checkingOperate));
  776. t2.Priority = ThreadPriority.BelowNormal;
  777. t2.Start();
  778. WriteFile("线程重启", "checkingOperate报错,已重启!");
  779. }
  780. }
  781. }
  782. public void CFDCheck()
  783. {
  784. ParallelLoopResult result2;
  785. try
  786. {
  787. List<string> list = dicReleaseArea.Keys.ToList();
  788. result2 = Parallel.ForEach<string>(list, new ParallelOptions { MaxDegreeOfParallelism = 2 }, key =>
  789. {
  790. if (dicReleaseArea[key] != null)
  791. {
  792. List<Button> listBtn = dicReleaseArea[key].btns;
  793. if (listBtn != null && listBtn.Count > 0)
  794. {
  795. var result = listBtn.AsParallel().Where(o => o.BackColor == Color.FromKnownColor(KnownColor.Control));
  796. if (result != null && result.Count() == listBtn.Count)
  797. {
  798. CFDClass cfd = dicReleaseArea[key];
  799. lock (dicReleaseArea)
  800. {
  801. // dicReleaseArea[key] = null;
  802. dicReleaseArea.Remove(key);
  803. }
  804. WriteFile("充放电记录", "(" + cfd.number + "," + cfd.numberInner + ") 组放行完成,已解锁");
  805. }
  806. else
  807. {
  808. for (int i = 0; i < listBtn.Count(); i++)
  809. {
  810. if (listBtn[i].BackColor == Color.Yellow)
  811. {
  812. DispatcherTool.ReleaseAgvOnStop(int.Parse(listBtn[i].Tag.ToString()), 0);
  813. }
  814. }
  815. }
  816. }
  817. }
  818. });
  819. if (result2.IsCompleted)
  820. {
  821. Thread.Sleep(200);
  822. new Thread(new ThreadStart(CFDCheck)).Start();
  823. }
  824. }
  825. catch (Exception ex)
  826. {
  827. DateTime time = DateTime.Now;
  828. //MessageBox.Show("线程异常:"+e.ExceptionObject);
  829. FileStream stream = File.Open(@"Exception\调度程序异常.txt", FileMode.Append, FileAccess.Write);
  830. StreamWriter writer = new StreamWriter(stream);
  831. if (ex != null)
  832. {
  833. 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);
  834. writer.WriteLine(msg);
  835. writer.Close();
  836. stream.Close();
  837. }
  838. Thread.Sleep(200);
  839. new Thread(new ThreadStart(CFDCheck)).Start();
  840. }
  841. }
  842. public void DockingOpeare0(object o)
  843. {
  844. object[] obj = (object[])o;
  845. DockingOpeare((List<Button>)obj[0], (int)obj[1], (string)obj[2]);
  846. }
  847. /// <summary>
  848. /// 工位事件
  849. /// </summary>
  850. /// <param name="listBtn"></param>
  851. public void DockingOpeare(List<Button> listBtn, int tag, string lineName)
  852. {
  853. ParallelLoopResult result2;
  854. bool bb = false;
  855. try
  856. {
  857. Stopwatch sw2 = new Stopwatch();
  858. List<Button> list = (List<Button>)listBtn;
  859. // TabPageTag tag = this.tabControl1.TabPages[i].Tag as TabPageTag;
  860. DataTable dt = (list[0].Parent.Tag as TabPageTag).Table;
  861. List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
  862. List<int> list2 = AGVIDWithPosByCheckPos.Values.ToList();
  863. // while (true)
  864. {
  865. // for (int i = 0; i < list.Count; i++)
  866. result2 = Parallel.ForEach<Button>(list, new ParallelOptions { MaxDegreeOfParallelism = 2 }, btn =>
  867. {
  868. //if (list2.Contains((int)btn.Tag))
  869. //{
  870. // IEnumerable<AGVInfoModel> temp = listModel.Where(o => o.MARKNUM.ToString() == btn.Tag.ToString() && o.ConnectStatus == true);
  871. // btn.Invoke(new InvokeDelegate(delegate
  872. // {
  873. // if (btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  874. // {
  875. // btn.BackColor = Color.Blue;
  876. // }
  877. // }));
  878. //}
  879. //else
  880. {
  881. // Button btn = list[i];
  882. // if (btn.Tag != null)
  883. {
  884. IEnumerable<AGVInfoModel> temp = listModel.Where(o => o.MARKNUM.ToString() == btn.Tag.ToString() && o.ConnectStatus == true);
  885. IEnumerable<DataRow> query =
  886. from order in dt.AsEnumerable()
  887. where order["工位地标"].ToString() == btn.Tag.ToString()
  888. select order;
  889. if (temp.Count() > 0)
  890. {
  891. IEnumerable<AGVInfoModel> temp2 = temp.Where(o => ((o.RUNSTATUS_Cur == 0x43 || o.RUNSTATUS_Cur == 0x44 || (o.MARKFUN == 1 && o.ALARMINFO == "低电压报警"))
  892. && o.ConnectStatus == true && o.AGVNUM != 0));
  893. if (temp2.Count() > 0)
  894. {
  895. List<AGVInfoModel> infos = temp2.ToList();
  896. AGVInfoModel info = infos[0];
  897. btn.Invoke(new InvokeDelegate(delegate
  898. {
  899. if (btn.BackColor != Color.FromKnownColor(KnownColor.Yellow))
  900. {
  901. if (AGVIDWithPosByCheckPos.ContainsKey(info.AGVNUM))
  902. {
  903. AGVIDWithPosByCheckPos.Remove(info.AGVNUM);
  904. }
  905. if (AGVIDWithPosByCheckPos.ContainsKey(info.AGVNUM))
  906. {
  907. int mark = AGVIDWithPosByCheckPos[info.AGVNUM];
  908. AGVIDWithPosByCheckPos.Remove(info.AGVNUM);
  909. if (AGVIDWithPosByCheckPos.ContainsValue(mark))
  910. {
  911. for (int ab = AGVIDWithPosByCheckPos.Keys.Count - 1; ab >= 0; ab--)
  912. {
  913. if (AGVIDWithPosByCheckPos[AGVIDWithPosByCheckPos.Keys.ToList()[ab]] == mark)
  914. {
  915. AGVIDWithPosByCheckPos.Remove(AGVIDWithPosByCheckPos.Keys.ToList()[ab]);
  916. }
  917. }
  918. }
  919. recordReleaseInfo();
  920. }
  921. if (result != null && result.IsSuccess)
  922. {
  923. OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)info.AGVNUM);
  924. OperateResult rs2 = null;
  925. if (info.ALARMCODE != "5")
  926. {
  927. rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.StopOnStation);
  928. }
  929. else //在工位上时 障碍物的状态也给停站状态
  930. {
  931. rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.StopOnStation);
  932. }
  933. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)(int.Parse(info.ALARMCODE) - 1));
  934. if (rs.IsSuccess && rs2.IsSuccess)
  935. {
  936. btn.BackColor = Color.Yellow;
  937. if (btn.Text.Contains("("))
  938. {
  939. int textIndex = btn.Text.LastIndexOf("(");
  940. string orignName = btn.Text.Substring(0, textIndex).Replace("\n", "");
  941. btn.Text = orignName + "\n" + "(" + info.AGVNUM + ")";
  942. }
  943. else
  944. {
  945. btn.Text = btn.Text.Replace("\n", "") + "\n" + "(" + info.AGVNUM + ")";
  946. }
  947. }
  948. }
  949. }
  950. else
  951. {
  952. //OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)info.AGVNUM);
  953. //OperateResult rs2 = null;
  954. //if (info.ALARMCODE != "5")
  955. //{
  956. // rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.StopOnStation);
  957. //}
  958. //else //在工位上时 障碍物的状态也给停站状态
  959. //{
  960. // rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.StopOnStation);
  961. //}
  962. //GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)(int.Parse(info.ALARMCODE) - 1));
  963. }
  964. }));
  965. if (result != null && result.IsSuccess)
  966. {
  967. string releaseAddress = query.ToList()[0]["放行DB"].ToString();
  968. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  969. //if (resultRead.IsSuccess && DBWithByteValue.Keys.Contains(releaseAddress) &&
  970. // DBWithByteValue[releaseAddress] != resultRead.Content)
  971. {
  972. if (resultRead.Content == (byte)GlobalPara.DBControlValues.OKRelease)
  973. {
  974. {
  975. DispatcherTool.ReleaseAgvOnStop(int.Parse(btn.Tag.ToString()),
  976. 0, int.Parse(query.ToList()[0]["目的地标"].ToString()),5);
  977. lock (portLock)
  978. {
  979. DBWithByteValue[releaseAddress] = resultRead.Content;
  980. }
  981. WriteFile("工位点记录-" + DateTime.Now.ToString("yyyy-MM-dd"), "(放行地标:" + btn.Tag.ToString() + ",放行路线:0" + ",目的地标:" + (query.ToList()[0]["目的地标"].ToString()) + ",车号:" + info.AGVNUM + ") ");
  982. }
  983. //走OK放行路线
  984. }
  985. else if (resultRead.Content == (byte)GlobalPara.DBControlValues.NGRelease)
  986. {
  987. {
  988. DispatcherTool.ReleaseAgvOnStop(int.Parse(btn.Tag.ToString()),
  989. int.Parse(query.ToList()[0]["NG路线"].ToString()),0,5);
  990. lock (portLock)
  991. {
  992. DBWithByteValue[releaseAddress] = resultRead.Content;
  993. }
  994. WriteFile("工位点记录-" + DateTime.Now.ToString("yyyy-MM-dd"), "(放行地标:" + btn.Tag.ToString() + ",放行路线:" + query.ToList()[0]["NG路线"].ToString() + ",车号:" + info.AGVNUM + ") ");
  995. }
  996. }
  997. else
  998. {
  999. if (resultRead.Content != (byte)GlobalPara.DBControlValues.StopImmediate)
  1000. {
  1001. DBWithByteValue[releaseAddress] = resultRead.Content;
  1002. }
  1003. }
  1004. }
  1005. }
  1006. }
  1007. else
  1008. {
  1009. IEnumerable<AGVInfoModel> temp3 = temp.Where(o => (o.RUNSTATUS_Cur == 0x41 || o.RUNSTATUS_Cur == 0x47 || (o.MARKFUN == 1 && o.ALARMINFO == "低电压停机")) && o.ConnectStatus == true && o.AGVNUM != 0);
  1010. if (temp3.Count() > 0)
  1011. {
  1012. AGVInfoModel info = temp3.ToList()[0];
  1013. btn.Invoke(new InvokeDelegate(delegate
  1014. {
  1015. if (btn.BackColor != Color.FromKnownColor(KnownColor.Red))
  1016. {
  1017. if (AGVIDWithPosByCheckPos.ContainsKey(info.AGVNUM))
  1018. {
  1019. AGVIDWithPosByCheckPos.Remove(info.AGVNUM);
  1020. }
  1021. if (AGVIDWithRouteByCheckPos.ContainsKey(info.AGVNUM))
  1022. {
  1023. AGVIDWithRouteByCheckPos.Remove(info.AGVNUM);
  1024. }
  1025. if (result != null && result.IsSuccess && info.ALARMCODE != "")
  1026. {
  1027. OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)info.AGVNUM);
  1028. OperateResult rs2 = null;
  1029. if (info.ALARMCODE != "5" && info.ALARMCODE != "0" && info.ALARMCODE != "9")
  1030. {
  1031. rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Pause);
  1032. }
  1033. else
  1034. {
  1035. rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Running);
  1036. }
  1037. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)(int.Parse(info.ALARMCODE) - 1));
  1038. if (btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  1039. {
  1040. if (rs.IsSuccess && rs2.IsSuccess)
  1041. {
  1042. btn.BackColor = Color.Red;
  1043. if (btn.Text.Contains("("))
  1044. {
  1045. int textIndex = btn.Text.LastIndexOf("(");
  1046. string orignName = btn.Text.Substring(0, textIndex).Replace("\n", "");
  1047. btn.Text = orignName + "\n" + "(" + info.AGVNUM + ")";
  1048. }
  1049. else
  1050. {
  1051. btn.Text = btn.Text.Replace("\n", "") + "\n" + "(" + info.AGVNUM + ")";
  1052. }
  1053. }
  1054. }
  1055. }
  1056. }
  1057. else
  1058. {
  1059. //OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)info.AGVNUM);
  1060. //OperateResult rs2 = null;
  1061. //if (info.ALARMCODE != "5" && info.ALARMCODE != "0" && info.ALARMCODE != "9")
  1062. //{
  1063. // rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Pause);
  1064. //}
  1065. //else
  1066. //{
  1067. // rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Running);
  1068. //}
  1069. //GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)(int.Parse(info.ALARMCODE) - 1));
  1070. }
  1071. }));
  1072. if (info.ALARMCODE == "1")
  1073. {
  1074. if (result != null && result.IsSuccess)
  1075. {
  1076. string releaseAddress = query.ToList()[0]["放行DB"].ToString();
  1077. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  1078. // if (resultRead.IsSuccess && DBWithByteValue.Keys.Contains(releaseAddress) &&
  1079. // DBWithByteValue[releaseAddress] != resultRead.Content)
  1080. {
  1081. if (resultRead.Content == (byte)GlobalPara.DBControlValues.StopRelease)
  1082. {
  1083. lock (portLock)
  1084. {
  1085. DBWithByteValue[releaseAddress] = resultRead.Content;
  1086. }
  1087. DispatcherTool.ControlAGVBy1071(info.AGVNUM, 0);
  1088. //放行被暂停的车
  1089. WriteFile("急停记录", btn.Text + "工位被放行,已生成消息,当前放行地标:" + btn.Tag.ToString() + ",车号为:" + info.AGVNUM);
  1090. }
  1091. else if (resultRead.Content == (byte)GlobalPara.DBControlValues.OKRelease)
  1092. {
  1093. lock (portLock)
  1094. {
  1095. DBWithByteValue[releaseAddress] = resultRead.Content;
  1096. }
  1097. DispatcherTool.ReleaseAgvOnStop(int.Parse(btn.Tag.ToString()),
  1098. int.Parse(query.ToList()[0]["放行路线"].ToString()));
  1099. //放行被暂停的车
  1100. WriteFile("工位点记录" + DateTime.Now.ToString("yyyy-MM-dd"), btn.Text + "工位被放行,已生成消息,当前放行地标:" + btn.Tag.ToString() + ",车号为:" + info.AGVNUM);
  1101. }
  1102. else
  1103. {
  1104. DBWithByteValue[releaseAddress] = resultRead.Content;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. }
  1110. else
  1111. {
  1112. IEnumerable<AGVInfoModel> temp4 = temp.Where(o => o.RUNSTATUS_Cur == 0x40 && o.ConnectStatus == true && o.AGVNUM != 0);
  1113. if (temp4.Count() > 0)
  1114. {
  1115. AGVInfoModel info = temp4.ToList()[0];
  1116. btn.Invoke(new InvokeDelegate(delegate
  1117. {
  1118. if (btn.BackColor != Color.FromKnownColor(KnownColor.Green))
  1119. {
  1120. if (AGVIDWithPosByCheckPos.ContainsKey(info.AGVNUM))
  1121. {
  1122. AGVIDWithPosByCheckPos.Remove(info.AGVNUM);
  1123. }
  1124. if (AGVIDWithRouteByCheckPos.ContainsKey(info.AGVNUM))
  1125. {
  1126. AGVIDWithRouteByCheckPos.Remove(info.AGVNUM);
  1127. }
  1128. // if (info.AGVNUM != 0)
  1129. //{
  1130. if (result != null && result.IsSuccess)
  1131. {
  1132. OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)info.AGVNUM);
  1133. OperateResult rs2 = null;
  1134. rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Running);
  1135. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)(int.Parse(info.ALARMCODE) - 1));
  1136. //}
  1137. if (btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  1138. {
  1139. if (rs.IsSuccess && rs2.IsSuccess)
  1140. {
  1141. btn.BackColor = Color.Green;
  1142. if (btn.Text.Contains("("))
  1143. {
  1144. int textIndex = btn.Text.LastIndexOf("(");
  1145. string orignName = btn.Text.Substring(0, textIndex).Replace("\n", "");
  1146. btn.Text = orignName + "\n" + "(" + info.AGVNUM + ")";
  1147. }
  1148. else
  1149. {
  1150. btn.Text = btn.Text.Replace("\n", "") + "\n" + "(" + info.AGVNUM + ")";
  1151. }
  1152. }
  1153. }
  1154. }
  1155. }
  1156. else
  1157. {
  1158. //OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)info.AGVNUM);
  1159. //OperateResult rs2 = null;
  1160. //rs2 = GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Running);
  1161. //GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)(int.Parse(info.ALARMCODE) - 1));
  1162. }
  1163. }));
  1164. if (query.ToList()[0]["放行DB"].ToString() == "DB200.2")
  1165. {
  1166. // MessageBox.Show(info.AGVNUM.ToString() + ":" + info.RUNSTATUS_Cur.ToString());
  1167. }
  1168. if (info.RUNSTATUS_Cur == 0x40 && info.ALARMCODE == "1")
  1169. {
  1170. if (result != null && result.IsSuccess)
  1171. {
  1172. string releaseAddress = query.ToList()[0]["放行DB"].ToString();
  1173. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  1174. // if (resultRead.IsSuccess && DBWithByteValue.Keys.Contains(releaseAddress) &&
  1175. // DBWithByteValue[releaseAddress] != resultRead.Content)
  1176. {
  1177. if (resultRead.Content == (byte)GlobalPara.DBControlValues.StopImmediate)
  1178. {
  1179. lock (portLock)
  1180. {
  1181. DBWithByteValue[releaseAddress] = resultRead.Content;
  1182. }
  1183. DispatcherTool.ControlAGVBy1071(info.AGVNUM, 1);
  1184. //立刻暂停行驶中的车
  1185. WriteFile("急停记录", btn.Text + "工位被暂停,已生成消息,当前急停地标:" + btn.Tag.ToString() + ",车号为:" + info.AGVNUM);
  1186. }
  1187. else
  1188. {
  1189. lock (portLock)
  1190. {
  1191. DBWithByteValue[releaseAddress] = (byte)resultRead.Content;
  1192. }
  1193. }
  1194. }
  1195. }
  1196. }
  1197. }
  1198. else
  1199. {
  1200. btn.Invoke(new InvokeDelegate(delegate
  1201. {
  1202. if (tag >= 1)
  1203. {
  1204. if (AGVIDWithPosByCheckPos.ContainsValue(int.Parse(query.ToList()[0]["工位地标"].ToString())))
  1205. {
  1206. List<int> keys = AGVIDWithPosByCheckPos.Keys.ToList();
  1207. for (int a = 0; a < keys.Count; a++)
  1208. {
  1209. if (AGVIDWithPosByCheckPos[keys[a]] == int.Parse(query.ToList()[0]["工位地标"].ToString()))
  1210. {
  1211. btn.Text = query.ToList()[0]["工位名称"].ToString() + "\n(" + keys[a] + ")";
  1212. btn.BackColor = Color.Blue;
  1213. break;
  1214. }
  1215. }
  1216. }
  1217. else
  1218. {
  1219. if (btn.BackColor != Color.FromKnownColor(KnownColor.Control))
  1220. {
  1221. OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)0);
  1222. OperateResult rs2 =
  1223. GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Unknow);
  1224. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)0);
  1225. if (btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  1226. {
  1227. if (rs.IsSuccess && rs2.IsSuccess)
  1228. {
  1229. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  1230. if (btn.Text.Contains("("))
  1231. {
  1232. int textIndex = btn.Text.LastIndexOf("(");
  1233. string orignName = btn.Text.Substring(0, textIndex).Replace("\n", "");
  1234. btn.Text = orignName;
  1235. }
  1236. }
  1237. }
  1238. }
  1239. }
  1240. }
  1241. else if (tag == 0)
  1242. {
  1243. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  1244. if (result != null && result.IsSuccess)
  1245. {
  1246. GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)0);
  1247. GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Unknow);
  1248. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)0);
  1249. }
  1250. if (AGVIDWithPosByCheckPos.ContainsValue(int.Parse(query.ToList()[0]["工位地标"].ToString())))
  1251. {
  1252. List<int> keys = AGVIDWithPosByCheckPos.Keys.ToList();
  1253. for (int a = 0; a < keys.Count; a++)
  1254. {
  1255. if (AGVIDWithPosByCheckPos[keys[a]] == int.Parse(query.ToList()[0]["工位地标"].ToString()))
  1256. {
  1257. btn.Text = query.ToList()[0]["工位名称"].ToString() + "\n(" + keys[a] + ")";
  1258. btn.BackColor = Color.Blue;
  1259. break;
  1260. }
  1261. }
  1262. }
  1263. }
  1264. }));
  1265. if (result != null && result.IsSuccess)
  1266. {
  1267. string releaseAddress = query.ToList()[0]["放行DB"].ToString();
  1268. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  1269. //if (resultRead.IsSuccess && DBWithByteValue.Keys.Contains(releaseAddress) &&
  1270. // DBWithByteValue[releaseAddress] != resultRead.Content)
  1271. {
  1272. {
  1273. if (resultRead.Content != (byte)GlobalPara.DBControlValues.StopImmediate)
  1274. {
  1275. DBWithByteValue[releaseAddress] = resultRead.Content;
  1276. }
  1277. }
  1278. }
  1279. }
  1280. }
  1281. }
  1282. }
  1283. }
  1284. else
  1285. {
  1286. btn.Invoke(new InvokeDelegate(delegate
  1287. {
  1288. if (tag >= 1)
  1289. {
  1290. if (btn.BackColor != Color.FromKnownColor(KnownColor.Control) &&
  1291. btn.BackColor != Color.FromKnownColor(KnownColor.Blue))
  1292. {
  1293. OperateResult rs = GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)0);
  1294. OperateResult rs2 =
  1295. GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Unknow);
  1296. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)0);
  1297. if (rs.IsSuccess && rs2.IsSuccess)
  1298. {
  1299. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  1300. if (btn.Text.Contains("("))
  1301. {
  1302. int textIndex = btn.Text.LastIndexOf("(");
  1303. string orignName = btn.Text.Substring(0, textIndex).Replace("\n", "");
  1304. btn.Text = orignName;
  1305. }
  1306. }
  1307. }
  1308. }
  1309. else if (tag <= 0)
  1310. {
  1311. btn.BackColor = Color.FromKnownColor(KnownColor.Control);
  1312. if (result != null && result.IsSuccess)
  1313. {
  1314. GlobalPara.s7.Write(query.ToList()[0]["AGVDB"].ToString(), (byte)0);
  1315. GlobalPara.s7.Write(query.ToList()[0]["行驶状态DB"].ToString(), (byte)GlobalPara.DBRunValues.Unknow);
  1316. GlobalPara.s7.Write(query.ToList()[0]["故障DB"].ToString(), (byte)0);
  1317. }
  1318. if (AGVIDWithPosByCheckPos.ContainsValue(int.Parse(query.ToList()[0]["工位地标"].ToString())))
  1319. {
  1320. List<int> keys = AGVIDWithPosByCheckPos.Keys.ToList();
  1321. for (int a = 0; a < keys.Count; a++)
  1322. {
  1323. if (AGVIDWithPosByCheckPos[keys[a]] == int.Parse(query.ToList()[0]["工位地标"].ToString()))
  1324. {
  1325. btn.Text = query.ToList()[0]["工位名称"].ToString() + "\n(" + keys[a] + ")";
  1326. btn.BackColor = Color.Blue;
  1327. break;
  1328. }
  1329. }
  1330. }
  1331. }
  1332. }));
  1333. if (result != null && result.IsSuccess)
  1334. {
  1335. string releaseAddress = query.ToList()[0]["放行DB"].ToString();
  1336. OperateResult<byte> resultRead = GlobalPara.s7.ReadByte(releaseAddress);
  1337. //if (resultRead.IsSuccess && DBWithByteValue.Keys.Contains(releaseAddress) &&
  1338. // DBWithByteValue[releaseAddress] != resultRead.Content)
  1339. {
  1340. if (resultRead.Content != (byte)GlobalPara.DBControlValues.StopImmediate)
  1341. {
  1342. DBWithByteValue[releaseAddress] = resultRead.Content;
  1343. }
  1344. }
  1345. }
  1346. }
  1347. }
  1348. }
  1349. });
  1350. if (result2.IsCompleted)
  1351. {
  1352. sw2.Stop();
  1353. Console.WriteLine(dt.TableName + "任务总共花费{0}s.", sw.Elapsed.TotalSeconds);
  1354. object[] obj = new object[3];
  1355. obj[0] = listBtn; obj[1] = ++tag; obj[2] = lineName;
  1356. if (tag == 10)
  1357. {
  1358. if (!InitOK.ContainsKey(lineName))
  1359. {
  1360. InitOK.Add(lineName, true);
  1361. // tag = 1;
  1362. }
  1363. }
  1364. if (tag > 10)
  1365. {
  1366. tag = 11;
  1367. }
  1368. Thread.Sleep(100);
  1369. Thread t2 = new Thread(new ParameterizedThreadStart(DockingOpeare0));
  1370. t2.Priority = ThreadPriority.Highest;
  1371. t2.Start(obj);
  1372. // DockingOpeare(list);
  1373. }
  1374. }
  1375. }
  1376. catch (Exception ex)
  1377. {
  1378. DateTime time = DateTime.Now;
  1379. //MessageBox.Show("线程异常:"+e.ExceptionObject);
  1380. FileStream stream = File.Open(@"Exception\调度程序异常.txt", FileMode.Append, FileAccess.Write);
  1381. StreamWriter writer = new StreamWriter(stream);
  1382. if (ex != null)
  1383. {
  1384. 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);
  1385. writer.WriteLine(msg);
  1386. writer.Close();
  1387. stream.Close();
  1388. }
  1389. bb = true;
  1390. //object[] obj = new object[3];
  1391. //obj[0] = listBtn; obj[1] = 0; obj[2] = lineName;
  1392. //Thread.Sleep(500);
  1393. //new Thread(new ParameterizedThreadStart(DockingOpeare0)).Start(obj);
  1394. }
  1395. finally
  1396. {
  1397. if (bb)
  1398. {
  1399. object[] obj = new object[3];
  1400. obj[0] = listBtn; obj[1] = 0; obj[2] = lineName;
  1401. Thread.Sleep(500);
  1402. Thread t2 = new Thread(new ParameterizedThreadStart(DockingOpeare0));
  1403. t2.Priority = ThreadPriority.Highest;
  1404. t2.Start(obj);
  1405. WriteFile("线程重启", "DockingOpeare0报错,已重启!");
  1406. }
  1407. if (!DockingOK && InitOK.Count == LineDt.Rows.Count)
  1408. {
  1409. DockingOK = true;
  1410. Thread t2 = new Thread(new ThreadStart(checkingOperate));
  1411. t2.Priority = ThreadPriority.BelowNormal;
  1412. t2.Start();
  1413. Thread t3 = new Thread(new ThreadStart(TrafficByDispatch));
  1414. t3.Priority = ThreadPriority.AboveNormal;
  1415. t3.Start();
  1416. }
  1417. }
  1418. }
  1419. void WriteFile(string fileName, string context)
  1420. {
  1421. DateTime time = DateTime.Now;
  1422. //MessageBox.Show("线程异常:"+e.ExceptionObject);
  1423. FileStream stream = File.Open(@"Exception\" + fileName + ".txt", FileMode.Append, FileAccess.Write);
  1424. StreamWriter writer = new StreamWriter(stream);
  1425. {
  1426. string msg = string.Format("[{0}]Test:\r\nMessage:{1}\r\n", time.ToString("yyyy-MM-dd HH:mm:ss"), context);
  1427. writer.WriteLine(msg);
  1428. writer.Close();
  1429. stream.Close();
  1430. }
  1431. }
  1432. void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
  1433. {
  1434. Graphics g = e.Graphics;
  1435. Font font = new Font("微软雅黑", 16f, System.Drawing.GraphicsUnit.Pixel);
  1436. SolidBrush brush = new SolidBrush(Color.Black);
  1437. if (e.Index == this.tabControl1.SelectedIndex)
  1438. {
  1439. brush = new SolidBrush(Color.Blue);
  1440. }
  1441. RectangleF rectangle = (RectangleF)(tabControl1.GetTabRect(e.Index));
  1442. RectangleF rectangle2 = new RectangleF(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
  1443. g.FillRectangle(new SolidBrush(SystemColors.ButtonHighlight), rectangle2);
  1444. StringFormat sformat = new StringFormat();
  1445. sformat.LineAlignment = StringAlignment.Center;
  1446. sformat.Alignment = StringAlignment.Center;
  1447. g.DrawString(((TabControl)sender).TabPages[e.Index].Text, font, brush, rectangle2, sformat);
  1448. }
  1449. /// <summary>
  1450. /// 获取心跳包
  1451. /// </summary>
  1452. private void PLCHeartBit()
  1453. {
  1454. while (true)
  1455. {
  1456. PLCResult = GlobalPara.s7.ReadByte(dtConfig.Rows[4][1].ToString()); //PLC心跳记录
  1457. if (PLCResult != null && PLCResult.IsSuccess)
  1458. {
  1459. // OperateResult<byte> tempResult = GlobalPara.s7.ReadByte(dtConfig.Rows[1][4].ToString());
  1460. if (PLCResult != null && PLCResult.IsSuccess)
  1461. {
  1462. if (PLCResult.Content != PLCHeartBitCount)
  1463. {
  1464. result = GlobalPara.s7.ConnectServer();
  1465. PLCHeartBitCount = PLCResult.Content;
  1466. HeartBitFlag = true;
  1467. IsPLCChangeOff = false;
  1468. }
  1469. else
  1470. {
  1471. result = null;
  1472. GlobalPara.s7.ConnectServer();
  1473. HeartBitFlag = false;
  1474. IsPLCChangeOff = true;
  1475. }
  1476. }
  1477. }
  1478. else
  1479. {
  1480. result = null;
  1481. GlobalPara.s7.ConnectServer();
  1482. HeartBitFlag = false;
  1483. IsPLCChangeOff = true;
  1484. }
  1485. Thread.Sleep(2000);
  1486. }
  1487. }
  1488. /// <summary>
  1489. /// 发送心跳包
  1490. /// </summary>
  1491. private void HeartBit()
  1492. {
  1493. while (true)
  1494. {
  1495. if (HeartBitFlag)
  1496. {
  1497. //更新心跳包
  1498. if (result == null || !result.IsSuccess)
  1499. {
  1500. if (GlobalPara.s7 != null)
  1501. {
  1502. result = GlobalPara.s7.ConnectServer();
  1503. }
  1504. statusStrip1.Invoke(new InvokeDelegate(delegate
  1505. {
  1506. toolStripStatusLabel1.Text = GlobalPara.s7.IpAddress + ", " + "PLC连接失败";
  1507. }));
  1508. }
  1509. else
  1510. {
  1511. if (result != null && result.IsSuccess)
  1512. {
  1513. var tempResult = GlobalPara.s7.Write(dtConfig.Rows[1][1].ToString(), (byte)HeartBitCount);
  1514. if (tempResult.IsSuccess)
  1515. {
  1516. HeartBitCount++;
  1517. if (HeartBitCount > 0xFF)
  1518. {
  1519. HeartBitCount = 1;
  1520. }
  1521. statusStrip1.Invoke(new InvokeDelegate(delegate
  1522. {
  1523. toolStripStatusLabel1.Text = GlobalPara.s7.IpAddress + "," + " " + "PLC连接成功";
  1524. }
  1525. ));
  1526. }
  1527. else
  1528. {
  1529. this.Invoke(new InvokeDelegate(delegate
  1530. {
  1531. toolStripStatusLabel1.Text = GlobalPara.s7.IpAddress + "," + " " + "PLC连接失败";
  1532. }
  1533. ));
  1534. }
  1535. }
  1536. }
  1537. }
  1538. Thread.Sleep(500);
  1539. }
  1540. }
  1541. /// <summary>
  1542. /// 处理调度的协议
  1543. /// </summary>
  1544. private void Analzye()
  1545. {
  1546. while (true)
  1547. {
  1548. if (GlobalPara.dispatchList.Count > 0)
  1549. {
  1550. byte[] bytes = GlobalPara.dispatchList[0];
  1551. Console.WriteLine("调度模块收到数据:" + GlobalPara.ByteToHexString(bytes));
  1552. GlobalPara.dispatchList.Remove(bytes);
  1553. }
  1554. Thread.Sleep(20);
  1555. }
  1556. }
  1557. }
  1558. }