예제 #1
0
        public void UpdateValue(Data data, bool readOnlyOverride = false)
        {
            var normalisedAddress = AddressTools.AddressNormalisation(data.Address);
            var addressType       = AddressTools.AddressType(data.Address);

            if (addressType != data.Type)
            {
                throw new DataTypeDoesNotMatchAddressException(data.Address.ToString());
            }

            if (ValueExists(data.Address))
            {
                switch (addressType)
                {
                case DataType.Coil:
                    Coils.Find(c => c.Address == normalisedAddress).Update(data.Value, readOnlyOverride);
                    break;

                case DataType.DiscreteInput:
                    DiscreteInputs.Find(c => c.Address == normalisedAddress).Update(data.Value, readOnlyOverride);
                    break;

                case DataType.InputRegister:
                    InputRegisters.Find(c => c.Address == normalisedAddress).Update(data.Value, readOnlyOverride);
                    break;

                case DataType.HoldingRegister:
                    HoldingRegisters.Find(c => c.Address == normalisedAddress).Update(data.Value, readOnlyOverride);
                    break;
                }
            }
            else
            {
                var normalisedData = Data.Create(normalisedAddress, data.Type);

                switch (addressType)
                {
                case DataType.Coil:
                    Coils.Add(normalisedData);
                    break;

                case DataType.DiscreteInput:
                    DiscreteInputs.Add(normalisedData);
                    break;

                case DataType.InputRegister:
                    InputRegisters.Add(normalisedData);
                    break;

                case DataType.HoldingRegister:
                    HoldingRegisters.Add(normalisedData);
                    break;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Decodes a message string from Mdbus.exe (Calta Software)
        /// </summary>
        /// <param name="message">Message string from Mdbus Monitor logging, starting with the Slave ID</param>
        /// <param name="useModiconFormat">True if Modicon format for Float or LongInt are used (the least significant bytes are sent in the first register and the most significant bytes in the second register of a pair)</param>
        /// <param name="mode">Set the mode to Master or Slave. Used to distinguish between request and response when decoding</param>
        /// <param name="dataType">Set the data type to decode, either float, int or long int (default is float)</param>
        /// <example>
        ///
        /// Example responses:
        ///
        /// 3 (0x03) Read Holding Registers
        /// SlaveID=1, FC=0x03, ByteCount=0x10 (16), values: 60 3A 46 ....
        /// 01 03 10 60 3A 46 33 69 89 44 57 33 CE 43 06 8B 59 3B 72 C4 8E
        ///
        /// 4 (0x04) Read Input Registers
        /// SlaveID=1, FC=0x04, ByteCount=0x10 (16), values: 60 3A 46 ....
        /// 01 04 10 60 3A 46 33 69 89 44 57 33 CE 43 06 8B 59 3B 72 C4 8E
        ///
        /// 16 (0x10) Write Multiple Registers
        /// SlaveID=1, FC=0x10 (16), StartAddr=0x0064 (100), Registers=0x0032 (50), ByteCount=0x64 (100), Values:48 9C 1C ...
        /// 01 10 00 64 00 32 64 48 9C 1C B6 48 94 27 C8 48 98 95 47 48 87 F7 BD 42 AC 07 2B 42 AE 57 91 42 AC 89 5E 42 AF DE 29 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 45 C9 F0 4D 45 CA 95 4D 45 C9 23 FE 45 C9 64 DF 42 0A 66 66 42 0C CC CD 42 13 33 33 42 11 33 33 42 9E CC CD 9D A4
        /// </example>
        /// <returns></returns>
        ///
        private void DecodeMessage(string message, bool useModiconFormat, ModbusMessageMode mode, ModbusDataType dataType)
        {
            if (!message.Contains(' '))
            {
                // TODO: create string array from text without spaces.
                throw new ArgumentException("Given message string does not contain spaces. Must use a valid string from Mdbus Monitor log");
            }
            OriginalMessageString = message;
            UseModiconFormat      = useModiconFormat;
            MessageMode           = mode;
            DataType = dataType;
            // Check if there are RX or TX in beginning
            MessageType = ModbusMessageType.Unknown;
            if (message.Trim().StartsWith("RX"))
            {
                MessageType = ModbusMessageType.Receive;
                message     = message.Replace("RX", "");
            }
            else if (message.Trim().StartsWith("TX"))
            {
                MessageType = ModbusMessageType.Transmit;
                message     = message.Replace("TX", "");
            }
            // If we are slave then a TX would be a request (from master) and RX would be a response
            // If we are master, then a TX would be a request (from us) and RX would be a response
            // By default we want to decode as response (we are slave and RX message is important)
            MessageRole = ModbusMessageRole.Response;
            if (MessageMode == ModbusMessageMode.Master && MessageType == ModbusMessageType.Transmit)
            {
                MessageRole = ModbusMessageRole.Request;
            }
            else if (MessageMode == ModbusMessageMode.Slave && MessageType == ModbusMessageType.Receive)
            {
                MessageRole = ModbusMessageRole.Request;
            }

            string[] hexValuesSplit = message.Trim().Split(' ');

            if (hexValuesSplit.Length > 0)
            {
                SlaveId = Convert.ToInt32(hexValuesSplit[0], 16);
            }
            if (hexValuesSplit.Length > 1)
            {
                var byteValue = Convert.ToInt16(hexValuesSplit[1], 16);
                FunctionCode    = byteValue & 0x7F;             // Ignore the first (error) bit
                ModbusException = ((byteValue & 0x80) == 0x80); // Use the first (error) bit
                if (ModbusException && (hexValuesSplit.Length > 2))
                {
                    ExceptionCode = Convert.ToInt16(hexValuesSplit[2], 16);
                }
            }

            if (hexValuesSplit.Length > 1)
            {
                Checksum   = hexValuesSplit[hexValuesSplit.Length - 2] + hexValuesSplit[hexValuesSplit.Length - 1];
                ChecksumOk = ModbusUtility.CheckModbusCRC(hexValuesSplit);
            }


            int  startByte     = 0;
            bool hasDataValues = false;

            switch (FunctionCode)
            {
            case 1:
            case 2:
                if (MessageRole == ModbusMessageRole.Request)
                {
                    StartAddress = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 2, 2);
                    CoilCount    = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 4, 2);
                }
                else
                {
                    ByteCount = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 2, 1);
                    startByte = 3;
                }
                break;

            case 3:
            case 4:
                // Request and response differs (we are slave):
                // RX 01 03 00 00 00 2C 44 17
                // TX 01 03 58 01 14 00 00 46 81 38 00 45 48 40 00 44 F1 80 00 42 47 99 9A 47 26 C9 00 47 1D 38 00 47 1D 3A 00 47 27 F8 00 43 82 F3 33 3F B3 33 33 43 2C 66 66 00 00 00 00 00 00 00 00 00 00 00 00 45 34 50 00 00 00 00 00 00 00 00 00 00 00 00 00 43 C9 80 00 41 49 99 9A 41 48 00 00 60 34
                if (MessageRole == ModbusMessageRole.Response)
                {
                    ByteCount     = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 2, 1);
                    startByte     = 3;
                    hasDataValues = true;
                }
                else
                {
                    StartAddress  = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 2, 2);
                    RegisterCount = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 4, 2);
                }

                break;

            case 6:
                // Request and response are the same:
                // RX 01 06 00 00 02 FD 49 2B
                // TX 01 06 00 00 02 FD 49 2B
                StartAddress        = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 2, 2);
                SingleRegisterValue = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 4, 2);
                startByte           = 6;
                hasDataValues       = true;
                break;

            case 16:
                // Request and response differs (we are slave):
                // RX 01 10 00 5B 00 10 20 00 00 3F 80 00 00 3F 80 CC CD 42 0C CC CD 41 DC 51 EC 41 2C 1E B8 40 B5 CC CD 40 F4 E1 48 40 FA FB DB
                // TX 01 10 00 5B 00 10 B0 16
                StartAddress  = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 2, 2);
                RegisterCount = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 4, 2);
                // Only request has byte count
                if (MessageRole == ModbusMessageRole.Request)
                {
                    ByteCount     = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, 6, 1);
                    startByte     = 7;
                    hasDataValues = true;
                }
                break;

            default:
                break;
            }

            if (((FunctionCode == 1) || (FunctionCode == 2)) && (MessageRole == ModbusMessageRole.Response))
            {
                for (int i = startByte; (i - startByte < ByteCount) && (i < hexValuesSplit.Length - 2); i++)
                {
                    var byteValue = ModbusUtility.ConvertHexStringToInt(hexValuesSplit, i, 1);
                    if (byteValue.HasValue)
                    {
                        Coils.Add(byteValue.Value);
                    }
                }
            }

            if (hasDataValues)
            {
                switch (DataType)
                {
                case ModbusDataType.Float:
                    // convert all float values from hex string
                    ConvertDataToFloats(useModiconFormat, hexValuesSplit, startByte);
                    break;

                case ModbusDataType.Integer:
                    // convert all integer values from hex string
                    ConvertDataToIntegers(hexValuesSplit, startByte);
                    break;

                case ModbusDataType.LongInt:
                    // convert all integer values from hex string
                    ConvertDataToLongInts(useModiconFormat, hexValuesSplit, startByte);
                    break;

                default:
                    break;
                }
            }
        }