Example #1
0
        void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            while (serialPort.BytesToRead > 0)
            {
                if (currentCommand == null)
                {
                    var code = (byte)serialPort.ReadByte();
                    if (!pendingCommands.TryGetValue(code, out currentCommand))
                    {
                        throw new InvalidOperationException("Unexpected command response.");
                    }
                }

                var response  = currentCommand.Response;
                var bytesRead = serialPort.Read(response, currentCommand.Offset, response.Length - currentCommand.Offset);
                currentCommand.Offset += bytesRead;

                if (currentCommand.Offset == response.Length)
                {
                    var  userState = currentCommand.UserState;
                    byte code      = currentCommand.Code;
                    lock (pendingCommands)
                    {
                        pendingCommands.Remove(currentCommand.Code);
                    }

                    currentCommand = null;
                    OnCommandCompleted(new CommandCompletedEventArgs(code, response, null, false, userState));
                }
            }
        }
Example #2
0
        public void CommandAsync(byte[] command, int responseSize, object userState)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            if (command.Length < 1)
            {
                throw new ArgumentException("Command bytes must not be empty.");
            }

            var code = command[0];

            var response       = new byte[responseSize];
            var pendingCommand = new PendingCommand(code, response, userState);

            lock (pendingCommands)
            {
                if (pendingCommands.Count > 0)
                {
                    throw new TimeoutException("Communication manager adding a new command before response :" + "Trying to place a request a " + code + " when there is a " + pendingCommands.First().Key + " on the response queue");
                }
                pendingCommands.Add(code, pendingCommand);
            }

            serialPort.Write(command, 0, command.Length);
        }