Exemplo n.º 1
0
        public async Task <string> Execute(ICommand command, int timeout = 500)
        {
            byte[] bytes = null;
            try
            {
                await port.Send(command.GetKey());

                bytes = await port.Receive(timeout);
            }
            catch (Exception exc)
            {
                throw exc;
            }

            var response = System.Text.Encoding.UTF8.GetString(bytes);

            if (command.IsExpected(response))
            {
                return(response.Replace("\r\n", ""));
            }
            else
            {
                throw new NotValidResponseException(response);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Receive bytes from the serial port and relay them to the client.
        /// </summary>
        private async Task Receive(HttpListenerContext context)
        {
            byte[] buffer = new byte[10 * 1000];
            int    length = await port.Receive(buffer, 0, buffer.Length);

            string responseHex = buffer.ToHex(length);

            this.logger.AddUserMessage("HTTP response: " + responseHex);

            var writer = new StreamWriter(context.Response.OutputStream);
            await writer.WriteLineAsync(responseHex);

            await writer.FlushAsync();
        }
Exemplo n.º 3
0
 byte[] Receive(out IPEndPoint receivedEndpoint)
 {
     var(data, hostEndpoint) = port.Receive();
     receivedEndpoint        = hostEndpoint;
     return(data);
 }