public RuntimeVersionIdResponse(SendReceiveResult response)
        {
            if (response == null) throw new ArgumentNullException("response");
            if (!response.Success)
            {
                throw new ProtocolException(response.Error);
            }
            if (response.Lines.Count != 1)
            {
                throw new ProtocolException("Response to \"version-id\" command wasn't 1 lines long.");
            }

            var runningLine = new KeyAndValue(response.Lines[0]);
            if (runningLine.Key != "RuntimeVersionId") throw new ProtocolException("Key of RuntimeVersionId line isn't right.");
            this.RuntimeVersionId = Guid.Parse(runningLine.Value);
        }
        public CommandResponse(SendReceiveResult response)
        {
            if (response == null) throw new ArgumentNullException("response");
            if (!response.Success)
            {
                throw new ProtocolException(response.Error);
            }
            if (response.Lines.Count != 1)
            {
                throw new ProtocolException("Response to command wasn't 1 lines long.");
            }

            var successLine = new KeyAndValue(response.Lines[0]);
            if (successLine.Key != "Success") throw new ProtocolException("Key of Success line isn't right.");
            this.Success = bool.Parse(successLine.Value);
        }
 public ReadBooleanResponse(SendReceiveResult response)
 {
     if (response == null) throw new ArgumentNullException("response");
     if (!response.Success)
     {
         throw new ProtocolException(response.Error);
     }
     if (response.Lines.Count != 1)
     {
         throw new ProtocolException("Wrong number of lines in response to read b");
     }
     if (response.Lines[0] == "1")
     {
         this.Value = true;
     }
     else
     {
         this.Value = false;
     }
 }
        public DownloadOrPatchResponse(SendReceiveResult response)
        {
            if (response == null) throw new ArgumentNullException("response");
            if (!response.Success)
            {
                throw new ProtocolException(response.Error);
            }
            if (response.Lines.Count != 2)
            {
                throw new ProtocolException("Response to \"download\" or \"patch\" command wasn't 2 lines long.");
            }

            var successLine = new KeyAndValue(response.Lines[0]);
            if (successLine.Key != "Success") throw new ProtocolException("Key of Success line isn't right.");
            this.Success = bool.Parse(successLine.Value);

            var bytesLine = new KeyAndValue(response.Lines[1]);
            if (bytesLine.Key != "Bytes") throw new ProtocolException("Key of Bytes line isn't right.");
            this.Bytes = Int32.Parse(bytesLine.Value);
        }
        public InformationResponse(SendReceiveResult response)
        {
            if (response == null) throw new ArgumentNullException("response");
            if (!response.Success)
            {
                throw new ProtocolException(response.Error);
            }
            if (response.Lines.Count != 7)
            {
                throw new ProtocolException("Response to \"information\" command wasn't 7 lines long.");
            }
            this.RuntimeName = response.Lines[0];

            var protocolVersionLine = new KeyAndValue(response.Lines[1]);
            if (protocolVersionLine.Key != "Protocol Version") throw new ProtocolException("Key of Protocol Version line isn't right.");
            this.ProtocolVersion = protocolVersionLine.Value;

            var booleansLine = new KeyAndValue(response.Lines[2]);
            if (booleansLine.Key != "Booleans") throw new ProtocolException("Key of Booleans line isn't right.");
            this.Booleans = Int32.Parse(booleansLine.Value);

            var numericsLine = new KeyAndValue(response.Lines[3]);
            if (numericsLine.Key != "Numerics") throw new ProtocolException("Key of Numerics line isn't right.");
            this.Numerics = Int32.Parse(numericsLine.Value);

            var stringsLine = new KeyAndValue(response.Lines[4]);
            if (stringsLine.Key != "Strings") throw new ProtocolException("Key of Strings line isn't right.");
            this.Strings = Int32.Parse(stringsLine.Value);

            var maxProgramSizeLine = new KeyAndValue(response.Lines[5]);
            if (maxProgramSizeLine.Key != "Max Program Size") throw new ProtocolException("Key of Max Program Size line isn't right.");
            this.MaxProgramSize = Int32.Parse(maxProgramSizeLine.Value);

            var currentProgramSizeLine = new KeyAndValue(response.Lines[6]);
            if (currentProgramSizeLine.Key != "Current Program Size") throw new ProtocolException("Key of Current Program Size line isn't right.");
            this.CurrentProgramSize = Int32.Parse(currentProgramSizeLine.Value);
        }
 public LogItem(
     byte[] bytes,
     SendReceiveResult result)
 {
     this.bytes = bytes;
     this.result = result;
     Console.WriteLine("Sent: {0} bytes, received success: {1}, lines: {2}",
         bytes.Length, result.Success, result.Lines.Count);
 }
 public LogItem(
     string send,
     SendReceiveResult result)
 {
     this.send = send;
     this.result = result;
     Console.WriteLine("Sent: {0}, received success: {1}, lines: {2}",
         send, result.Success, result.Lines.Count);
 }
 private SendReceiveResult sendAndReceive(string request)
 {
     SendReceiveResult result;
     try
     {
         result = this.sendAndReceive(serialPort => serialPort.WriteLine(request));
         Console.WriteLine("sent: " + request);
         if (result.Success)
         {
             foreach (var line in result.Lines)
             {
                 Console.WriteLine(line);
             }
         }
         else
         {
             Console.WriteLine(result.Error);
         }
     }
     catch (IOException ex)
     {
         result = new SendReceiveResult(
             success: false,
             error: ex.Message,
             lines: new List<string>());
     }
     catch (TimeoutException ex)
     {
         result = new SendReceiveResult(
             success: false,
             error: ex.Message,
             lines: new List<string>());
     }
     catch (UnauthorizedAccessException ex) // can happen when opening port
     {
         result = new SendReceiveResult(
             success: false,
             error: ex.Message,
             lines: new List<string>());
     }
     catch (InvalidOperationException ex) // can happen when serial port is closed
     {
         result = new SendReceiveResult(
             success: false,
             error: ex.Message,
             lines: new List<string>());
     }
     // for debugging:
     //var logItem = new LogItem(request, result);
     //lock (this.m_logItemsLock)
     //{
     //    this.m_logItems.Add(logItem);
     //}
     return result;
 }