Пример #1
0
        void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte c;

            while (serial.BytesToRead > 0)
            {
                bool acceptByte = false;

                c = (byte)serial.ReadByte();

                switch (serialState)
                {
                    case SerialReceiverState.USART_RECEIVER_IDLE_RX_STATE:
                        if (APP_MAGIC_SYMBOL == c)
                        {
                            serialState = SerialReceiverState.USART_RECEIVER_SOF_RX_STATE;
                            currentMessage.Clear();
                            CheckSum = 0;
                        }
                        break;

                    case SerialReceiverState.USART_RECEIVER_SOF_RX_STATE:
                        if (SOF[1] == c)
                        {
                            serialState = SerialReceiverState.USART_RECEIVER_DATA_RX_STATE;
                        }
                        else
                        {
                            serialState = SerialReceiverState.USART_RECEIVER_IDLE_RX_STATE;
                        }
                        break;

                    case SerialReceiverState.USART_RECEIVER_DATA_RX_STATE:
                        if (APP_MAGIC_SYMBOL == c)
                        {
                            serialState = SerialReceiverState.USART_RECEIVER_MAGIC_RX_STATE;
                        }
                        else
                        {
                            acceptByte = true;
                        }
                        break;

                    case SerialReceiverState.USART_RECEIVER_MAGIC_RX_STATE:
                        if (APP_MAGIC_SYMBOL == c)
                        {
                            serialState = SerialReceiverState.USART_RECEIVER_DATA_RX_STATE;
                            acceptByte = true;
                        }
                        else if (EOF[1] == c)
                        {
                            serialState = SerialReceiverState.USART_RECEIVER_EOF_RX_STATE;
                        }
                        else
                        {
                            serialState = SerialReceiverState.USART_RECEIVER_IDLE_RX_STATE;
                            PrintMessage("TRAMA DEFECTUOSA");
                        }
                        break;

                    case SerialReceiverState.USART_RECEIVER_EOF_RX_STATE:
                        if (CheckSum == c)
                        {
                            serialState = SerialReceiverState.USART_RECEIVER_IDLE_RX_STATE;
                            InputMessage inputMessage = new InputMessage();
                            inputMessage.FromBinary(currentMessage.ToArray());

                            if (inputMessage.Content.Args != null)
                                OnOperationReceived(inputMessage);
                        }
                        else
                        {
                            serialState = SerialReceiverState.USART_RECEIVER_IDLE_RX_STATE;
                            PrintMessage("CHECKSUM INCORRECTO");
                        }
                        break;

                    default:
                        break;
                }

                CheckSum += c;

                if (acceptByte)
                {
                    currentMessage.Add(c);
                }
            }
        }
Пример #2
0
        void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte c;

            while (serial.BytesToRead > 0)
            {
                bool acceptByte = false;

                c = (byte)serial.ReadByte();

                switch (serialState)
                {
                case SerialReceiverState.USART_RECEIVER_IDLE_RX_STATE:
                    if (APP_MAGIC_SYMBOL == c)
                    {
                        serialState = SerialReceiverState.USART_RECEIVER_SOF_RX_STATE;
                        currentMessage.Clear();
                        CheckSum = 0;
                    }
                    break;

                case SerialReceiverState.USART_RECEIVER_SOF_RX_STATE:
                    if (SOF[1] == c)
                    {
                        serialState = SerialReceiverState.USART_RECEIVER_DATA_RX_STATE;
                    }
                    else
                    {
                        serialState = SerialReceiverState.USART_RECEIVER_IDLE_RX_STATE;
                    }
                    break;

                case SerialReceiverState.USART_RECEIVER_DATA_RX_STATE:
                    if (APP_MAGIC_SYMBOL == c)
                    {
                        serialState = SerialReceiverState.USART_RECEIVER_MAGIC_RX_STATE;
                    }
                    else
                    {
                        acceptByte = true;
                    }
                    break;

                case SerialReceiverState.USART_RECEIVER_MAGIC_RX_STATE:
                    if (APP_MAGIC_SYMBOL == c)
                    {
                        serialState = SerialReceiverState.USART_RECEIVER_DATA_RX_STATE;
                        acceptByte  = true;
                    }
                    else if (EOF[1] == c)
                    {
                        serialState = SerialReceiverState.USART_RECEIVER_EOF_RX_STATE;
                    }
                    else
                    {
                        serialState = SerialReceiverState.USART_RECEIVER_IDLE_RX_STATE;
                        PrintMessage("TRAMA DEFECTUOSA");
                    }
                    break;

                case SerialReceiverState.USART_RECEIVER_EOF_RX_STATE:
                    if (CheckSum == c)
                    {
                        serialState = SerialReceiverState.USART_RECEIVER_IDLE_RX_STATE;
                        InputMessage inputMessage = new InputMessage();
                        inputMessage.FromBinary(currentMessage.ToArray());

                        if (inputMessage.Content.Args != null)
                        {
                            OnOperationReceived(inputMessage);
                        }
                    }
                    else
                    {
                        serialState = SerialReceiverState.USART_RECEIVER_IDLE_RX_STATE;
                        PrintMessage("CHECKSUM INCORRECTO");
                    }
                    break;

                default:
                    break;
                }

                CheckSum += c;

                if (acceptByte)
                {
                    currentMessage.Add(c);
                }
            }
        }
