예제 #1
0
        private void ReadCommand_Execute(object obj)
        {
            try
            {
                // TODO implement
                ModbusReadCommandParameters p = null;
                switch (type)
                {
                case PointType.DO_REG:
                    p = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, address, 1);
                    break;

                case PointType.DI_REG:
                    p = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, address, 1);
                    break;

                case PointType.IN_REG:
                    p = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, address, 1);
                    break;
                }
                ModbusFunction fn = FunctionFactory.CreateModbusFunction(p);
                this.commandExecutor.ExecuteCommand(fn);
            }
            catch (Exception ex)
            {
                string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}";
                this.stateUpdater.LogMessage(message);
            }
        }
예제 #2
0
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters mdb_read_comm_pars = this.CommandParameters as ModbusReadCommandParameters;
            Dictionary <Tuple <PointType, ushort>, ushort> returnResponse = new Dictionary <Tuple <PointType, ushort>, ushort>();

            if (response[7] == (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS)
            {
                int numberOfBytes = response[8] / 2;

                for (ushort i = 0; i < numberOfBytes; i++)
                {
                    byte[] array = new byte[2];

                    array[0] = response[9 + i * 2 + 1];
                    array[1] = response[9 + i * 2];

                    ushort value = BitConverter.ToUInt16(array, 0);

                    returnResponse.Add
                        (new Tuple <PointType, ushort>(PointType.ANALOG_OUTPUT, (ushort)(mdb_read_comm_pars.StartAddress + i)), value);
                }
            }

            return(returnResponse);
        }
예제 #3
0
        /// <inheritdoc />
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters mdmReadCommParams      = this.CommandParameters as ModbusReadCommandParameters;
            Dictionary <Tuple <PointType, ushort>, ushort> dic = new Dictionary <Tuple <PointType, ushort>, ushort>();
            int    kolicina = response[8];
            int    prosao   = 0;
            ushort value    = 0;

            for (int i = 0; i < kolicina; i++)
            {
                ushort value1 = (ushort)(response[9 + i]);
                for (int j = 0; j < 8; j++)
                {
                    if (prosao != mdmReadCommParams.Quantity)
                    {
                        value = (ushort)(value1 & (byte)0x1);
                        dic.Add(new Tuple <PointType, ushort>(PointType.DIGITAL_INPUT, (ushort)(mdmReadCommParams.StartAddress + prosao)), value);

                        value1 = (ushort)(value1 >> 1);
                        prosao++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(dic);
        }
예제 #4
0
        /// <summary>
        /// Acquisition thread
        ///		Awaits for trigger;
        ///		After configured period send appropriate command to MdbSim for each point type
        /// </summary>
        private void Acquisition_DoWork()
        {
            while (true)
            {
                try
                {
                    acquisitionTrigger.WaitOne();
                    DO_REG_sekunde++;

                    HR_INT_sekunde++;
                    if (DO_REG_sekunde == ConfigReader.Instance.GetAcquisitionInterval("DigOut"))
                    {
                        ModbusReadCommandParameters p = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, ConfigReader.Instance.GetStartAddress("DigOut"), ConfigReader.Instance.GetNumberOfRegisters("DigOut"));;
                        ModbusFunction fn             = FunctionFactory.CreateModbusFunction(p);
                        this.commandExecutor.EnqueueCommand(fn);
                        DO_REG_sekunde = 0;
                    }

                    if (HR_INT_sekunde == ConfigReader.Instance.GetAcquisitionInterval("AnaOut"))
                    {
                        ModbusReadCommandParameters p = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, ConfigReader.Instance.GetStartAddress("AnaOut"), ConfigReader.Instance.GetNumberOfRegisters("AnaOut"));;
                        ModbusFunction fn             = FunctionFactory.CreateModbusFunction(p);
                        this.commandExecutor.EnqueueCommand(fn);
                        HR_INT_sekunde = 0;
                    }
                }
                catch (Exception ex)
                {
                    string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}";
                    stateUpdater.LogMessage(message);
                }
            }
        }
