private void stationThread() { List <Track> list; while (true) { for (int i = 0; i < stationList1.Count; i++) { Station s = stationList1[i]; if ((s.OccupiedCar != null) && (s.Next != "")) { if (stationDic[s.Next].OccupiedCar == null) { list = adjList.FindWay(adjList.Find(s), adjList.Find(stationDic[s.Next])); Track track = new Track(); foreach (Track t in list) { track.TrackPointList.AddRange(t.TrackPointList); } s.OccupiedCar.run(track); stationDic[s.Next].OccupiedCar = s.OccupiedCar; s.OccupiedCar = null; } } } Thread.Sleep(500); } }
public byte planRoadTable(byte carID, Station startStation, Station targetStation, Station endStation, AdjacencyList adj, SerialPort sp, Dictionary <string, Station> stationDic) { List <Track> list1 = adj.FindWay(adj.Find(startStation), adj.Find(targetStation)); List <Track> list2 = adj.FindWay(adj.Find(targetStation), adj.Find(stationDic["F32"])); List <Track> list3 = adj.FindWay(adj.Find(stationDic["F32"]), adj.Find(endStation)); List <byte> command = new List <byte>(); for (int i = 0; i < list1.Count; ++i) { if (list1[i].CarAction != null) { string station = list1[i].CarAction.Substring(0, list1[i].CarAction.IndexOf('G')); command.Add((byte)stationDic[station].CardID); switch (list1[i].CarAction.Substring(list1[i].CarAction.Length - 1, 1)) { case "L": command.Add((byte)(0x50)); break; case "R": command.Add((byte)(0x51)); break; case "S": command.Add((byte)(0x52)); break; } } } command.Add((byte)stationDic[targetStation.Name].CardID); command.Add((byte)(0x53)); for (int i = 0; i < list2.Count; ++i) { if (list2[i].CarAction != null) { string station = list2[i].CarAction.Substring(0, list2[i].CarAction.IndexOf('G')); command.Add((byte)stationDic[station].CardID); switch (list2[i].CarAction.Substring(list2[i].CarAction.Length - 1, 1)) { case "L": command.Add((byte)(0x50)); break; case "R": command.Add((byte)(0x51)); break; case "S": command.Add((byte)(0x52)); break; } } } command.Add((byte)stationDic["F32"].CardID); command.Add((byte)(0x53)); for (int i = 1; i < list3.Count; ++i) { if (list3[i].CarAction != null) { string station = list3[i].CarAction.Substring(0, list3[i].CarAction.IndexOf('G')); command.Add((byte)stationDic[station].CardID); switch (list3[i].CarAction.Substring(list3[i].CarAction.Length - 1, 1)) { case "L": command.Add((byte)(0x50)); break; case "R": command.Add((byte)(0x51)); break; case "S": command.Add((byte)(0x52)); break; } } } command.Add((byte)stationDic[endStation.Name].CardID); command.Add((byte)(0x53)); if (command.Count >= 4 && command.Count <= 20) { byte CardCount = (byte)(command.Count >> 1); command.Insert(0, CardCount); command.Insert(0, CardCount); command.Insert(0, (byte)0); command.Insert(0, (byte)(carID)); command.Insert(0, (byte)0); command.Insert(0, (byte)0); command.Insert(0, (byte)(CardCount * 2 + 4)); command.Insert(0, (byte)0xe6); command.Add(checksum(command)); command.Insert(0, (byte)0x68); byte[] roadTable = new byte[command.Count]; command.CopyTo(roadTable); //for (int j = 0; j < roadTable.Length; ++j) //{ // Console.Write(roadTable[j] + " "); //} sp.Write(roadTable, 0, roadTable.Length); return(CardCount); } else if (command != null && 20 < command.Count && command.Count <= 40) { byte total = (byte)(command.Count >> 1); byte CardAccount = (byte)(command.Count >> 1); command.Insert(0, (byte)10); command.Insert(0, total); command.Insert(0, (byte)0); command.Insert(0, carID); command.Insert(0, (byte)0); command.Insert(0, (byte)0); command.Insert(0, (byte)(24)); command.Insert(0, (byte)0xe6); byte csum = checksum(command, 28); command.Insert(0, (byte)0x68); byte[] roadTable = new byte[30]; command.CopyTo(0, roadTable, 0, 29); roadTable[29] = (byte)(csum); //for (int j = 0; j < 30; ++j) //{ // Console.Write(roadTable[j] + " "); //} //Console.WriteLine(); //roadTable[12] = (byte)0x50; //roadTable[18] = (byte)0x50; //roadTable[26] = (byte)0x50; //roadTable[28] = (byte)0x50; sp.Write(roadTable, 0, 30); Thread.Sleep(5000); command.RemoveRange(0, 29); CardAccount = (byte)(command.Count / 2); command.Insert(0, CardAccount); command.Insert(0, total); command.Insert(0, (byte)0x0a); command.Insert(0, carID);//bug command.Insert(0, (byte)0); command.Insert(0, (byte)0); command.Insert(0, (byte)(CardAccount * 2 + 4)); command.Insert(0, (byte)0xe6); command.Add(checksum(command)); command.Insert(0, (byte)0x68); roadTable = new byte[command.Count]; command.CopyTo(roadTable); //for (int j = 0; j < roadTable.Length; ++j) //{ // Console.Write(roadTable[j] + " "); //} sp.Write(roadTable, 0, roadTable.Length); return(total); } else if (command != null && 56 < command.Count && command.Count <= 84) { byte total = (byte)(command.Count >> 1); byte CardAccount = (byte)(command.Count >> 1); command.Insert(0, (byte)14); command.Insert(0, total); command.Insert(0, (byte)0); command.Insert(0, carID); command.Insert(0, (byte)0); command.Insert(0, (byte)0); command.Insert(0, (byte)(32)); command.Insert(0, (byte)0xe6); //command.Add(checksum(command)); byte csum = checksum(command, 36); command.Insert(0, (byte)0x68); byte[] roadTable = new byte[38]; command.CopyTo(0, roadTable, 0, 37); roadTable[37] = csum; for (int j = 0; j < 38; ++j) { Console.Write(roadTable[j] + " "); } Console.WriteLine(); sp.Write(roadTable, 0, 38); command.RemoveRange(0, 37); Console.WriteLine(); command.Insert(0, (byte)14); command.Insert(0, total); command.Insert(0, (byte)0x26); command.Insert(0, carID); command.Insert(0, (byte)0); command.Insert(0, (byte)0); command.Insert(0, (byte)(32)); command.Insert(0, (byte)0xe6); //command.Add(checksum(command)); csum = checksum(command, 36); command.Insert(0, (byte)0x68); roadTable = new byte[38]; command.CopyTo(0, roadTable, 0, 37); roadTable[37] = csum; for (int j = 0; j < 38; ++j) { Console.Write(roadTable[j] + " "); } Console.WriteLine(); sp.Write(roadTable, 0, 38); command.RemoveRange(0, 37); CardAccount = (byte)(command.Count >> 1); command.Insert(0, CardAccount); command.Insert(0, total); command.Insert(0, (byte)0x4c); command.Insert(0, carID); command.Insert(0, (byte)0); command.Insert(0, (byte)0); command.Insert(0, (byte)(CardAccount * 2 + 4)); command.Insert(0, (byte)0xe6); command.Add(checksum(command)); command.Insert(0, (byte)0x68); roadTable = new byte[command.Count]; command.CopyTo(roadTable); for (int j = 0; j < roadTable.Length; ++j) { Console.Write(roadTable[j] + " "); } sp.Write(roadTable, 0, roadTable.Length); return(0); } return(0); }
public Form1() { InitializeComponent(); comboBox.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames()); comboBox.SelectedItem = comboBox.Items[0]; InitPort(); CurvePoint1 = new Point[0]; #region Start = new Station("W0", 150, 400); T1 = new Station("T1", 400, 300); K1 = new Station("K1", 350, 350); F1 = new Station("F1", 450, 350); T2 = new Station("T2", 400, 500); K2 = new Station("K2", 350, 450); F2 = new Station("F2", 450, 450); P1 = new Station("P1", 200, 350); P2 = new Station("P2", 600, 350); P3 = new Station("P3", 200, 450); P4 = new Station("P4", 600, 450); Adj.AddVertex(Start); Adj.AddVertex(T1); Adj.AddVertex(K1); Adj.AddVertex(F1); Adj.AddVertex(T2); Adj.AddVertex(K2); Adj.AddVertex(F2); Adj.AddVertex(P1); Adj.AddVertex(P2); Adj.AddVertex(P3); Adj.AddVertex(P4); Adj.AddDirectedEdge(Adj.Find(Start), Adj.Find(P1)); //1 Adj.AddDirectedEdge(Adj.Find(P1), Adj.Find(K1)); //2 Adj.AddDirectedEdge(Adj.Find(K1), Adj.Find(F1)); //3 Adj.AddDirectedEdge(Adj.Find(K1), Adj.Find(T1)); //4 Adj.AddDirectedEdge(Adj.Find(T1), Adj.Find(F1)); //5 Adj.AddDirectedEdge(Adj.Find(F1), Adj.Find(P2)); //6 Adj.AddDirectedEdge(Adj.Find(P2), Adj.Find(P4)); //7 Adj.AddDirectedEdge(Adj.Find(P4), Adj.Find(F2)); //8 Adj.AddDirectedEdge(Adj.Find(F2), Adj.Find(K2)); //9 Adj.AddDirectedEdge(Adj.Find(F2), Adj.Find(T2)); //10 Adj.AddDirectedEdge(Adj.Find(T2), Adj.Find(K2)); //11 Adj.AddDirectedEdge(Adj.Find(K2), Adj.Find(P3)); //12 Adj.AddDirectedEdge(Adj.Find(P3), Adj.Find(Start)); //13 #endregion }