Esempio n. 1
0
 public override bool ClientDecode(
     ModbusCommand command,
     ByteArrayReader body)
 {
     return(true);
 }
Esempio n. 2
0
 /// <summary>
 /// Encode the server-side command toward the master remote device
 /// </summary>
 /// <param name="command"></param>
 /// <param name="body"></param>
 /// <returns>True when the function operated successfully</returns>
 public virtual bool ServerEncode(
     ModbusCommand command,
     ByteArrayWriter body)
 {
     return(false);
 }
Esempio n. 3
0
 /// <summary>
 /// Decode the incoming data from the remote master device
 /// to a server-side command
 /// </summary>
 /// <param name="command"></param>
 /// <param name="body"></param>
 /// <returns>True when the function operated successfully</returns>
 public virtual bool ServerDecode(
     ModbusCommand command,
     ByteArrayReader body)
 {
     return(false);
 }
Esempio n. 4
0
        CommResponse IProtocolCodec.ServerDecode(CommDataBase data)
        {
            var             server   = (ModbusServer)data.OwnerProtocol;
            ByteArrayReader incoming = data.IncomingData;

            //validate header first
            var length = incoming.Length;

            if (length < 4)
            {
                goto LabelUnknown;
            }

            //address
            var address = incoming.ReadByte();

            if (address == server.Address)
            {
                //function code
                var fncode = incoming.ReadByte();

                if (fncode < CommandCodecs.Length)
                {
                    //create a new command
                    var command = new ModbusCommand(fncode);
                    data.UserData            = command;
                    command.QueryTotalLength = 2; //= addr + fn (no crc)

                    //get the command codec
                    var codec = CommandCodecs[fncode];

                    //decode the command, where possible
                    var body = new ByteArrayReader(incoming.ReadBytes(length - 4));

                    if (codec.ServerDecode(command, body))
                    {
                        //calculate the CRC-16 over the received stream
                        ushort crcCalc;
                        unchecked
                        {
                            crcCalc = (ushort)ModbusRtuCodec.Crc16.Compute(
                                ((IByteArray)incoming).Data,
                                0,
                                command.QueryTotalLength);
                        }

                        //validate the CRC-16
                        ushort crcRead = ByteArrayHelpers.ReadUInt16LE(
                            ((IByteArray)incoming).Data,
                            command.QueryTotalLength);

                        if (crcRead == crcCalc)
                        {
                            return(new CommResponse(
                                       data,
                                       CommResponse.Ack));
                        }
                    }
                }
            }

            //exception
            return(new CommResponse(
                       data,
                       CommResponse.Ignore));

LabelUnknown:
            return(new CommResponse(
                       data,
                       CommResponse.Unknown));
        }