예제 #5
0
        /// <inheritdoc />
        public void ExecuteReadCommand(IConfigItem configItem, ushort transactionId, byte remoteUnitAddress, ushort startAddress, ushort numberOfPoints)
        {
            ModbusReadCommandParameters p  = new ModbusReadCommandParameters(6, (byte)GetReadFunctionCode(configItem.RegistryType), startAddress, numberOfPoints, transactionId, remoteUnitAddress);
            IModbusFunction             fn = FunctionFactory.CreateModbusFunction(p);

            this.functionExecutor.EnqueueCommand(fn);
        }
예제 #6
0
        /// <inheritdoc />
        public override byte[] PackRequest()
        {        /*
                  * ModbusReadCommandParameters para = this.CommandParameters as ModbusReadCommandParameters;
                  * byte[] ret = new byte[12];
                  *
                  * Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)para.TransactionId)), 0, ret, 0, 2);
                  * Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)para.ProtocolId)), 0, ret, 2, 2);
                  * Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)para.Length)), 0, ret, 4, 2);
                  *
                  * ret[6] = para.UnitId;
                  * ret[7] = para.FunctionCode;
                  *
                  * Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)para.StartAddress)), 0, ret, 8, 2);
                  * Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)para.Quantity)), 0, ret, 10, 2);
                  *
                  * return ret;
                  */
            ModbusReadCommandParameters mdbParams = (ModbusReadCommandParameters)CommandParameters;

            byte[] req = new byte[12];

            Buffer.BlockCopy(BitConverter.GetBytes(Htons((short)CommandParameters.ProtocolId)), 0, req, 2, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(Htons((short)CommandParameters.Length)), 0, req, 4, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(Htons((short)CommandParameters.TransactionId)), 0, req, 0, 2);
            req[6] = mdbParams.UnitId;
            req[7] = mdbParams.FunctionCode;
            Buffer.BlockCopy(BitConverter.GetBytes(Htons((short)mdbParams.StartAddress)), 0, req, 8, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(Htons((short)mdbParams.Quantity)), 0, req, 10, 2);

            return(req);
        }
예제 #7
0
        /// <summary>
        /// Method that is executed when read button is clicked on control window;
        /// Method should create read command parameters and provide it to FunctionFactory
        /// </summary>
        /// <param name="obj">Not used</param>
        private void ReadCommand_Execute(object obj)
        {
            try
            {
                ConfigReader reader = ConfigReader.Instance;

                ModbusReadCommandParameters mdb = null;

                if (Type == PointType.ANALOG_INPUT)
                {
                    mdb = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, Address, 1);
                }
                if (Type == PointType.DIGITAL_OUTPUT)
                {
                    mdb = new ModbusReadCommandParameters(6, (byte)Type, Address, 1);
                }
                if (Type == PointType.DIGITAL_INPUT)
                {
                    mdb = new ModbusReadCommandParameters(6, (byte)Type, Address, 1);
                }
                if (Type == PointType.ANALOG_OUTPUT)
                {
                    mdb = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, Address, 1);
                }

                ModbusFunction fn = FunctionFactory.CreateModbusFunction(mdb);
                this.commandExecutor.EnqueueCommand(fn);
            }
            catch (Exception ex)
            {
                string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}";
                this.stateUpdater.LogMessage(message);
            }
        }
예제 #8
0
        /// <inheritdoc />
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters mbParams = (ModbusReadCommandParameters)CommandParameters;
            var    retval = new Dictionary <Tuple <PointType, ushort>, ushort>();
            ushort address = mbParams.StartAddress, value;

            int quantity = response[8];

            for (int i = 0; i < quantity; ++i)
            {
                for (int j = 0; j < 8; ++j)
                {
                    if ((j + i * 8) >= mbParams.Quantity)
                    {
                        break;
                    }

                    value            = (ushort)(response[9 + i] & 0x1);
                    response[9 + i] /= 2; // >>> 1

                    retval.Add(new Tuple <PointType, ushort>(PointType.DIGITAL_INPUT, (ushort)(address + j)), value);
                }
            }
            return(retval);
        }
