Esempio n. 1
0
        public Protocol(ProtocolSettings protocolSettings, IConverseCheckSum16 converseCheckSum = null)
            : base(GlobalSettings.SystemServices, null)
        {
            ProtocolSettings = protocolSettings;
            Type = ProtocolSettings.Type;
            if (converseCheckSum == null)
            {
                if (ProtocolSettings.CheckSum == "ModbusCheckSum16" || ProtocolSettings.CheckSum == "ModbusCRC16")
                    Calculations = new Conversations.ModbusConverseCalculations();
                else if (ProtocolSettings.CheckSum == "GrowattCheckSum16")
                    Calculations = new Conversations.GrowattConverseCalculations();
                else if (ProtocolSettings.CheckSum == "CRC16")
                    Calculations = new Conversations.CRC16ConverseCalculations();
                else if (ProtocolSettings.CheckSum == "CheckSum8")
                    Calculations = new Conversations.FroniusConverseCalculations();
                else
                    Calculations = new Conversations.ConverseCalculations();

                LogMessage("Protocol", "Using Calculations: " + Calculations.GetType().ToString(), LogEntryType.DetailTrace);
            }
            else
                Calculations = converseCheckSum;
            LoadConverse();
        }
        private void LoadConverse()
        {
            String configFile = "";
            UsePortReader = false;
            EmulatorType = DeviceEmulator.EmulatorType.CMS;
            Converse = null;
            liveElement = null;
            rootElement = null;

            if (EmulatorName == "CMS Inverter")
            {
                EmulatorType = DeviceEmulator.EmulatorType.CMS;
                Converse = (Converse)new Phoenixtec_Converse(Services);
                configFile = EmulatorMatrix[(int)EmulatorType, 1];
                configFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
                ConversationLoader conversations = new ConversationLoader(configFile, Services);
                conversations.LoadConversations(Converse);
                Converse.NoTimeout = false;
                UsePortReader = true;
            }
            else if (EmulatorName == "Modbus Growatt")
            {
                EmulatorType = DeviceEmulator.EmulatorType.ModbusGrowatt;
                ModbusConverseCalculations calc = new ModbusConverseCalculations();
                Converse = (Converse)new Converse(Services,calc);
                Converse.SetCheckSum16Endian(true);
                configFile = EmulatorMatrix[(int)EmulatorType, 1];
                configFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
                ConversationLoader conversations = new ConversationLoader(configFile, Services);
                conversations.LoadConversations(Converse);
                Converse.NoTimeout = false;
                UsePortReader = true;
            }
            else if (EmulatorName == "Modbus KLNE")
            {
                EmulatorType = DeviceEmulator.EmulatorType.ModbusKLNE;
                ModbusConverseCalculations calc = new ModbusConverseCalculations();
                Converse = (Converse)new Converse(Services, calc);
                Converse.SetCheckSum16Endian(true);
                configFile = EmulatorMatrix[(int)EmulatorType, 1];
                configFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
                ConversationLoader conversations = new ConversationLoader(configFile, Services);
                conversations.LoadConversations(Converse);
                Converse.NoTimeout = false;
                UsePortReader = true;
            }
            else if (EmulatorName == "KLNE Inverter")
            {
                EmulatorType = DeviceEmulator.EmulatorType.KLNE;
                Converse = (Converse)new Phoenixtec_Converse(Services);
                configFile = EmulatorMatrix[(int)EmulatorType, 1];
                configFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
                ConversationLoader conversations = new ConversationLoader(configFile, Services);
                conversations.LoadConversations(Converse);
                Converse.NoTimeout = false;
                UsePortReader = true;
            }
            else if (EmulatorName == "Modbus Xantrex")
            {
                EmulatorType = DeviceEmulator.EmulatorType.ModbusXantrex;
                ModbusConverseCalculations calc = new ModbusConverseCalculations();
                Converse = (Converse)new Converse(Services, calc);
                Converse.SetCheckSum16Endian(true);
                configFile = EmulatorMatrix[(int)EmulatorType, 1];
                configFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
                ConversationLoader conversations = new ConversationLoader(configFile, Services);
                conversations.LoadConversations(Converse);
                Converse.NoTimeout = false;
                UsePortReader = true;
            }
            else if (EmulatorName == "Modbus Sunnyroo")
            {
                EmulatorType = DeviceEmulator.EmulatorType.ModbusSunnyroo;
                ModbusConverseCalculations calc = new ModbusConverseCalculations();
                Converse = (Converse)new Converse(Services, calc);
                Converse.SetCheckSum16Endian(true);
                configFile = EmulatorMatrix[(int)EmulatorType, 1];
                configFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
                ConversationLoader conversations = new ConversationLoader(configFile, Services);
                conversations.LoadConversations(Converse);
                Converse.NoTimeout = false;
                UsePortReader = true;
            }
            else if (EmulatorName == "Fronius")
            {
                EmulatorType = DeviceEmulator.EmulatorType.Fronius;
                FroniusConverseCalculations calc = new FroniusConverseCalculations();
                Converse = (Converse)new Converse(Services, calc);
                configFile = EmulatorMatrix[(int)EmulatorType, 1];
                configFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
                ConversationLoader conversations = new ConversationLoader(configFile, Services);
                conversations.LoadConversations(Converse);
                Converse.NoTimeout = false;
                UsePortReader = true;
            }
            else if (EmulatorName == "Xantrex ASCII")
            {
                EmulatorType = DeviceEmulator.EmulatorType.XantrexASCII;
                Converse = (Converse)new Converse(Services,null);
                configFile = EmulatorMatrix[(int)EmulatorType, 1];
                configFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
                ConversationLoader conversations = new ConversationLoader(configFile, Services);
                conversations.LoadConversations(Converse);
                Converse.NoTimeout = false;
                UsePortReader = true;
            }
            else if (EmulatorName == "CC EnviR")
            {
                EmulatorType = DeviceEmulator.EmulatorType.CurrentCostEnviR;
                configFile = EmulatorMatrix[(int)EmulatorType, 1];
                configFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
                rootElement = XElement.Load(configFile, LoadOptions.None);
                liveElement = rootElement.Element("live").Element("msg");
                UsePortReader = false;
            }
            else if (EmulatorName == "GenericReader")
            {
                EmulatorType = DeviceEmulator.EmulatorType.GenericReader;
                ModbusConverseCalculations calc = new ModbusConverseCalculations();
                Converse = (Converse)new Converse(Services, calc);
                Converse.SetCheckSum16Endian(true);
                configFile = EmulatorMatrix[(int)EmulatorType, 1];
                configFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
                ConversationLoader conversations = new ConversationLoader(configFile, Services);
                conversations.LoadConversations(Converse);
                Converse.NoTimeout = false;
                UsePortReader = true;
            }
        }