Пример #3
0
 void OnOperationReceived(InputMessage oper)
 {
     if (OperationReceived != null)
         OperationReceived(oper, null);
 }
Пример #4
0
        void Form1_OperationReceived(object sender, EventArgs e)
        {
            InputMessage message   = (sender as InputMessage);
            Operation    operation = message.Content;

            string msgToPrint = "";

            if (operation.OpCode == Operation.OPCodes.ConfigWriteResponse)
            {
                if (configWriteBuffer.Count > 0)
                {
                    if (operation.Args[0] == configWriteBuffer[0].Args[0])
                    {
                        if (operation.Args[1] == (byte)ConfigErrorCodes.OK)
                        {
                            configWriteBuffer.RemoveAt(0);
                            if (configWriteBuffer.Count > 0)
                            {
                                SendOperation(configWriteBuffer[0]);
                            }
                            else
                            {
                                msgToPrint = String.Format("CONFIG. UPDATE DONE TO 0x{0:X4}", operation.SourceAddress);
                            }
                        }
                        else
                        {
                            configWriteBuffer.Clear();
                            msgToPrint = "CONFIG. ERROR CODE: " + Enum.GetName(typeof(ConfigErrorCodes), (object)operation.Args[1]);
                        }
                    }
                    else
                    {
                        int rcvFragment      = operation.Args[0] & 0x0F;
                        int rcvTotalFragment = (operation.Args[0] & 0xF0) >> 4;
                        int expFragment      = configWriteBuffer[0].Args[0] & 0x0F;
                        int expTotalFragment = (configWriteBuffer[0].Args[0] & 0xF0) >> 4;

                        msgToPrint = String.Format("CONFIG. INVALID PROTOCOL: RECEIVED({0}/{1}) EXPECTED({2}/{3})",
                                                   rcvFragment, rcvTotalFragment, expFragment, expTotalFragment);

                        configWriteBuffer.Clear();
                    }
                }
            }
            else if (operation.OpCode == Operation.OPCodes.TemperatureReadResponse || operation.OpCode == Operation.OPCodes.HumidityReadResponse)
            {
                if (operation.Args[2] == 0xFF)
                {
                    msgToPrint = String.Format("SENSOR 0x{1:X4} FROM 0x{0:X4} UNKNOWN",
                                               operation.SourceAddress,
                                               ((ushort)operation.Args[1]) << 8 | operation.Args[0]);
                }
                else
                {
                    string baseStr = operation.OpCode == Operation.OPCodes.TemperatureReadResponse ?
                                     "TEMPERATURE RECEIVED FROM 0x{0:X4} SENSOR {1}: {2}ºC" :
                                     "HUMIDITY RECEIVED FROM 0x{0:X4} SENSOR {1}: {2}%";

                    msgToPrint = String.Format(baseStr,
                                               operation.SourceAddress,
                                               (((ushort)operation.Args[1]) << 8 | operation.Args[0]),
                                               operation.Args[2]);
                }
            }
            else if (operation.OpCode == Operation.OPCodes.PresenceReadResponse)
            {
                string valStr = (operation.Args[2] == 0xFF) ? "UNKNOWN" : (operation.Args[2] != 0) ? "DETECTED" : "NOT DETECTED";

                msgToPrint = String.Format("PRESENCE READ FROM 0x{0:X4}: 0x{1:X4} {2}",
                                           operation.SourceAddress,
                                           ((ushort)operation.Args[1]) << 8 | operation.Args[0],
                                           valStr);
            }
            else if (operation.OpCode == Operation.OPCodes.ConfigChecksumResponse)
            {
                msgToPrint = String.Format("CHECKSUM RECEIVED FROM 0x{0:X4}: 0x{1:X4}",
                                           operation.SourceAddress,
                                           ((ushort)operation.Args[1]) << 8 | operation.Args[0]);
            }
            else if (operation.OpCode == Operation.OPCodes.LogicReadResponse)
            {
                msgToPrint = String.Format("LOGIC READ FROM 0x{0:X4}: 0x{1:X4} {2}",
                                           operation.SourceAddress,
                                           ((ushort)operation.Args[1]) << 8 | operation.Args[0],
                                           (operation.Args[2] == 0xFF) ? "UNKNOWN" : (operation.Args[2] != 0).ToString());
            }
            else if (operation.OpCode == Operation.OPCodes.DimmerReadResponse)
            {
                msgToPrint = String.Format("DIMMER READ FROM 0x{0:X4}: 0x{1:X4} {2}",
                                           operation.SourceAddress,
                                           ((ushort)operation.Args[1]) << 8 | operation.Args[0],
                                           (operation.Args[2] == 0xFF) ? "UNKNOWN" : operation.Args[2].ToString());
            }
            else if (operation.OpCode == Operation.OPCodes.DateTimeRead) //TIME SYNC REQUEST!
            {
                SendDateTime(operation.SourceAddress, false);
                msgToPrint = String.Format("TIME SYNC RESPONSE SENT TO 0x{0:X4}", operation.SourceAddress);
            }
            else if (operation.OpCode == Operation.OPCodes.DateTimeReadResponse)
            {
                string DateTimeStr = string.Empty;

                if (operation.Args[0] != 0xFF)//Valid DATETIME
                {
                    string DateStr = String.Format("{0}  {1:d2}/{2:d2}/{3:d4}",
                                                   Enum.GetName(typeof(DayOfWeeks), (object)operation.Args[0]),
                                                   operation.Args[1],
                                                   operation.Args[2],
                                                   ((ushort)operation.Args[4]) << 8 | operation.Args[3]);

                    string TimeStr = String.Format(" {0:d2}:{1:d2}:{2:d2}",
                                                   operation.Args[5],
                                                   operation.Args[6],
                                                   operation.Args[7]);

                    DateTimeStr = DateStr + " " + TimeStr;
                }
                else
                {
                    DateTimeStr = "INVALID";
                }
                msgToPrint = String.Format("DATETIME READ FROM 0x{0:X4} -> ", operation.SourceAddress) + DateTimeStr;
            }
            else if (operation.OpCode == Operation.OPCodes.MacReadResponse)
            {
                msgToPrint = String.Format("MAC FROM 0x{0:X4} -> 0x{1:X2}-0x{2:X2}-0x{3:X2}-0x{4:X2}-0x{5:X2}-0x{6:X2}",
                                           operation.SourceAddress,
                                           operation.Args[0],
                                           operation.Args[1],
                                           operation.Args[2],
                                           operation.Args[3],
                                           operation.Args[4],
                                           operation.Args[5]);
            }
            else if (operation.OpCode == Operation.OPCodes.FirmwareVersionReadResponse)
            {
                msgToPrint = String.Format("FIRMWARE VERSION FROM 0x{0:X4} -> {1}",
                                           operation.SourceAddress,
                                           operation.Args[0]);
            }
            else if (operation.OpCode == Operation.OPCodes.BaseModelReadResponse)
            {
                msgToPrint = String.Format("BASE MODEL FROM 0x{0:X4} -> {1}",
                                           operation.SourceAddress,
                                           operation.Args[0]);
            }
            else if (operation.OpCode == Operation.OPCodes.ShieldModelReadResponse)
            {
                msgToPrint = String.Format("SHIELD MODEL FROM 0x{0:X4} -> {1}",
                                           operation.SourceAddress,
                                           operation.Args[0]);
            }
            else if (operation.OpCode == Operation.OPCodes.NextHopReadResponse)
            {
                msgToPrint = String.Format("NEXTHOP READ FROM 0x{0:X4} -> ADDRESS: 0x{1:X4}   NEXTHOP: 0x{2:X4}   SCORE: {3}   LQI: {4}",
                                           operation.SourceAddress,
                                           ((ushort)operation.Args[1] << 8 | operation.Args[0]),
                                           ((ushort)operation.Args[3] << 8 | operation.Args[2]),
                                           operation.Args[4],
                                           operation.Args[5]);
            }
            else if (operation.OpCode == Operation.OPCodes.PingResponse)
            {
                msgToPrint = String.Format("PING RESPONSE RECEIVED FROM 0x{0:X4}", operation.SourceAddress);
            }
            else if (operation.OpCode == Operation.OPCodes.JoinRequest)
            {
                msgToPrint = String.Format("JOIN REQUEST RECEIVED FROM 0x{0:X4}", operation.SourceAddress);
                SendJoinRequestResponse(operation.SourceAddress);
            }
            else if (operation.OpCode == Operation.OPCodes.JoinAbort)
            {
                msgToPrint = String.Format("JOIN ABORT RECEIVED FROM 0x{0:X4} -> NUMBER OF RESPONSES: {1}", operation.SourceAddress, operation.Args[0]);
            }
            else if (operation.OpCode == Operation.OPCodes.WakeUp)
            {
                msgToPrint = String.Format("WAKEUP RECEIVED FROM 0x{0:X4}", operation.SourceAddress);
            }
            else if (operation.OpCode == Operation.OPCodes.JoinAccept)
            {
                byte[] macAddress = operation.Args.Take(6).ToArray();
                byte[] aesKey     = operation.Args.Skip(6).ToArray();

                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("JOIN ACCEPT RECEIVED FROM 0x{0:X4} -> MAC: ", operation.SourceAddress);

                foreach (byte b in macAddress)
                {
                    sb.AppendFormat("0x{0:X2} ", b);
                }

                sb.Append("  AES-KEY: ");

                foreach (byte b in aesKey)
                {
                    sb.AppendFormat("0x{0:X2} ", b);
                }

                msgToPrint = sb.ToString();

                SendJoinAcceptResponse(operation.SourceAddress, macAddress, aesKey);
            }
            else if (operation.OpCode == Operation.OPCodes.ConfigReadResponse)
            {
                int fragment      = operation.Args[0] & 0x0F;
                int totalFragment = (operation.Args[0] & 0xF0) >> 4;

                Operation lastOp = null;
                if (configReadBuffer.Count > 0)
                {
                    lastOp = configReadBuffer.Last();
                }

                if (lastOp == null || (lastOp != null && operation.Args[0] == lastOp.Args[0] + 1))
                {
                    if (operation.Args[0] == 0 && operation.Args[1] == 0) //ERROR
                    {
                        msgToPrint = String.Format("CONFIG READ RESPONSE FROM 0x{0:X4} -> BUSY SENDING CONFIG STATE",
                                                   operation.SourceAddress);
                    }
                    else //OK
                    {
                        msgToPrint = String.Format("CONFIG READ RESPONSE FROM 0x{0:X4} -> {1}/{2} OK",
                                                   operation.SourceAddress,
                                                   fragment,
                                                   totalFragment);

                        configReadBuffer.Add(operation);
                        SendOperation(new Operation()
                        {
                            SourceAddress = 0x00, DestinationAddress = DestinationAddress, OpCode = Operation.OPCodes.ConfigReadConfirmation, Args = new byte[] { operation.Args[0], 0x00 }
                        });

                        if (fragment == totalFragment)
                        {
                            SaveConfigFile();
                        }
                    }
                }
                else
                {
                    msgToPrint = String.Format("CONFIG READ RESPONSE FROM 0x{0:X4} -> {1}/{2} ERROR",
                                               operation.SourceAddress,
                                               fragment,
                                               totalFragment);

                    //Send error code back
                    SendOperation(new Operation()
                    {
                        SourceAddress = 0x00, DestinationAddress = DestinationAddress, OpCode = Operation.OPCodes.ConfigReadConfirmation, Args = new byte[] { operation.Args[0], 0x01 }
                    });
                }
            }
            else
            {
                msgToPrint = operation.ToString();
            }

            if (!string.IsNullOrWhiteSpace(msgToPrint))
            {
                PrintMessage(String.Format("RSSI: {0} NEXTHOP: 0x{1:X4}     {2}", message.RSSI, message.NextHop, msgToPrint));
            }
        }