예제 #9
0
        /// <inheritdoc />
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters mrcp = this.CommandParameters as ModbusReadCommandParameters;

            Dictionary <Tuple <PointType, ushort>, ushort> dic = new Dictionary <Tuple <PointType, ushort>, ushort>();

            ushort q = response[8];
            ushort value;

            for (int i = 0; i < q; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    value            = (ushort)(response[9 + i] & (byte)0x1);
                    response[9 + i] /= 2;

                    if (mrcp.Quantity < (j + i * 8))
                    {
                        break;
                    }

                    dic.Add(new Tuple <PointType, ushort>(PointType.DIGITAL_INPUT, (ushort)(mrcp.StartAddress + (ushort)(j + i * 8))), value);
                }
            }

            return(dic);
        }
예제 #10
0
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            // Jedno od resenja
            ModbusReadCommandParameters mdmReadCommParams      = this.CommandParameters as ModbusReadCommandParameters;
            Dictionary <Tuple <PointType, ushort>, ushort> dic = new Dictionary <Tuple <PointType, ushort>, ushort>();
            ushort value = (ushort)(response[9] & (byte)0x1);

            dic.Add(new Tuple <PointType, ushort>(PointType.DO_REG, mdmReadCommParams.StartAddress), value);
            return(dic);
        }
예제 #11
0
        /// <summary>
        /// Acquisition thread
        ///		Awaits for trigger;
        ///		After configured period send appropriate command to MdbSim for each point type
        ///
        ///		Kao uslov za while petlju korititi acquisitionStopSignal da bi se akvizicioni thread ugasio kada se aplikacija ugasi
        /// </summary>
        private void Acquisition_DoWork()
        {
            ConfigReader cr  = ConfigReader.Instance;
            int          cnt = 0;

            int DigOut = cr.GetAcquisitionInterval("DigOut");
            int DigIn  = cr.GetAcquisitionInterval("DigIn");
            int AnaOut = cr.GetAcquisitionInterval("AnaOut");
            int AnaIn  = cr.GetAcquisitionInterval("AnaIn");

            ModbusReadCommandParameters mrcp = null;
            ModbusFunction fn = null;

            while (true)
            {
                if (cnt % DigOut == 0)
                {
                    mrcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, cr.GetStartAddress("DigOut"), cr.GetNumberOfRegisters("DigOut"));
                    fn   = FunctionFactory.CreateModbusFunction(mrcp);
                    this.commandExecutor.EnqueueCommand(fn);
                }

                if (cnt % DigIn == 0)
                {
                    mrcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, cr.GetStartAddress("DigIn"), cr.GetNumberOfRegisters("DigIn"));
                    fn   = FunctionFactory.CreateModbusFunction(mrcp);
                    this.commandExecutor.EnqueueCommand(fn);
                }

                if (cnt % AnaOut == 0)
                {
                    mrcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, cr.GetStartAddress("AnaOut"), cr.GetNumberOfRegisters("AnaOut"));
                    fn   = FunctionFactory.CreateModbusFunction(mrcp);
                    this.commandExecutor.EnqueueCommand(fn);
                }

                if (cnt % AnaIn == 0)
                {
                    mrcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, cr.GetStartAddress("AnaIn"), cr.GetNumberOfRegisters("AnaIn"));
                    fn   = FunctionFactory.CreateModbusFunction(mrcp);
                    this.commandExecutor.EnqueueCommand(fn);
                }

                try
                {
                    acquisitionTrigger.WaitOne();
                    cnt++;
                }
                catch (Exception ex)
                {
                    string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}";
                    stateUpdater.LogMessage(message);
                }
            }
        }
예제 #12
0
        /// <inheritdoc />
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters para = this.CommandParameters as ModbusReadCommandParameters;
            Dictionary <Tuple <PointType, ushort>, ushort> dic = new Dictionary <Tuple <PointType, ushort>, ushort>();

            ushort value = (ushort)(response[9] & (byte)0x1);

            dic.Add(new Tuple <PointType, ushort>(PointType.DIGITAL_OUTPUT, para.StartAddress), value);

            return(dic);
        }
