예제 #1
0
        void IProtocolCodec.ClientEncode(CommDataBase data)
        {
            var client = (ModbusClient)data.OwnerProtocol;
            var command = (ModbusCommand)data.UserData;
            var fncode = command.FunctionCode;

            //encode the command body, if applies
            var body = new ByteArrayWriter();
            var codec = CommandCodecs[fncode];
            if (codec != null)
                codec.ClientEncode(command, body);

            //create a writer for the outgoing data
            var writer = new ByteArrayWriter();

            //unit identifier (address)
            writer.WriteByte(client.Address);

            //function code
            writer.WriteByte(fncode);

            //body data
            writer.WriteBytes(body);

            //CRC-16
            ushort crc = ByteArrayHelpers.CalcCRC16(
                writer.ToArray(),
                0,
                writer.Length);

            writer.WriteInt16LE((short)crc);

            data.OutgoingData = writer.ToReader();
        }
예제 #2
0
        void IProtocolCodec.ServerEncode(CommDataBase data)
        {
            ModbusServer    ownerProtocol    = (ModbusServer)data.OwnerProtocol;
            ModbusCommand   userData         = (ModbusCommand)data.UserData;
            byte            functionCode     = userData.FunctionCode;
            ByteArrayWriter byteArrayWriter1 = new ByteArrayWriter();

            ModbusCodecBase.CommandCodecs[(int)functionCode]?.ServerEncode(userData, byteArrayWriter1);
            if (userData.ExceptionCode == (byte)0)
            {
                int length = byteArrayWriter1.Length;
            }
            ByteArrayWriter byteArrayWriter2 = new ByteArrayWriter();

            byteArrayWriter2.WriteByte(ownerProtocol.Address);
            if (userData.ExceptionCode == (byte)0)
            {
                byteArrayWriter2.WriteByte(userData.FunctionCode);
                byteArrayWriter2.WriteBytes(byteArrayWriter1);
            }
            else
            {
                byteArrayWriter2.WriteByte((byte)((uint)userData.FunctionCode | 128U));
                byteArrayWriter2.WriteByte(userData.ExceptionCode);
            }
            ushort num = ByteArrayHelpers.CalcCRC16(byteArrayWriter2.ToArray(), 0, byteArrayWriter2.Length);

            byteArrayWriter2.WriteInt16LE((short)num);
            data.OutgoingData = byteArrayWriter2.ToReader();
        }
예제 #3
0
        /// <summary>
        /// Helper for packing the discrete data outgoing as a bit-array
        /// </summary>
        /// <param name="command"></param>
        /// <param name="body"></param>
        internal static void PushDiscretes(
            ModbusCommand command,
            ByteArrayWriter body)
        {
            var count = command.Count;

            body.WriteByte((byte)((count + 7) / 8));

            int i    = 0;
            int cell = 0;

            for (int k = 0; k < count; k++)
            {
                if (command.Data[k] != 0)
                {
                    cell |= (1 << i);
                }

                if (++i == 8)
                {
                    body.WriteByte((byte)cell);
                    i    = 0;
                    cell = 0;
                }
            }

            if (i > 0)
            {
                body.WriteByte((byte)cell);
            }
        }
예제 #4
0
        void IProtocolCodec.ServerEncode(CommDataBase data)
        {
            ModbusServer    ownerProtocol    = (ModbusServer)data.OwnerProtocol;
            ModbusCommand   userData         = (ModbusCommand)data.UserData;
            byte            functionCode     = userData.FunctionCode;
            ByteArrayWriter byteArrayWriter1 = new ByteArrayWriter();

            ModbusCodecBase.CommandCodecs[(int)functionCode]?.ServerEncode(userData, byteArrayWriter1);
            int             num = userData.ExceptionCode == (byte)0 ? 2 + byteArrayWriter1.Length : 3;
            ByteArrayWriter byteArrayWriter2 = new ByteArrayWriter();

            byteArrayWriter2.WriteUInt16BE((ushort)userData.TransId);
            byteArrayWriter2.WriteInt16BE((short)0);
            byteArrayWriter2.WriteInt16BE((short)num);
            byteArrayWriter2.WriteByte(ownerProtocol.Address);
            if (userData.ExceptionCode == (byte)0)
            {
                byteArrayWriter2.WriteByte(userData.FunctionCode);
                byteArrayWriter2.WriteBytes(byteArrayWriter1);
            }
            else
            {
                byteArrayWriter2.WriteByte((byte)((uint)userData.FunctionCode | 128U));
                byteArrayWriter2.WriteByte(userData.ExceptionCode);
            }
            data.OutgoingData = byteArrayWriter2.ToReader();
        }
