Exemple #1
0
        /// <summary>
        /// Create modbus header for reading.
        /// </summary>
        /// <param name="myInvocationId"></param>
        /// <param name="myStartAddress"></param>
        /// <param name="myLength"></param>
        /// <param name="myFunctionCode"></param>
        /// <returns></returns>
        public static MemoryStream CreateReadHeader(UInt16 myInvocationId, UInt16 myStartAddress, UInt16 myLength, FunctionCode myFunctionCode)
        {
            var _Header = CreateGenericHeader(myInvocationId, 6, myFunctionCode);

            // Start address
            var _adr = BitConverter.GetBytes((Int16)IPAddress.HostToNetworkOrder((Int16)myStartAddress));

            _Header.WriteByte(_adr[0]);             // high byte
            _Header.WriteByte(_adr[1]);             // low  byte

            // Number of data to read
            var _length = BitConverter.GetBytes((Int16)IPAddress.HostToNetworkOrder((Int16)myLength));

            _Header.WriteByte(_length[0]);                      // high byte
            _Header.WriteByte(_length[1]);                      // low  byte

            return(_Header);
        }
Exemple #2
0
        // Create modbus header for write action
        public static MemoryStream CreateWriteHeader(UInt16 myInvocationId, UInt16 startAddress, UInt16 numData, Byte numBytes, FunctionCode myFunctionCode)
        {
            var _Header = CreateGenericHeader(myInvocationId, (UInt16)(numBytes + 5), myFunctionCode);

            _Header.WriteWord(startAddress, ByteOrder.HostToNetwork);

            if (myFunctionCode >= FunctionCode.WriteMultipleCoils)
            {
                _Header.WriteWord(numData, ByteOrder.HostToNetwork);
                _Header.WriteByte((Byte)(numBytes - 2));
            }

            return(_Header);
        }
Exemple #3
0
        public static MemoryStream CreateGenericHeader(UInt16 myInvocationId, UInt16 myMessageSize, FunctionCode myFunctionCode)
        {
            var _MemoryStream = new MemoryStream(20);

            _MemoryStream.WriteWord(myInvocationId);                         // Invocation/Transaction Identifier
            _MemoryStream.WriteWord(0);                                      // Protocol Identifier (Zero for Modbus/TCP)
            _MemoryStream.WriteWord(myMessageSize, ByteOrder.HostToNetwork); // Length Field/Message size
            _MemoryStream.WriteByte(0);                                      // Unit Identifier/Slave address (255 if not used)
            _MemoryStream.WriteByte(myFunctionCode.Value);                   // Function code

            return(_MemoryStream);
        }