예제 #13
0
파일: Acquisitor.cs 프로젝트: fr830/SCADA-3
        private void Acquisition_DoWork()
        {
            int cnt = 0;

            ConfigReader cr    = ConfigReader.Instance;
            int          DOCnt = cr.GetAcquisitionIntervalForPointType(PointType.DIGITAL_OUTPUT);
            int          DICnt = cr.GetAcquisitionIntervalForPointType(PointType.DIGITAL_INPUT);
            int          AICnt = cr.GetAcquisitionIntervalForPointType(PointType.ANALOG_INPUT);
            int          AOCnt = cr.GetAcquisitionIntervalForPointType(PointType.ANALOG_OUTPUT);



            while (true)
            {
                //// TODO implement
                if (cnt % DOCnt == 0)
                {
                    // TODO implement
                    ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, cr.GetStartAddressForPointType(PointType.DIGITAL_OUTPUT), cr.GetNumberOfRegistersForPointType(PointType.DIGITAL_OUTPUT));
                    ModbusFunction fn = FunctionFactory.CreateModbusFunction(mcp);
                    this.commandExecutor.EnqueueCommand(fn);
                }
                if (cnt % DICnt == 0)
                {
                    ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, cr.GetStartAddressForPointType(PointType.DIGITAL_INPUT), cr.GetNumberOfRegistersForPointType(PointType.DIGITAL_INPUT));
                    ModbusFunction fn = FunctionFactory.CreateModbusFunction(mcp);
                    this.commandExecutor.EnqueueCommand(fn);
                }
                if (cnt % AOCnt == 0)
                {
                    ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, cr.GetStartAddressForPointType(PointType.ANALOG_OUTPUT), cr.GetNumberOfRegistersForPointType(PointType.ANALOG_OUTPUT));
                    ModbusFunction fn = FunctionFactory.CreateModbusFunction(mcp);
                    this.commandExecutor.EnqueueCommand(fn);
                }
                if (cnt % AICnt == 0)
                {
                    ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, cr.GetStartAddressForPointType(PointType.ANALOG_INPUT), cr.GetNumberOfRegistersForPointType(PointType.ANALOG_INPUT));
                    ModbusFunction fn = FunctionFactory.CreateModbusFunction(mcp);
                    this.commandExecutor.EnqueueCommand(fn);
                }

                try
                {
                    acquisitionTrigger.WaitOne();
                    cnt++;
                }
                catch (Exception ex)
                {
                    string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}";
                    stateUpdater.LogMessage(message);
                }
            }
        }
예제 #14
0
 private void ReadCommand_Execute(object obj)
 {
     try
     {
         ModbusReadCommandParameters parameters = new ModbusReadCommandParameters(6, (byte)Type, this.Address, 1);
         ModbusFunction fn = FunctionFactory.CreateModbusFunction(this.Type, CommandType.READ, parameters);
         this.commandExecutor.ExecuteCommand(fn);
     }
     catch (Exception ex)
     {
         this.stateUpdater.LogMessage(ex.Message);
     }
 }
예제 #15
0
        /// <inheritdoc />
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters para = this.CommandParameters as ModbusReadCommandParameters;
            Dictionary <Tuple <PointType, ushort>, ushort> dic = new Dictionary <Tuple <PointType, ushort>, ushort>();

            byte b1 = response[9];
            byte b2 = response[10];

            ushort value = (ushort)(b2 + (b1 << 8));

            dic.Add(new Tuple <PointType, ushort>(PointType.ANALOG_OUTPUT, para.StartAddress), value);

            return(dic);
        }
예제 #16
0
 /// <summary>
 /// Method that is executed when read button is clicked on control window;
 /// Method should create read command parameters and provide it to FunctionFactory
 /// </summary>
 /// <param name="obj">Not used</param>
 private void ReadCommand_Execute(object obj)
 {
     try
     {
         ModbusReadCommandParameters p  = new ModbusReadCommandParameters(6, (byte)GetReadFunctionCode(type), address, 1, configuration);
         IModbusFunction             fn = FunctionFactory.CreateModbusFunction(p);
         this.commandExecutor.EnqueueCommand(fn);
     }
     catch (Exception ex)
     {
         string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}";
         this.stateUpdater.LogMessage(message);
     }
 }