예제 #5
0
        void IProtocolCodec.ServerEncode(CommDataBase data)
        {
            var server  = (ModbusServer)data.OwnerProtocol;
            var command = (ModbusCommand)data.UserData;
            var fncode  = command.FunctionCode;

            //encode the command body, if applies
            var body  = new ByteArrayWriter();
            var codec = CommandCodecs[fncode];

            if (codec != null)
            {
                codec.ServerEncode(command, body);
            }

            //calculate length field
            var length = (command.ExceptionCode == 0)
                ? 2 + body.Length
                : 3;

            //create a writer for the outgoing data
            var writer = new ByteArrayWriter();

            //unit identifier (address)
            writer.WriteByte(server.Address);

            if (command.ExceptionCode == 0)
            {
                //function code
                writer.WriteByte(fncode);

                //body data
                writer.WriteBytes(body);
            }
            else
            {
                //function code
                writer.WriteByte((byte)(command.FunctionCode | 0x80));

                //exception code
                writer.WriteByte(command.ExceptionCode);
            }

            //CRC-16
            ushort crc;

            unchecked
            {
                crc = (ushort)ModbusRtuCodec.Crc16.Compute(
                    ((IByteArray)writer).Data,
                    0,
                    writer.Length);
            }

            writer.WriteUInt16LE(crc);

            data.OutgoingData = writer.ToReader();
        }
예제 #6
0
        void IProtocolCodec.ServerEncode(CommDataBase data)
        {
            var server  = (ModbusServer)data.OwnerProtocol;
            var command = (ModbusCommand)data.UserData;
            var fncode  = command.FunctionCode;

            //encode the command body, if applies
            var body  = new ByteArrayWriter();
            var codec = CommandCodecs[fncode];

            if (codec != null)
            {
                codec.ServerEncode(command, body);
            }

            //calculate length field
            var length = (command.ExceptionCode == 0)
                ? 2 + body.Length
                : 3;

            //create a writer for the outgoing data
            var writer = new ByteArrayWriter();

            //transaction-id
            writer.WriteUInt16BE((ushort)command.TransId);

            //protocol-identifier
            writer.WriteInt16BE(0);

            //message length
            writer.WriteInt16BE((short)length);

            //unit identifier (address)
            writer.WriteByte(data.Address);

            if (command.ExceptionCode == 0)
            {
                //function code
                writer.WriteByte(command.FunctionCode);

                //body data
                writer.WriteBytes(body);
            }
            else
            {
                //function code
                writer.WriteByte((byte)(command.FunctionCode | 0x80));

                //exception code
                writer.WriteByte(command.ExceptionCode);
            }

            data.OutgoingData = writer.ToReader();
        }
예제 #7
0
        internal static void PushDiscretes(ModbusCommand command, ByteArrayWriter body)
        {
            int count = command.Count;

            body.WriteByte((byte)((count + 7) / 8));
            for (int index = 0; index < count; ++index)
            {
                byte num1 = (byte)((uint)command.Data[index] >> 8);
                byte num2 = (byte)((uint)command.Data[index] & (uint)byte.MaxValue);
                body.WriteByte(num1);
                body.WriteByte(num2);
            }
        }
