public DeviceConfigurationResponse(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 \"device-config\" command wasn't at least 1 line long.");
            }

            var deviceNameLine = new KeyAndValue(response.Lines[0]);

            if (deviceNameLine.Key != "Device Name")
            {
                throw new ProtocolException("Key of Device Name line isn't right.");
            }
            this.DeviceName = deviceNameLine.Value;

            // Remaining lines are comma (and space) separated with the format:
            // Name, Address, Type
            // where Type is one of: input, output, analogInput, analogOutput

            var ioSignals = new List <IOSignal>();

            for (var i = 1; i < response.Lines.Count; i++)
            {
                ioSignals.Add(new IOSignal(response.Lines[i]));
            }
            this.IOSignals = ioSignals.AsReadOnly();
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
 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);
 }
Esempio n. 4
0
 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);
 }
Esempio n. 5
0
        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);
        }
 public ReadNumericResponse(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 n");
     }
     this.Value = Decimal.Parse(response.Lines[0]);
 }
Esempio n. 7
0
 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;
     }
 }
Esempio n. 8
0
        public RuntimeIdResponse(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 \"runtime-id\" command wasn't 1 lines long.");
            }

            var runningLine = new KeyAndValue(response.Lines[0]);

            if (runningLine.Key != "RuntimeId")
            {
                throw new ProtocolException("Key of RuntimeId line isn't right.");
            }
            this.RuntimeId = Guid.Parse(runningLine.Value);
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        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);
        }