예제 #17
0
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters mdmReadCommParams      = this.CommandParameters as ModbusReadCommandParameters;
            Dictionary <Tuple <PointType, ushort>, ushort> dic = new Dictionary <Tuple <PointType, ushort>, ushort>();


            byte port1 = response[10];
            byte port2 = response[9];

            ushort value = (ushort)(port1 + (port2 << 8));

            dic.Add(new Tuple <PointType, ushort>(PointType.HR_INT, mdmReadCommParams.StartAddress), value);
            return(dic);
        }
예제 #18
0
        public override byte[] PackRequest()
        {
            ModbusReadCommandParameters mdmReadCommParams = this.CommandParameters as ModbusReadCommandParameters;

            byte[] mdbRequest = new byte[12];
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)mdmReadCommParams.TransactionId)), 0, mdbRequest, 0, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)mdmReadCommParams.ProtocolId)), 0, mdbRequest, 2, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)mdmReadCommParams.Length)), 0, mdbRequest, 4, 2);
            mdbRequest[6] = mdmReadCommParams.UnitId;
            mdbRequest[7] = mdmReadCommParams.FunctionCode;
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)mdmReadCommParams.StartAddress)), 0, mdbRequest, 8, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)mdmReadCommParams.Quantity)), 0, mdbRequest, 10, 2);
            return(mdbRequest);
        }
예제 #19
0
        /// <inheritdoc />
        public override byte[] PackRequest()
        {
            byte[] paket = new byte[12];
            ModbusReadCommandParameters read = (ModbusReadCommandParameters)CommandParameters;

            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)read.TransactionId)), 0, paket, 0, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)read.ProtocolId)), 0, paket, 2, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)read.Length)), 0, paket, 4, 2);
            paket[6] = read.UnitId;
            paket[7] = read.FunctionCode;
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)read.StartAddress)), 0, paket, 8, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)read.Quantity)), 0, paket, 10, 2);
            return(paket);
        }
예제 #20
0
        /// <inheritdoc/>
        public override byte[] PackRequest()
        {
            byte[] retVal = new byte[12];
            ModbusReadCommandParameters cmp = this.CommandParameters as ModbusReadCommandParameters;

            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)cmp.TransactionId)), 0, retVal, 0, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)cmp.ProtocolId)), 0, retVal, 2, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)cmp.Length)), 0, retVal, 4, 2);
            retVal[6] = cmp.UnitId;
            retVal[7] = cmp.FunctionCode;
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)cmp.StartAddress)), 0, retVal, 8, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)cmp.Quantity)), 0, retVal, 10, 2);

            return(retVal);
        }
예제 #21
0
        /// <inheritdoc />
        public override byte[] PackRequest()
        {
            byte[] req = new byte[12];
            ModbusReadCommandParameters mbParams = (ModbusReadCommandParameters)CommandParameters;

            Buffer.BlockCopy(BitConverter.GetBytes(Htons((short)mbParams.TransactionId)), 0, req, 0, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(Htons((short)mbParams.ProtocolId)), 0, req, 2, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(Htons((short)mbParams.Length)), 0, req, 4, 2);
            req[6] = mbParams.UnitId;
            req[7] = mbParams.FunctionCode;
            Buffer.BlockCopy(BitConverter.GetBytes(Htons((short)mbParams.StartAddress)), 0, req, 8, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(Htons((short)mbParams.Quantity)), 0, req, 10, 2);

            return(req);
        }