예제 #8
0
        void IProtocolCodec.ClientEncode(CommDataBase data)
        {
            ModbusClient    ownerProtocol    = (ModbusClient)data.OwnerProtocol;
            ModbusCommand   userData         = (ModbusCommand)data.UserData;
            byte            functionCode     = userData.FunctionCode;
            ByteArrayWriter byteArrayWriter1 = new ByteArrayWriter();

            ModbusCodecBase.CommandCodecs[(int)functionCode]?.ClientEncode(userData, byteArrayWriter1);
            ByteArrayWriter byteArrayWriter2 = new ByteArrayWriter();

            byteArrayWriter2.WriteByte(ownerProtocol.Address);
            byteArrayWriter2.WriteByte(functionCode);
            byteArrayWriter2.WriteBytes(byteArrayWriter1);
            ushort num = ByteArrayHelpers.CalcCRC16(byteArrayWriter2.ToArray(), 0, byteArrayWriter2.Length);

            byteArrayWriter2.WriteInt16LE((short)num);
            data.OutgoingData = byteArrayWriter2.ToReader();
        }
 public override void ServerEncode(
     ModbusCommand command,
     ByteArrayWriter body)
 {
     var count = command.Count;
     body.WriteByte((byte)(count * 2));
     for (int i = 0; i < count; i++)
         body.WriteUInt16BE(command.Data[i]);
 }
예제 #10
0
        public override async Task WriteBuffer(ByteArrayWriter Writer, int CurrentOffset)
        {
            Writer.WriteInt32(Size);

            if (Size < 0)
            {
                Writer.WriteBytes(Encoding.Unicode.GetBytes(String));

                Writer.WriteByte(0);
                Writer.WriteByte(0);
            }
            else
            {
                Writer.WriteBytes(Encoding.ASCII.GetBytes(String));

                Writer.WriteByte(0);
            }
        }
예제 #11
0
        void IProtocolCodec.ClientEncode(CommDataBase data)
        {
            ModbusClient    ownerProtocol    = (ModbusClient)data.OwnerProtocol;
            ModbusCommand   userData         = (ModbusCommand)data.UserData;
            byte            functionCode     = userData.FunctionCode;
            ByteArrayWriter byteArrayWriter1 = new ByteArrayWriter();

            ModbusCodecBase.CommandCodecs[(int)functionCode]?.ClientEncode(userData, byteArrayWriter1);
            int             num = 2 + byteArrayWriter1.Length;
            ByteArrayWriter byteArrayWriter2 = new ByteArrayWriter();

            byteArrayWriter2.WriteUInt16BE((ushort)userData.TransId);
            byteArrayWriter2.WriteInt16BE((short)0);
            byteArrayWriter2.WriteInt16BE((short)num);
            byteArrayWriter2.WriteByte(ownerProtocol.Address);
            byteArrayWriter2.WriteByte(functionCode);
            byteArrayWriter2.WriteBytes(byteArrayWriter1);
            data.OutgoingData = byteArrayWriter2.ToReader();
        }
예제 #12
0
        void IProtocolCodec.ClientEncode(CommDataBase data)
        {
            var client  = (ModbusClient)data.OwnerProtocol;
            var command = (ModbusCommand)data.UserData;
            var fncode  = command.FunctionCode;

            //encode the command body, if applies
            var body  = new ByteArrayWriter();
            var codec = CommandCodecs[fncode];

            if (codec != null)
            {
                codec.ClientEncode(command, body);
            }

            //create a writer for the outgoing data
            var writer = new ByteArrayWriter();

            //unit identifier (address)
            writer.WriteByte(client.Address);

            //function code
            writer.WriteByte(fncode);

            //body data
            writer.WriteBytes(body);

            //CRC-16
            ushort crc;

            unchecked
            {
                crc = (ushort)ModbusRtuCodec.Crc16.Compute(
                    ((IByteArray)writer).Data,
                    0,
                    writer.Length
                    );
            }

            writer.WriteUInt16LE(crc);

            data.OutgoingData = writer.ToReader();
        }
        public override void ServerEncode(ModbusCommand command, ByteArrayWriter body)
        {
            int count = command.Count;

            body.WriteByte((byte)(count * 2));
            for (int index = 0; index < count; ++index)
            {
                body.WriteUInt16BE(command.Data[index]);
            }
        }
예제 #14
0
 public override async Task WriteBuffer(ByteArrayWriter Writer, int CurrentOffset)
 {
     if (byteValue.HasValue)
     {
         Writer.WriteByte(byteValue.Value);
     }
     else
     {
         await NameIndexValue.WriteBuffer(Writer, CurrentOffset);
     }
 }
예제 #15
0
        public override void ClientEncode(ModbusCommand command, ByteArrayWriter body)
        {
            ModbusCodecBase.PushRequestHeader(command, body);
            int count = command.Count;

            body.WriteByte((byte)(count * 2));
            for (int index = 0; index < count; ++index)
            {
                body.WriteUInt16BE(command.Data[index]);
            }
        }
