Exemplo n.º 1
0
        protected void OnCommandReceived(ControllerEventArgs e)
        {
            var data = e.Data ?? "NULL";
            Debug.Print("Command Received: " + e.Command.ToString() + " " + data);

            ControllerEventHandler handler = CommandReceived;
            if(handler != null)
                handler(this, e);
        }
Exemplo n.º 2
0
        private void DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            var data = port.ReadByte();

            if((data & 0x80) != 0)
            {
                index = -1;
                var address = data & 0x7f;
                if(address == 0 || address == myAddress)
                {
                    port.Write("RECEIVING");
                    receiving = true;
                }
                else
                {
                    port.Write("IGNORING");
                    receiving = false;
                }

                return;
            }

            if(receiving)
            {
                if(index >= BufferSize)
                {
                    port.Write("BUFFER OVERFLOW!");
                    receiving = false;
                    return;
                }

                if(index < 0)
                {
                    command = (CommandType) (data);
                    port.Write("COMMAND RECEIVED: " + command);
                    index = 0;
                    return;

                }

                if(data == 0x00)
                {
                    var temp = new byte[index];
                    Array.Copy(buffer, temp, index);
                    var s = new string(Encoding.UTF8.GetChars(temp));
                    var args = new ControllerEventArgs {
                        Command = command,
                        Data = s
                    };
                    OnCommandReceived(args);
                    port.Write("DATA RECEIVED: " + s);
                    receiving = false;
                    return;
                }

                buffer[index++] = data;
            }
        }