Exemple #1
0
        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}"));
            }
        }
Exemple #2
0
 public void Disconnect()
 {
     if (_port.IsOpen)
     {
         ConsoleOutput.Info("SERIAL: Port is open, sending shutdown command [:Qq#]");
         _port.Write(":Qq#");
         ConsoleOutput.Info("SERIAL: Closing port...");
         _port.Close();
         _port = null;
         ConsoleOutput.Success("SERIAL: Disconnected...");
     }
 }
Exemple #3
0
        public SerialCommunicationHandler(string comPort)
        {
            ConsoleOutput.Info($"COMMFACTORY: Creating Serial handler on {comPort} at {_baudRate} baud...");
            ConsoleOutput.Info($"COMMFACTORY: Read Timeout {_readTimeout}");
            ConsoleOutput.Info($"COMMFACTORY: Write Timeout {_writeTimeout}");

            _portName          = comPort;
            _port              = new SerialPort(comPort);
            _port.BaudRate     = _baudRate;
            _port.DtrEnable    = false;
            _port.ReadTimeout  = _readTimeout;
            _port.WriteTimeout = _writeTimeout;
        }
        static List <string> DiscoverDevices()
        {
            ConsoleOutput.Info("COMMFACTORY: Device Discovery initiated.");
            ConsoleOutput.Info("COMMFACTORY: Checking Serial ports....");

            List <string> _available = new List <string>();

            foreach (var port in SerialPort.GetPortNames())
            {
                Console.WriteLine("COMMFACTORY: Found Serial port [{0}]", port);
                _available.Add("Serial : " + port);
            }

            return(_available);
        }
        static SerialCommunicationHandler ConnectToDevice(string device)
        {
            if (_commHandler != null && _commHandler.Connected)
            {
                ConsoleOutput.Warning($"COMMFACTORY: Already connected to {device}...");
                return(null);
            }

            ConsoleOutput.Info($"COMMFACTORY: Attempting to connect to device {device}...");
            if (device.StartsWith("Serial : "))
            {
                string comPort = device.Substring("Serial : ".Length);
                return(new SerialCommunicationHandler(comPort));
            }
            return(null);
        }
Exemple #6
0
        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> StartTest()
        {
            Dictionary <string, string> keyValuePairs = new Dictionary <string, string>();

            keyValuePairs.Add("GVP#,#", "Product name");
            keyValuePairs.Add("GVN#,#", "Firmware version");
            keyValuePairs.Add("XGM#,#", "Mount configuration");
            keyValuePairs.Add("Gt#,#", "Site Latitude");
            keyValuePairs.Add("Gg#,#", "Site Longitude");
            keyValuePairs.Add("XGR#,#", "RA Steps");
            keyValuePairs.Add("XGD#,#", "DEC Steps");
            if (_firmwareVersion >= 10869)
            {
                keyValuePairs.Add("XGDL#,#", "DEC Limits");
            }
            keyValuePairs.Add("XGT#,#", "Tracking speed");
            keyValuePairs.Add("XGH#,#", "HA");
            keyValuePairs.Add("XGL#,#", "LST");
            keyValuePairs.Add("GC#,#", "Local date");
            keyValuePairs.Add("GL#,#", "Local time");
            keyValuePairs.Add("GG#,#", "UTC Offset");
            keyValuePairs.Add("XGN#,#", "Network settings");

            List <CommandResponse> replys = new List <CommandResponse>();

            foreach (var cmd in keyValuePairs)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("----- {0} -----\r", cmd.Value);
                Console.ResetColor();
                var result = await SendCommand(cmd.Key);

                if (!result.Success)
                {
                    Console.WriteLine("Press any key to return...");
                    Console.ReadKey();
                    return(false);
                }
                replys.Add(result);
            }

            // Disconnect
            _commHandler.Disconnect();
            _commHandler = null;

            // Print summery
            int cnt = 0;

            ConsoleOutput.Info("--------------------------------------- SUMMARY -----------------------------------------------------------\r");
            foreach (var cmd in keyValuePairs)
            {
                ConsoleOutput.Info(string.Format("| {0} | {1} |\r", cmd.Value.PadLeft(30), replys[cnt].Data.PadRight(70)));
                cnt++;
            }
            ConsoleOutput.Info("-----------------------------------------------------------------------------------------------------------\r");

            ConsoleOutput.Info("Press any key to return...");
            Console.ReadKey();

            return(true);
        }