예제 #16
0
        void IProtocolCodec.ClientEncode(CommDataBase data)
        {
            var client  = (ModbusClient)data.OwnerProtocol;
            var command = (ModbusCommand)data.UserData;
            var fncode  = command.FunctionCode;

            //encode the command body, if applies
            var body  = new ByteArrayWriter();
            var codec = CommandCodecs[fncode];

            if (codec != null)
            {
                codec.ClientEncode(command, body);
            }

            //calculate length field
            var length = 2 + body.Length;

            //create a writer for the outgoing data
            var writer = new ByteArrayWriter();

            //transaction-id
            writer.WriteUInt16BE((ushort)command.TransId);

            //protocol-identifier (always zero)
            writer.WriteInt16BE(0);

            //message length
            writer.WriteInt16BE((short)length);

            //unit identifier (address)
            writer.WriteByte(client.Address);

            //function code
            writer.WriteByte(fncode);

            //body data
            writer.WriteBytes(body);

            data.OutgoingData = writer.ToReader();
        }
        public override void ServerEncode(
            ModbusCommand command,
            ByteArrayWriter body)
        {
            var count = command.Count;

            body.WriteByte((byte)(count * 2));
            for (int i = 0; i < count; i++)
            {
                body.WriteUInt16BE(command.Data[i]);
            }
        }
예제 #18
0
        public override void ServerEncode(
            ModbusCommand command,
            ByteArrayWriter body)
        {
            body.WriteByte(_device);
            var count = command.Count;
            body.WriteByte((byte)(count));
            body.WriteByte(0xFF);
            body.WriteByte(0xFF);
            body.WriteInt32BE(_address);

            if (_category == 0)
            {
                for (int i = 0; i < count/2; i++)
                {
                    UInt16 v = (UInt16) GetRandomNumber(0, 25);
                    byte h = (byte)(v >> 8);
                    byte l = (byte)(v &0x00FF);
                    UInt16 t = (UInt16)((l << 8) + h);
                    body.WriteUInt16BE(t);
                }
            }
            else if (_category == 2)
            {
                for (int i = 0; i < count / 2; i++)
                {
                    body.WriteUInt16BE((UInt16)GetRandomNumber(0, 5));
                }

            }
            else 
            {
                for (int i = 0; i < count / 2; i++)
                {
                    body.WriteUInt16BE((UInt16)GetRandomNumber(0, 50));
                }

            }

        }
        public override void ClientEncode(
            ModbusCommand command,
            ByteArrayWriter body)
        {
            ModbusCodecBase.PushRequestHeader(
                command,
                body);

            var count = command.Count;
            body.WriteByte((byte)(count * 2));
            for (int i = 0; i < count; i++)
                body.WriteUInt16BE(command.Data[i]);
        }
예제 #20
0
        /// <summary>
        /// Helper for packing the discrete data outgoing as a bit-array
        /// </summary>
        /// <param name="command"></param>
        /// <param name="body"></param>
        internal static void PushDiscretes(
            ModbusCommand command,
            ByteArrayWriter body)
        {
            var count         = ((byte)((command.Count + 7) / 8));
            var wholeWords    = command.Count / 16;
            var remainingBits = command.Count % 16;

            body.WriteByte(count);
            int k;

            for (k = 0; k < wholeWords; k++)
            {
                var hb = (byte)(command.Data[k] >> 8);
                var lb = (byte)(command.Data[k] & 0x00FF);
                body.WriteByte(hb);
                body.WriteByte(lb);
            }
            if (remainingBits > 0)
            {
                byte bitMask     = 1;
                byte cell        = 0;
                byte currentByte = (byte)(command.Data[k] >> 8);
                for (int j = 0; j < remainingBits; j++)
                {
                    if (j == 8)
                    {
                        body.WriteByte(cell);
                        currentByte = (byte)(command.Data[k] & 0x00FF);
                        bitMask     = 1;
                        cell        = 0;
                    }
                    cell   |= (byte)(currentByte & bitMask);
                    bitMask = (byte)(bitMask << 1);
                }
                body.WriteByte(cell);
            }
        }