예제 #22
0
파일: Acquisitor.cs 프로젝트: fr830/SCADA-3
        /// <summary>
        /// Acquisition thread
        ///		Awaits for trigger;
        ///		After configured period send appropriate command to MdbSim for each point type
        /// </summary>
        private void Acquisition_DoWork()
        {
            int cnt = 0;

            ConfigReader cr = ConfigReader.Instance;

            int DOCnt = cr.GetAcquisitionInterval("DigOut");
            int AOCnt = cr.GetAcquisitionInterval("AnaOut");

            while (true)
            {
                if (cnt % DOCnt == 0)
                {
                    ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, cr.GetStartAddress("DigOut"), cr.GetNumberOfRegisters("DigOut"));
                    ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp);
                    this.commandExecutor.EnqueueCommand(mf);
                }

                if (cnt % AOCnt == 0)
                {
                    ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, cr.GetStartAddress("AnaOut"), cr.GetNumberOfRegisters("AnaOut"));
                    ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp);
                    this.commandExecutor.EnqueueCommand(mf);
                }



                //ModbusWriteCommandParameters mwcp1 = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_COIL, cr.GetStartAddress("DigOut"), cr.GetNumberOfRegisters("DigOut"));
                //ModbusFunction mf1 = FunctionFactory.CreateModbusFunction(mwcp1);
                //this.commandExecutor.EnqueueCommand(mf1);

                //ModbusWriteCommandParameters mwcp2 = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, cr.GetStartAddress("AnaOut"), cr.GetNumberOfRegisters("AnaOut"));
                //ModbusFunction mf2 = FunctionFactory.CreateModbusFunction(mwcp2);
                //this.commandExecutor.EnqueueCommand(mf2);

                try
                {
                    acquisitionTrigger.WaitOne();
                    cnt++;
                }
                catch (Exception ex)
                {
                    string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}";
                    stateUpdater.LogMessage(message);
                }
            }
        }
예제 #23
0
        /// <inheritdoc />
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters para = this.CommandParameters as ModbusReadCommandParameters;
            Dictionary <Tuple <PointType, ushort>, ushort> dic = new Dictionary <Tuple <PointType, ushort>, ushort>();

            int q = response[8];

            int start1 = 7;
            int start2 = 8;

            for (int i = 0; i < q / 2; i++)
            {
                ushort v = (ushort)(response[start2 += 2] + (response[start1 += 2] << 8));
                dic.Add(new Tuple <PointType, ushort>(PointType.ANALOG_INPUT, (ushort)(para.StartAddress + i)), v);
            }
            return(dic);
        }
예제 #24
0
        /// <inheritdoc/>
        public override byte[] PackRequest()
        {
            ModbusReadCommandParameters para = this.CommandParameters as ModbusReadCommandParameters;

            byte[] ret = new byte[12];

            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)para.TransactionId)), 0, ret, 0, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)para.ProtocolId)), 0, ret, 2, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)para.Length)), 0, ret, 4, 2);

            ret[6] = para.UnitId;
            ret[7] = para.FunctionCode;

            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)para.StartAddress)), 0, ret, 8, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)para.Quantity)), 0, ret, 10, 2);

            return(ret);
        }
예제 #25
0
        /// <inheritdoc />
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters mdbParams = (ModbusReadCommandParameters)CommandParameters;
            var retval = new Dictionary <Tuple <PointType, ushort>, ushort>();

            ushort address  = mdbParams.StartAddress;
            int    quantity = response[8];

            for (int i = 0, j = 0; i < quantity; i += 2, ++j)
            {
                ushort byte1, byte2, value;
                byte1 = response[9 + i];
                byte2 = response[10 + i];

                value = (ushort)(byte2 + (byte1 << 8));
                retval.Add(new Tuple <PointType, ushort>(PointType.ANALOG_INPUT, (ushort)(address + j)), value);
            }

            return(retval);
        }
예제 #26
0
        /// <inheritdoc />
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters mdmReadCommParams      = this.CommandParameters as ModbusReadCommandParameters;
            Dictionary <Tuple <PointType, ushort>, ushort> dic = new Dictionary <Tuple <PointType, ushort>, ushort>();

            ushort quantity = response[8];

            ushort value;

            int p1 = 7, p2 = 8;

            for (int i = 0; i < quantity / 2; i++)
            {
                byte port1 = response[p1 += 2];
                byte port2 = response[p2 += 2];

                value = (ushort)(port2 + (port1 << 8));
                dic.Add(new Tuple <PointType, ushort>(PointType.ANALOG_INPUT, (ushort)(mdmReadCommParams.StartAddress + i)), value);
            }
            return(dic);
        }
