Пример #1
0
        internal byte[] Serialize()
        {
            var buffer = new DataBuffer(2);

            buffer.SetByte(0, DeviceId);

            var fn = (byte)Function;

            if (IsError)
            {
                fn = (byte)(fn & Consts.ErrorMask);
                buffer.AddByte((byte)ErrorCode);
            }
            else
            {
                switch (Function)
                {
                case FunctionCode.ReadCoils:
                case FunctionCode.ReadDiscreteInputs:
                case FunctionCode.ReadHoldingRegisters:
                case FunctionCode.ReadInputRegisters:
                    buffer.AddByte((byte)Data.Length);
                    buffer.AddBytes(Data.Buffer);
                    break;

                case FunctionCode.WriteMultipleCoils:
                case FunctionCode.WriteMultipleRegisters:
                    buffer.AddUInt16(Address);
                    buffer.AddUInt16(Count);
                    break;

                case FunctionCode.WriteSingleCoil:
                case FunctionCode.WriteSingleRegister:
                    buffer.AddUInt16(Address);
                    buffer.AddBytes(Data.Buffer);
                    break;

                case FunctionCode.EncapsulatedInterface:
                    buffer.AddByte((byte)MEIType);
                    switch (MEIType)
                    {
                    case MEIType.CANOpenGeneralReference:
                        if (Data?.Length > 0)
                        {
                            buffer.AddBytes(Data.Buffer);
                        }
                        break;

                    case MEIType.ReadDeviceInformation:
                        buffer.AddByte((byte)MEICategory);
                        buffer.AddByte(ConformityLevel);
                        buffer.AddByte((byte)(MoreRequestsNeeded ? 0xFF : 0x00));
                        buffer.AddByte(NextObjectId);
                        buffer.AddByte(ObjectCount);
                        buffer.AddBytes(Data.Buffer);
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            buffer.SetByte(1, fn);

            var crc = Checksum.CRC16(buffer.Buffer);

            buffer.AddBytes(crc);

            return(buffer.Buffer);
        }
Пример #2
0
        /// <summary>
        /// Serializes the request ready to send via serial.
        /// </summary>
        /// <returns></returns>
        public byte[] Serialize()
        {
            var buffer = new DataBuffer(2);

            buffer.SetByte(0, DeviceId);
            buffer.SetByte(1, (byte)Function);

            switch (Function)
            {
            case FunctionCode.ReadCoils:
            case FunctionCode.ReadDiscreteInputs:
            case FunctionCode.ReadHoldingRegisters:
            case FunctionCode.ReadInputRegisters:
                buffer.AddUInt16(Address);
                buffer.AddUInt16(Count);
                break;

            case FunctionCode.WriteMultipleCoils:
            case FunctionCode.WriteMultipleRegisters:
                buffer.AddUInt16(Address);
                buffer.AddUInt16(Count);
                if (Data?.Length > 0)
                {
                    buffer.AddBytes(Data.Buffer);
                }
                break;

            case FunctionCode.WriteSingleCoil:
            case FunctionCode.WriteSingleRegister:
                buffer.AddUInt16(Address);
                if (Data?.Length > 0)
                {
                    buffer.AddBytes(Data.Buffer);
                }
                break;

            case FunctionCode.EncapsulatedInterface:
                buffer.AddByte((byte)MEIType);
                switch (MEIType)
                {
                case MEIType.CANOpenGeneralReference:
                    if (Data?.Length > 0)
                    {
                        buffer.AddBytes(Data.Buffer);
                    }
                    break;

                case MEIType.ReadDeviceInformation:
                    buffer.AddByte((byte)MEICategory);
                    buffer.AddByte((byte)MEIObject);
                    break;

                default:
                    throw new NotImplementedException();
                }
                break;

            default:
                throw new NotImplementedException();
            }

            byte[] crc = Checksum.CRC16(buffer.Buffer);
            buffer.AddBytes(crc);

            return(buffer.Buffer);
        }
Пример #3
0
        /// <summary>
        /// Serializes the request ready to send via tcp.
        /// </summary>
        /// <returns></returns>
        internal byte[] Serialize()
        {
            var buffer = new DataBuffer(8);

            buffer.SetUInt16(0, TransactionId);
            buffer.SetUInt16(2, 0x0000);             // Protocol ID

            buffer.SetByte(6, DeviceId);
            buffer.SetByte(7, (byte)Function);

            switch (Function)
            {
            case FunctionCode.ReadCoils:
            case FunctionCode.ReadDiscreteInputs:
            case FunctionCode.ReadHoldingRegisters:
            case FunctionCode.ReadInputRegisters:
                buffer.AddUInt16(Address);
                buffer.AddUInt16(Count);
                break;

            case FunctionCode.WriteMultipleCoils:
            case FunctionCode.WriteMultipleRegisters:
                buffer.AddUInt16(Address);
                buffer.AddUInt16(Count);
                buffer.AddByte((byte)(Data?.Length ?? 0));
                if (Data?.Length > 0)
                {
                    buffer.AddBytes(Data.Buffer);
                }
                break;

            case FunctionCode.WriteSingleCoil:
            case FunctionCode.WriteSingleRegister:
                buffer.AddUInt16(Address);
                if (Data?.Length > 0)
                {
                    buffer.AddBytes(Data.Buffer);
                }
                break;

            case FunctionCode.EncapsulatedInterface:
                buffer.AddByte((byte)MEIType);
                switch (MEIType)
                {
                case MEIType.CANOpenGeneralReference:
                    if (Data?.Length > 0)
                    {
                        buffer.AddBytes(Data.Buffer);
                    }
                    break;

                case MEIType.ReadDeviceInformation:
                    buffer.AddByte((byte)MEICategory);
                    buffer.AddByte((byte)MEIObject);
                    break;

                default:
                    throw new NotImplementedException();
                }
                break;

            default:
                throw new NotImplementedException();
            }

            var len = buffer.Length - 6;

            buffer.SetUInt16(4, (ushort)len);

            return(buffer.Buffer);
        }