예제 #21
0
        void IProtocolCodec.ClientEncode(CommDataBase data)
        {
            var client = (ModbusClient)data.OwnerProtocol;
            var command = (ModbusCommand)data.UserData;
            var fncode = command.FunctionCode;

            //encode the command body, if applies
            var body = new ByteArrayWriter();
            var codec = CommandCodecs[fncode];
            if (codec != null)
                codec.ClientEncode(command, body);

            //calculate length field
            var length = 2 + body.Length;

            //create a writer for the outgoing data
            var writer = new ByteArrayWriter();

            //transaction-id 
            writer.WriteUInt16BE((ushort)command.TransId);

            //protocol-identifier (always zero)
            writer.WriteInt16BE(0);

            //message length
            writer.WriteInt16BE((short)length);

            //unit identifier (address)
            writer.WriteByte(client.Address);

            //function code
            writer.WriteByte(fncode);

            //body data
            writer.WriteBytes(body);

            data.OutgoingData = writer.ToReader();
        }
        public override void ServerEncode(
            ModbusCommand command,
            ByteArrayWriter body)
        {
            body.WriteByte(_device);
            var count = command.Count;

            body.WriteByte((byte)(count));
            body.WriteByte(0xFF);
            body.WriteByte(0xFF);
            body.WriteInt32BE(_address);

            if (_category == 0)
            {
                for (int i = 0; i < count / 2; i++)
                {
                    UInt16 v = (UInt16)GetRandomNumber(0, 25);
                    byte   h = (byte)(v >> 8);
                    byte   l = (byte)(v & 0x00FF);
                    UInt16 t = (UInt16)((l << 8) + h);
                    body.WriteUInt16BE(t);
                }
            }
            else if (_category == 2)
            {
                for (int i = 0; i < count / 2; i++)
                {
                    body.WriteUInt16BE((UInt16)GetRandomNumber(0, 5));
                }
            }
            else
            {
                for (int i = 0; i < count / 2; i++)
                {
                    body.WriteUInt16BE((UInt16)GetRandomNumber(0, 50));
                }
            }
        }
예제 #23
0
        void IProtocolCodec.ClientEncode(CommDataBase data)
        {
            var client  = (ModbusClient)data.OwnerProtocol;
            var command = (ModbusCommand)data.UserData;
            var fncode  = command.FunctionCode;

            //encode the command body, if applies
            var body  = new ByteArrayWriter();
            var codec = CommandCodecs[fncode];

            if (codec != null)
            {
                codec.ClientEncode(command, body);
            }

            //create a writer for the outgoing data
            var writer = new ByteArrayWriter();

            //unit identifier (address)
            writer.WriteByte(client.Address);

            //function code
            writer.WriteByte(fncode);

            //body data
            writer.WriteBytes(body);

            //CRC-16
            ushort crc = ByteArrayHelpers.CalcCRC16(
                writer.ToArray(),
                0,
                writer.Length);

            writer.WriteInt16LE((short)crc);

            data.OutgoingData = writer.ToReader();
        }
예제 #24
0
        /// <summary>
        /// Helper for packing the discrete data outgoing as a bit-array
        /// </summary>
        /// <param name="command"></param>
        /// <param name="body"></param>
        internal static void PushDiscretes(
            ModbusCommand command,
            ByteArrayWriter body)
        {
            var count = ((byte)((command.Count + 7) / 8));

            body.WriteByte(count);
            for (int k = 0; k < count / 2; k++)
            {
                var hb = (byte)(command.Data[k] >> 8);
                var lb = (byte)(command.Data[k] & 0x00FF);
                body.WriteByte(hb);
                body.WriteByte(lb);
            }

            //var count = command.Count;
            //body.WriteByte((byte)((count + 7) / 8));

            //int i = 0;
            //int cell = 0;
            //for (int k = 0; k < count; k++)
            //{
            //    if (command.Data[k] != 0)
            //        cell |= (1 << i);

            //    if (++i == 8)
            //    {
            //        body.WriteByte((byte)cell);
            //        i = 0;
            //        cell = 0;
            //    }
            //}

            //if (i > 0)
            //    body.WriteByte((byte)cell);
        }
        public override void ClientEncode(
            ModbusCommand command,
            ByteArrayWriter body)
        {
            ModbusTcpCodec.PushRequestHeader(
                command,
                body);

            var count = command.Count;

            body.WriteByte((byte)(count * 2));
            for (int i = 0; i < count; i++)
            {
                body.WriteUInt16BE(command.Data[i]);
            }
        }
