private async Task <CommandResponse> SendCommand(string command, ResponseType needsResponse) { if (await EnsurePortIsOpen()) { _port.DiscardInBuffer(); _port.DiscardOutBuffer(); requestIndex++; try { ConsoleOutput.Info(string.Format("[{0:0000}] SERIAL: [{1}] Sending command", requestIndex, command)); _port.Write(command); } catch (Exception ex) { ConsoleOutput.Error(string.Format("[{0:0000}] SERIAL: [{1}] Failed to send command. {2}", requestIndex, command, ex.Message)); return(new CommandResponse(string.Empty, false, $"Unable to write to {_portName}. " + ex.Message)); } try { switch (needsResponse) { case ResponseType.NoResponse: { ConsoleOutput.Info(string.Format("[{0:0000}] SERIAL: [{1}] No response needed for command", requestIndex, command)); return(new CommandResponse(string.Empty, true)); } case ResponseType.DigitResponse: { ConsoleOutput.Info(string.Format("[{0:0000}] SERIAL: [{1}] Expecting single digit response for command, waiting...", requestIndex, command)); string response = new string((char)_port.ReadChar(), 1); ConsoleOutput.Success(string.Format("[{0:0000}] SERIAL: [{1}] Received single digit response '{2}'", requestIndex, command, response)); return(new CommandResponse(response, true)); } case ResponseType.FullResponse: { ConsoleOutput.Info(string.Format("[{0:0000}] SERIAL: [{1}] Expecting #-delimited response for Command, waiting...", requestIndex, command)); string response = _port.ReadTo("#"); ConsoleOutput.Success(string.Format("[{0:0000}] SERIAL: [{1}] Received response '{2}'", requestIndex, command, response)); return(new CommandResponse(response, true)); } } } catch (Exception ex) { ConsoleOutput.Error(string.Format("[{0:0000}] SERIAL: [{1}] Failed to receive response to command. {2}", requestIndex, command, ex.Message)); return(new CommandResponse(string.Empty, false, $"Unable to read response to {command} from {_portName}. {ex.Message}")); } return(new CommandResponse(string.Empty, false, "Something weird going on...")); } else { ConsoleOutput.Error(string.Format("[{0:0000}] SERIAL: Failed to open port {1}", requestIndex, _portName)); return(new CommandResponse(string.Empty, false, $"Unable to open {_portName}")); } }
static async Task <bool> TestGPS() { Console.Clear(); ConsoleOutput.Logo(); Console.WriteLine("********* GPS TEST ***************\r"); Console.WriteLine("* Battery may need to be charged *\r"); Console.WriteLine("* first time GPS is used ~30min *\r"); Console.WriteLine("* 0 = No satelites found *\r"); Console.WriteLine("* 1 = Satelites found *\r"); Console.WriteLine("**********************************\r"); Console.WriteLine("Press ESC to stop"); int outputStart = Console.CursorTop + 1; do { while (!Console.KeyAvailable) { Console.CursorTop = outputStart; Console.CursorLeft = 0; ConsoleOutput.ClearRestOfScreen(); var result = await SendCommand(":gT100#,n"); if (result.Data == "1") { result = await SendCommand(":Gt#,#"); var latitudeArray = result.Data.Split('*'); float latitude = float.Parse(latitudeArray[0], _oatCulture) + (float.Parse(latitudeArray[1], _oatCulture) / 60.0f); await Task.Delay(250); result = await SendCommand(":Gg#,#"); var longitudeArray = result.Data.Split('*'); float longitude = float.Parse(longitudeArray[0], _oatCulture) + (float.Parse(longitudeArray[1], _oatCulture) / 60.0f); if (longitude > 180) { longitude -= 360; } await Task.Delay(250); ConsoleOutput.Error(string.Format("Lat: {0}", latitude)); ConsoleOutput.Error(string.Format("Lon: {0}", longitude)); } //Console.WriteLine("ESC - Exit\n"); await Task.Delay(1000); } } while (Console.ReadKey(true).Key != ConsoleKey.Escape); _commHandler.Disconnect(); _commHandler = null; Console.ReadKey(); return(true); }
private async Task <bool> EnsurePortIsOpen() { if (!_port.IsOpen) { try { ConsoleOutput.Info(string.Format("SERIAL: Port {0} is not open, attempting to open...", _portName)); _port.Open(); ConsoleOutput.Info(string.Format("SERIAL: Checking if buffer contains data", _portName)); string preCheck = _port.ReadExisting(); if (!string.IsNullOrEmpty(preCheck)) { ConsoleOutput.Error(string.Format("SERIAL: Possible problem, data already in buffer: {0}", preCheck)); ConsoleOutput.Warning("SERIAL: Flushing serial buffers..."); _port.DiscardInBuffer(); _port.DiscardOutBuffer(); } else { ConsoleOutput.Success("SERIAL: No exising serial data in buffer, all good..."); } await Task.Delay(750); // Arduino resets on connection. Give it time to start up. ConsoleOutput.Info("SERIAL: Port is open, sending initial [:I#] command.."); _port.Write(":I#"); ConsoleOutput.Success("SERIAL: Connected!"); return(true); } catch (Exception ex) { ConsoleOutput.Error(string.Format("SERIAL: Failed to open the port. {0}", ex.Message)); return(false); } } ConsoleOutput.Warning("SERIAL: Flushing serial buffers..."); _port.DiscardInBuffer(); _port.DiscardOutBuffer(); Console.WriteLine("SERIAL: Port {0} is open, continuing...", _portName); return(true); }
static async Task <bool> CreateCommHandler(string device) { var tempCon = ConnectToDevice(device); if (tempCon == null) { return(true); } _commHandler = tempCon; _commHandler.ReadTimeout = _readTimeout; _commHandler.WriteTimeout = _writeTimeout; _commHandler.BaudRate = _baudRate; await SendCommand(":I#,"); var versionResponse = await SendCommand(":GVN#,#"); _firmwareVersion = 1000; if (versionResponse.Success) { _oatFwVersion = versionResponse.Data; var versionNumbers = _oatFwVersion.Substring(1).Split(".".ToCharArray()); if (versionNumbers.Length != 3) { ConsoleOutput.Error(string.Format("Unrecognizable firmware version '{0}'", _oatFwVersion)); } else { try { _firmwareVersion = long.Parse(versionNumbers[0]) * 10000L + long.Parse(versionNumbers[1]) * 100L + long.Parse(versionNumbers[2]); } catch { ConsoleOutput.Error(string.Format("Unparseable firmware version '{0}'", _oatFwVersion)); } } } return(true); }