예제 #27
0
        /// <inheritdoc />
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            Dictionary <Tuple <PointType, ushort>, ushort> odgovor = new Dictionary <Tuple <PointType, ushort>, ushort>();
            ModbusReadCommandParameters parameters = (ModbusReadCommandParameters)CommandParameters;


            // used for counting the amount of data in the dictionary and comparing that to the quantity of data in the response
            ushort bitCounter = 0;

            //9 is the first byte of data in the response
            for (int i = 9; i < response.Length; i += 2)
            {
                short dataShort = BitConverter.ToInt16(response, i);
                dataShort = IPAddress.NetworkToHostOrder(dataShort);

                odgovor.Add(new Tuple <PointType, ushort>(PointType.ANALOG_INPUT, (ushort)(parameters.StartAddress + bitCounter)), (ushort)dataShort);
                bitCounter++;
            }

            return(odgovor);
        }
        /// <inheritdoc />
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            //Extract all the data bytes from the entire response
            Dictionary <Tuple <PointType, ushort>, ushort> odgovor = new Dictionary <Tuple <PointType, ushort>, ushort>();
            ModbusReadCommandParameters parameters = (ModbusReadCommandParameters)CommandParameters;

            // used for counting the amount of unique data entries
            ushort byteCounter = 0;

            //9 is the first byte of data in the response analog responses take up 2 bytes instead of 1
            for (int i = 9; i < response.Length; i += 2)
            {
                short dataShort = BitConverter.ToInt16(response, i);
                dataShort = IPAddress.NetworkToHostOrder(dataShort);

                odgovor.Add(new Tuple <PointType, ushort>(PointType.ANALOG_OUTPUT, (ushort)(parameters.StartAddress + byteCounter)), (ushort)dataShort);
                byteCounter++;
            }

            return(odgovor);
        }
예제 #29
0
        public static IReadModbusFunction CreateReadModbusFunction(ModbusReadCommandParameters commandParameters)
        {
            IReadModbusFunction modbusFunction;

            if (FunctionFactory.scadaModel == null)
            {
                string message = $"CreateReadModbusFunction => SCADA model is null.";
                LoggerWrapper.Instance.LogError(message);
                //TODO: InternalSCADAServiceException
                throw new Exception(message);
            }

            switch ((ModbusFunctionCode)commandParameters.FunctionCode)
            {
            case ModbusFunctionCode.READ_COILS:
                modbusFunction = new ReadCoilsFunction(commandParameters, FunctionFactory.scadaModel);
                break;

            case ModbusFunctionCode.READ_DISCRETE_INPUTS:
                modbusFunction = new ReadDiscreteInputsFunction(commandParameters, FunctionFactory.scadaModel);
                break;

            case ModbusFunctionCode.READ_INPUT_REGISTERS:
                modbusFunction = new ReadInputRegistersFunction(commandParameters, FunctionFactory.scadaModel);
                break;

            case ModbusFunctionCode.READ_HOLDING_REGISTERS:
                modbusFunction = new ReadHoldingRegistersFunction(commandParameters, FunctionFactory.scadaModel);
                break;

            default:
                modbusFunction = null;
                string message = $"CreateReadModbusFunction => Wrong function code {(ModbusFunctionCode)commandParameters.FunctionCode}.";
                LoggerWrapper.Instance.LogError(message);
                break;
            }

            return(modbusFunction);
        }
        public override Dictionary <Tuple <PointType, ushort>, ushort> ParseResponse(byte[] response)
        {
            ModbusReadCommandParameters mdbrp = (ModbusReadCommandParameters)CommandParameters;
            Dictionary <Tuple <PointType, ushort>, ushort> returnResponse = new Dictionary <Tuple <PointType, ushort>, ushort>();

            if (response[7] == (byte)ModbusFunctionCode.READ_INPUT_REGISTERS)
            {
                int n = response[8] / 2;

                for (ushort i = 0; i < n; i++)
                {
                    byte[] array = new byte[2];

                    array[0] = response[9 + i * 2 + 1];
                    array[1] = response[9 + i * 2];

                    ushort value = BitConverter.ToUInt16(array, 0);
                    returnResponse.Add(new Tuple <PointType, ushort>(PointType.ANALOG_INPUT, (ushort)(mdbrp.StartAddress + i)), value);
                }
            }
            return(returnResponse);
        }