Created
April 11, 2017 16:08
-
-
Save oisincar/8c6f2e4241eacd749d2f29ef80b58d0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.IO.Ports; | |
| using System.Threading; | |
| namespace Lab2 | |
| { | |
| class Program | |
| { | |
| static List<string> messages = new List<string>(); | |
| static SerialPort port = new SerialPort(); | |
| static int numGanteries = 0; | |
| // toggles between 0 and 1. | |
| // Buggy 0 parks in the 3rd gantry and goes first. | |
| // Buggy 1 parks in the parking lane and goes second. | |
| // Currently, Buggy 0 always goes clockwise (and goes first) and buggy 1 always goes anti-clockwise, starting second. | |
| static int currentBuggy = 0; | |
| static int lapsLeft = 20; | |
| static bool buggy0Alive = false; | |
| static bool buggy1Alive = false; | |
| static bool buggy0Responded = false; | |
| static bool buggy1Responded = false; | |
| static bool isDone = false; | |
| public enum Challege { Bronze, Silver, Gold }; | |
| static Challege CurrentChallege; | |
| static void Main(string[] args) | |
| { | |
| // Initilise port with correct port ID. | |
| port.PortName = "COM8"; | |
| port.BaudRate = 9600; | |
| port.Open(); | |
| port.Write("+++"); | |
| Thread.Sleep(1100); | |
| port.WriteLine("ATID 3306, CH C, CN"); | |
| // Set up listener to port, whenever the port recieves information, DataHandler will be called. | |
| port.DataReceived += new SerialDataReceivedEventHandler(DataHandler); | |
| int mode = 0; | |
| while (true) | |
| { | |
| Console.WriteLine("Enter the challage you'd like to run. 0: Bronze, 1: Silver, 2: Gold"); | |
| if (int.TryParse(Console.ReadLine(), out mode)) | |
| break; | |
| else | |
| Console.WriteLine("Please enter a valid number."); | |
| } | |
| if (mode == 0) CurrentChallege = Challege.Bronze; | |
| if (mode == 1) CurrentChallege = Challege.Silver; | |
| if (mode == 2) CurrentChallege = Challege.Gold; | |
| if (CurrentChallege == Challege.Silver) | |
| lapsLeft = 2; | |
| while (true && CurrentChallege == Challege.Gold) | |
| { | |
| Console.WriteLine("Enter the number of laps you'd like each buggy to complete"); | |
| if (int.TryParse(Console.ReadLine(), out lapsLeft)) | |
| break; | |
| else | |
| Console.WriteLine("Please enter a valid number."); | |
| } | |
| // Convert to the total number of laps done by either buggy. | |
| // E.g: If you enter 2 laps. Both buggies do 2 laps, and the last buggy does an extra lap to park = 5 laps. | |
| lapsLeft = lapsLeft * 2 + 1; | |
| //while (true) | |
| //{ | |
| // Console.Write("> "); | |
| // string message = Console.ReadLine(); | |
| // if (message == "PrintChat") | |
| // PrintChat(); | |
| // port.WriteLine(message); | |
| // messages.Add("Sent: " + message); | |
| //} | |
| while (true) | |
| { | |
| if (!buggy0Responded && buggy0Alive) | |
| { | |
| Console.WriteLine("Buggy 0 disconnected"); | |
| buggy0Alive = false; | |
| } | |
| if (!buggy1Responded && buggy1Alive) | |
| { | |
| Console.WriteLine("Buggy 1 disconnected"); | |
| buggy1Alive = false; | |
| } | |
| if (buggy0Responded && !buggy0Alive) | |
| { | |
| Console.WriteLine("Buggy 0 reconnected"); | |
| buggy0Alive = true; | |
| if (currentBuggy == 0 && ! isDone) | |
| port.WriteLine("0GO"); | |
| } | |
| if (buggy1Responded && !buggy1Alive) | |
| { | |
| Console.WriteLine("Buggy 1 reconnected"); | |
| buggy1Alive = true; | |
| if (currentBuggy == 1 && ! isDone) | |
| port.WriteLine("1GO"); | |
| } | |
| buggy0Responded = false; | |
| buggy1Responded = false; | |
| // ping buggys | |
| port.WriteLine("0?"); | |
| port.WriteLine("1?"); | |
| //Console.WriteLine("tst"); | |
| Thread.Sleep(2000); | |
| } | |
| } | |
| static void PrintChat() | |
| { | |
| foreach (string s in messages) | |
| { | |
| Console.WriteLine(s); | |
| } | |
| } | |
| static string message = ""; | |
| private static void DataHandler(object sender, SerialDataReceivedEventArgs e) | |
| { | |
| SerialPort sp = (SerialPort) sender; | |
| foreach (char c in sp.ReadExisting()) | |
| { | |
| if (c == '\n') | |
| { | |
| HandleMessage(message); | |
| message = ""; | |
| } | |
| else | |
| message += c; | |
| } | |
| } | |
| private static void HandleMessage(string message) | |
| { | |
| //SerialPort sp = (SerialPort) sender; | |
| //string message = sp.ReadExisting(); | |
| /Console.WriteLine($"recieved: {message}"); | |
| // Buggy ping renponse. | |
| if (message.StartsWith("!")) | |
| { | |
| if (message[1] == '0') | |
| buggy0Responded = true; | |
| else if (message[1] == '1') | |
| buggy1Responded = true; | |
| else Console.WriteLine("error reading ping response"); | |
| } | |
| // Gantry message. | |
| else if (message.StartsWith("G")) | |
| { | |
| numGanteries++; | |
| int gate = 4; | |
| // When the buggy is parked it sends 'GP', so if the second charachter of the message isn't a number we're parked. | |
| if (!int.TryParse(message.Substring(1, message.Length - 1), out gate)) | |
| Console.WriteLine("Buggy finished parking."); | |
| Console.WriteLine($"Buggy: {currentBuggy}, At Gate: G{gate}, NumGanteries: {numGanteries}, Laps Left: {lapsLeft}"); | |
| Console.WriteLine($"Other buggy waiting at: " + (currentBuggy == 0 ? "Parking Lane" : "3rd Gantry")); | |
| if (CurrentChallege == Challege.Bronze) | |
| { | |
| if (numGanteries < 5) | |
| port.WriteLine("0GO"); | |
| else if (numGanteries == 5) | |
| { | |
| Console.WriteLine("telling buggy to park"); | |
| port.WriteLine("0PARKL"); | |
| isDone = true; | |
| } | |
| } | |
| else // Gold or Silver challeges. | |
| { | |
| if (numGanteries == 3) | |
| { | |
| lapsLeft--; | |
| // switch buggies | |
| currentBuggy = currentBuggy == 0 ? 1 : 0; | |
| numGanteries = 0; | |
| Console.WriteLine($"Changing to buggy {currentBuggy}"); | |
| if (lapsLeft > 0) | |
| port.WriteLine($"{currentBuggy}GO"); | |
| else | |
| { | |
| Console.WriteLine("--------- DONE ---------"); | |
| isDone = true; | |
| } | |
| } | |
| // Park buggy 1 on every lap, and buggy 0 on the last lap. | |
| else if (numGanteries == 2 && currentBuggy == 1) | |
| { | |
| //Console.WriteLine("telling buggy to park"); | |
| port.WriteLine("1PARKR"); | |
| } | |
| else if (numGanteries == 2 && currentBuggy == 0 && lapsLeft == 1) | |
| { | |
| port.WriteLine("0PARKL"); | |
| } | |
| else | |
| { | |
| // No gate of interest, tell buggy to go. | |
| port.WriteLine($"{currentBuggy}GO"); | |
| } | |
| } | |
| } | |
| messages.Add("Recieved: " + message); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment