Esempio n. 1
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();
        }
Esempio n. 2
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();
        }
Esempio n. 3
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();
        }
Esempio n. 4
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();
        }
Esempio n. 5
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();
        }
Esempio n. 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();

            //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();
        }