예제 #26
0
        void IProtocolCodec.ServerEncode(CommDataBase data)
        {
            var server = (ModbusServer)data.OwnerProtocol;
            var command = (ModbusCommand)data.UserData;
            var fncode = command.FunctionCode;

            //encode the command body, if applies
            var body = new ByteArrayWriter();
            var codec = CommandCodecs[fncode];
            if (codec != null)
                codec.ServerEncode(command, body);

            //calculate length field
            var length = (command.ExceptionCode == 0)
                ? 2 + body.Length
                : 3;

            //create a writer for the outgoing data
            var writer = new ByteArrayWriter();

            //unit identifier (address)
            writer.WriteByte(server.Address);

            if (command.ExceptionCode == 0)
            {
                //function code
                writer.WriteByte(command.FunctionCode);

                //body data
                writer.WriteBytes(body);
            }
            else
            {
                //function code
                writer.WriteByte((byte)(command.FunctionCode | 0x80));

                //exception code
                writer.WriteByte(command.ExceptionCode);
            }

            //CRC-16
            ushort crc = ByteArrayHelpers.CalcCRC16(
                writer.ToArray(),
                0,
                writer.Length);

            writer.WriteInt16LE((short)crc);

            data.OutgoingData = writer.ToReader();
        }
예제 #27
0
        void IProtocolCodec.ServerEncode(CommDataBase data)
        {
            var server = (ModbusServer)data.OwnerProtocol;
            var command = (ModbusCommand)data.UserData;
            var fncode = command.FunctionCode;

            //encode the command body, if applies
            var body = new ByteArrayWriter();
            var codec = CommandCodecs[fncode];
            if (codec != null)
                codec.ServerEncode(command, body);

            //calculate length field
            var length = (command.ExceptionCode == 0)
                ? 2 + body.Length
                : 3;

            //create a writer for the outgoing data
            var writer = new ByteArrayWriter();

            //transaction-id
            writer.WriteUInt16BE((ushort)command.TransId);

            //protocol-identifier
            writer.WriteInt16BE(0);

            //message length
            writer.WriteInt16BE((short)length);

            //unit identifier (address)
            writer.WriteByte(server.Address);

            if (command.ExceptionCode == 0)
            {
                //function code
                writer.WriteByte(command.FunctionCode);

                //body data
                writer.WriteBytes(body);
            }
            else
            {
                //function code
                writer.WriteByte((byte)(command.FunctionCode | 0x80));

                //exception code
                writer.WriteByte(command.ExceptionCode);
            }

            data.OutgoingData = writer.ToReader();
        }
예제 #28
0
 /// <summary>
 /// Helper for packing the discrete data outgoing as a bit-array
 /// </summary>
 /// <param name="command"></param>
 /// <param name="body"></param>
 internal static void PushDiscretes(
     ModbusCommand command,
     ByteArrayWriter body)
 {
     var count = ((byte)((command.Count + 7) / 8));
     var wholeWords = command.Count / 16;
     var remainingBits = command.Count % 16;
     body.WriteByte(count);
     int k;
     for (k = 0; k < wholeWords; k++)
     {
         var hb = (byte)(command.Data[k] >> 8);
         var lb = (byte)(command.Data[k] & 0x00FF);
         body.WriteByte(hb);
         body.WriteByte(lb);
     }
     if (remainingBits > 0)
     {
         byte bitMask = 1;
         byte cell = 0;
         byte currentByte = (byte)(command.Data[k] >> 8);
         for (int j = 0; j < remainingBits; j++)
         {
             if (j == 8)
             {
                 body.WriteByte(cell);
                 currentByte = (byte)(command.Data[k] & 0x00FF);
                 bitMask = 1;
                 cell = 0;
             }
             cell |= (byte)(currentByte & bitMask);
             bitMask = (byte)(bitMask << 1);
         }
         body.WriteByte(cell);
     }
 }