Exemple #1
0
        public ModbusMaster(Device device, ICoreLogger coreLogger)
        {
            logger = coreLogger ?? throw new ArgumentNullException(nameof(coreLogger));

            if (device == null)
            {
                logger.Fatal(null, "Device is NULL");
                return;
            }
            myDevice   = device;
            IpAddress  = myDevice.Ip;
            ModbusPort = myDevice.Port;
            logger.Info($"ModbusMaster is creating - {device.Name}");
            myTimer.Interval = myDevice.RefreshRate;
            myTimer.Elapsed += MyTimer_Tick;
        }
Exemple #2
0
        private static bool ParseFile(string filename)
        {
            if (!File.Exists(filename))
            {
                logger.Fatal(null, $"File not found - {filename}");
                return(false);
            }

            HashSet <string> taglist = new HashSet <string>();

            string[] allLines;
            try
            {
                allLines = File.ReadAllLines(filename);
            }
            catch (IOException ex)
            {
                logger.Fatal(ex, "IO Exception");
                return(false);
            }

            foreach (var line in allLines)
            {
                if (line.StartsWith("//"))
                {
                    continue;
                }
                string[] splittedLine = line.Split(_Seperator_);
                if (splittedLine.Length == 0)
                {
                    continue;
                }
                if (splittedLine[0].ToLower() == "device")
                {
                    if (splittedLine.Length < 7)
                    {
                        continue;
                    }
                    string devicename = splittedLine[1];
                    string ip         = splittedLine[2];
                    int.TryParse(splittedLine[3], out int port);
                    byte.TryParse(splittedLine[4], out byte deviceid);
                    int refreshrate = _RefreshRate_;
                    int.TryParse(splittedLine[5], out refreshrate);
                    bool   isactive = splittedLine[6] == "1" ? true : false;
                    Device aDevice  = new Device(
                        devicename,
                        ip,
                        port,
                        deviceid,
                        refreshrate,
                        isactive,
                        logger
                        );
                    myDevices.Add(aDevice.Name, aDevice);
                }
                else if (splittedLine[0].ToLower() == "tag")
                {
                    taglist.Add(line);
                }
            }

            foreach (var tagstring in taglist)
            {
                string[] splittedLine = tagstring.Split(_Seperator_);
                if (splittedLine.Length < 10)
                {
                    continue;
                }
                string tagname       = splittedLine[1];
                string devicename    = splittedLine[2];
                string addressstring = splittedLine[3];
                string modbustype    = splittedLine[4].ToLower();
                string direction     = splittedLine[5].ToLower();
                string valuetype     = splittedLine[6].ToLower();
                string masktype      = splittedLine[7].ToLower();
                string mask          = splittedLine[8].ToLower();
                string mergetype     = splittedLine[9].ToLower();
                string range         = splittedLine[10].ToLower();
                Tag    aTag          = new Tag(tagname);
                if (modbustype == "cs")
                {
                    bool.TryParse(mask, out bool boolmask);
                    aTag.DefineBoolTag(
                        StatusFunction.CoilStatus,
                        ConvertStringToMaskType(masktype), boolmask,
                        ConvertStringToMergeType(mergetype));
                }
                else if (modbustype == "is")
                {
                    bool.TryParse(mask, out bool boolmask);
                    aTag.DefineBoolTag(
                        StatusFunction.InputStatus,
                        ConvertStringToMaskType(masktype), boolmask,
                        ConvertStringToMergeType(mergetype));
                }
                else if (modbustype == "hr")
                {
                    if (valuetype == "ushort")
                    {
                        ushort ushortmask = ushort.MaxValue;
                        ushort.TryParse(mask, out ushortmask);
                        ushort ushortrange = ushort.MinValue;
                        ushort.TryParse(range, out ushortrange);
                        aTag.DefineUshortTag(
                            RegisterFunction.HoldingRegister,
                            ConvertStringToMaskType(masktype), ushortmask,
                            ConvertStringToMergeType(mergetype),
                            ushortrange);
                    }
                    else if (valuetype == "lsrf")
                    {
                        ushort ushortrange = ushort.MinValue;
                        ushort.TryParse(range, out ushortrange);
                        aTag.DefineFloatTag(
                            RegisterFunction.HoldingRegister,
                            LSRF,
                            ushortrange);
                    }
                    else if (valuetype == "msrf")
                    {
                        ushort ushortrange = ushort.MinValue;
                        ushort.TryParse(range, out ushortrange);
                        aTag.DefineFloatTag(
                            RegisterFunction.HoldingRegister,
                            MSRF,
                            ushortrange);
                    }
                }
                else if (modbustype == "ir")
                {
                    if (valuetype == "ushort")
                    {
                        ushort ushortmask = ushort.MaxValue;
                        ushort.TryParse(mask, out ushortmask);
                        ushort ushortrange = ushort.MinValue;
                        ushort.TryParse(range, out ushortrange);
                        aTag.DefineUshortTag(
                            RegisterFunction.HoldingRegister,
                            ConvertStringToMaskType(masktype), ushortmask,
                            ConvertStringToMergeType(mergetype),
                            ushortrange);
                    }
                    else if (valuetype == "lsrf")
                    {
                        ushort ushortrange = ushort.MinValue;
                        ushort.TryParse(range, out ushortrange);
                        aTag.DefineFloatTag(
                            RegisterFunction.HoldingRegister,
                            LSRF,
                            ushortrange);
                    }
                    else if (valuetype == "msrf")
                    {
                        ushort ushortrange = ushort.MinValue;
                        ushort.TryParse(range, out ushortrange);
                        aTag.DefineFloatTag(
                            RegisterFunction.HoldingRegister,
                            MSRF,
                            ushortrange);
                    }
                }

                aTag.TagDirection = direction == "write" ? Direction.Write : Direction.Read;
                ushort[] addresses = ParseAddressString(addressstring);
                foreach (var anaddress in addresses)
                {
                    aTag.InnerTag.AddAddress(anaddress);
                }

                if (myDevices.ContainsKey(devicename))
                {
                    myDevices[devicename].Collection.AddTag(aTag);
                }
            }
            return(true);
        }