Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                Logger.Error("Command file not specified. Please provide a path to the command file.");
            }

            ModbusFileParser.LoadFile(args[0]);

            ModbusProperties properties = ModbusFileParser.GetProperties();

            Bus bus = new Bus(new MasterDevice(0, properties.masterTimeout), properties.corruptionChance, properties.corruptionAttempts);

            for (int i = 0; i < properties.slaveCount; i++)
            {
                bus.Connect(new SlaveDevice((byte)(i + 1), properties.slaveProcessingTime, properties.slaveProcessingTimeJitter));
            }

            foreach (string[] tokens in ModbusFileParser.GetInstructions())
            {
                if (tokens.Length >= 2)
                {
                    switch (tokens[0])
                    {
                    case "Delay":
                        Thread.Sleep(int.Parse(tokens[1]));
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    bus.master.SendMessage(ModbusMessage.FromASCII(tokens[0], properties.useChecksum));
                }
            }

            Logger.Log("Executed all instructions");

            Console.ReadKey();
        }
Ejemplo n.º 2
0
        public static ModbusProperties GetProperties()
        {
            ModbusProperties properties = new ModbusProperties();

            lineIndex = 0;
            string currentLine;

            while ((currentLine = lines[lineIndex++].TrimEnd()) != "Begin")
            {
                if (currentLine == "" || currentLine.StartsWith("//"))
                {
                    continue;
                }

                string[] tokens = currentLine.Split(' ');

                try
                {
                    switch (tokens[0])
                    {
                    case "MasterTimeout":
                        properties.masterTimeout = int.Parse(tokens[1]);
                        break;

                    case "Slaves":
                        properties.slaveCount = byte.Parse(tokens[1]);
                        break;

                    case "SlaveProcessingTime":
                        properties.slaveProcessingTime = int.Parse(tokens[1]);
                        break;

                    case "SlaveProcessingTimeJitter":
                        properties.slaveProcessingTimeJitter = int.Parse(tokens[1]);
                        break;

                    case "UseChecksum":
                        if (tokens[1] != "True" && tokens[1] != "False")
                        {
                            throw new Exception("UseChecksum neither True nor False");
                        }
                        properties.useChecksum = tokens[1] == "True";
                        break;

                    case "CorruptionChance":
                        properties.corruptionChance = float.Parse(tokens[1]);
                        if (properties.corruptionChance < 0 || properties.corruptionChance > 1)
                        {
                            throw new Exception("CorruptionChance must be a float between 0 and 1");
                        }
                        break;

                    case "CorruptionAttempts":
                        properties.corruptionAttempts = int.Parse(tokens[1]);
                        break;
                    }
                }
                catch (Exception e)
                {
                    Logger.Error($"Parsing error in line {lineIndex}: {e.Message}");
                }
            }

            return(properties);
        }