/// <summary>
 /// Disposes the port.
 /// </summary>
 public void Dispose()
 {
     this.port.Dispose();
     this.message  = PowerLineModemMessage.Message.UserResetDetected;
     this.port     = null;
     this.readData = 0;
     this.receivedAck.Dispose();
     this.receivedAck     = null;
     this.sending         = null;
     this.sendingResponse = PowerLineModemMessage.MessageResponse.Unknown;
     this.state           = ReadState.START;
     this.data            = null;
     this.depth           = 0;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Reads all the devices from the PLM and stores the mapping locally.  Along with the
        /// basic info and config of the plm.
        /// </summary>
        public bool Startup()
        {
            GetInfo info = new GetInfo();

            PowerLineModemMessage.MessageResponse response = port.SendCommand(info);
            if (response == PowerLineModemMessage.MessageResponse.Ack)
            {
                plm = new PowerLineModule(this, info.Id, info.Category, info.Subcategory, info.FirmwareVersion);
                // Ask for the config and info about the modem.
                GetConfiguration config = new GetConfiguration();
                response = port.SendCommand(config);
                if (response == PowerLineModemMessage.MessageResponse.Ack)
                {
                    plm.Config = config.Configuration;
                    this.ReadAllLinks();
                    this.SetupAllDevices();
                    return(true);
                }
            }
            return(false);
        }
        private void ReadMessages(byte[] input, int size)
        {
            List <PowerLineModemMessage> receivedMessages = new List <PowerLineModemMessage>();

            lock (port)
            {
                string strDebug = "Input: [";
                // First we look for a 0x2, which they all start with.
                for (int i = 0; i < size; i++)
                {
                    int data = input[i];
                    strDebug += String.Format("{0:x2}-", data, readData, (this.data == null ? 0 :this.data.Length), state.ToString());
                    switch (state)
                    {
                    case ReadState.START:
                        if (data == START_OF_MESSAGE)
                        {
                            state = ReadState.MESSAGE_TYPE;
                        }
                        break;

                    case ReadState.MESSAGE_TYPE:
                        // This next one is the message type.
                        message   = (PowerLineModemMessage.Message)data;
                        state     = ReadState.MESSAGE_DATA;
                        this.data = new byte[MessageFactory.SizeOfMessage(message, sending)];
                        readData  = 0;
                        break;

                    case ReadState.MESSAGE_DATA:
                        if (readData < this.data.Length)
                        {
                            this.data[readData++] = (byte)data;
                        }
                        if (readData >= this.data.Length)
                        {
                            // If this is a response, update the request with the response data.
                            if (sending != null && message == sending.MessageType)
                            {
                                sendingResponse = sending.VerifyResponse(this.data);
                                sending         = null;
                                receivedAck.Set();
                            }
                            else
                            {
                                // Create the received message.
                                PowerLineModemMessage packet = MessageFactory.GetReceivedMessage(message, this.data);
                                if (packet != null)
                                {
                                    receivedMessages.Add(packet);
                                }
                            }
                            state = ReadState.START;
                        }
                        break;
                    }
                }
                log.Debug(strDebug + "]");
            }
            if (ReceivedMessage != null)
            {
                // Switch context so we don't end up deadlocked.
                ThreadPool.QueueUserWorkItem(delegate
                {
                    foreach (PowerLineModemMessage mess in receivedMessages)
                    {
                        ReceivedMessage(this, new RecievedMessageEventArgs(mess));
                    }
                });
            }
        }