Beispiel #1
0
        void ReadThread()
        {
            List <byte> buffer = new List <byte>();

            while (_readThread != null)
            {
                while (_innerPort.BytesToRead > 0)
                {
                    byte newByte = (byte)_innerPort.ReadByte();

                    if (newByte == 0xFF)
                    {
                        bool matchesCurrent;

                        //end byte.  queue up message
                        var response = ViscaResponse.ProcessResponse(buffer.ToArray(), CurrentCommand, out matchesCurrent);

                        if (matchesCurrent)
                        {
                            CurrentCommand = null;
                        }

                        lock (_responseQueue) _responseQueue.Enqueue(response);

                        buffer.Clear();
                    }
                    else
                    {
                        buffer.Add(newByte);
                    }
                }

                Thread.Sleep(10);
            }
        }
Beispiel #2
0
        public void SendCommand(ViscaCommand cmd)
        {
            if (CommandInProcess)
            {
                throw new InvalidOperationException();
            }

            CurrentCommand = cmd;

            var msgs = cmd.GetCommandMsgs();

            foreach (OutMessage msg in msgs)
            {
                _innerPort.Write(msg.Data, 0, msg.Data.Length);

                while (CurrentCommand != null)
                {
                    Thread.Sleep(10);
                }
            }
        }
        internal static ViscaResponse ProcessResponse(byte[] packet, ViscaCommand pendingCmd, out bool matchesPending)
        {
            byte src       = (byte)(packet[0] >> 4);
            byte socket    = (byte)(packet[1] & 0x0F);
            byte replyType = (byte)(packet[1] >> 4);

            matchesPending = false;

            if (packet.Last() != 0xFF)
            {
                throw new ArgumentException("Invalid Data:  Does not end with expected 0xFF");
            }

            ViscaResponse response = null;

            switch (replyType)
            {
            case 4:     //Ack
                if (pendingCmd == null)
                {
                    throw new InvalidOperationException();
                }

                response       = new Acknowledgement();
                matchesPending = true;
                break;

            case 5:                     //Completion (commands/inquiries)
                if (packet.Length == 3) //Command Complete
                {
                    response = new CommandComplete();
                }
                else
                {
                    matchesPending = true;
                    response       = (ViscaInquiryResponse)Activator.CreateInstance(pendingCmd.InquiryResponseType);
                }
                break;

            case 6:     //Error
                if (packet.Length != 4)
                {
                    throw new ArgumentException("Invalid Data: Error Packet not correct.");
                }
                response = new CommandError(packet[2]);

                if (pendingCmd != null)
                {
                    matchesPending = true;
                }

                break;

            default: throw new NotSupportedException();
            }

            response.SourceAddress = src;
            response.Socket        = socket;

            if (response is ViscaInquiryResponse)
            {
                //process only the meat data of the packet (strip out header and footer bytes)
                byte[] data = new byte[packet.Length - 3];

                Array.Copy(packet, 2, data, 0, packet.Length - 3);

                ((ViscaInquiryResponse)response).ProcessResponse_Internal(data);
            }

            return(response);
        }