Ejemplo n.º 1
0
        private void CheckResponse <T>(Request request, TcpResponse <T> response) where T : struct
        {
            var tcpRequest = (TcpRequest)request;

            if (tcpRequest.TransactionId != response.TransactionId)
            {
                throw new DataCorruptedException("Response transaction id does not match " + _transactionId);
            }
            if (tcpRequest.ProtocolId != response.ProtocolId)
            {
                throw new DataCorruptedException("Response protocol id does not match " + PROTOCOL_ID);
            }
            if (response.ComputeMessageLength() != response.Length)
            {
                throw new DataCorruptedException("Actual message bytes length does not match " + response.Length);
            }
            if (request.SlaveAddress != response.SlaveAddress)
            {
                throw new MismatchDataException("Response slave address mismatch " + request.SlaveAddress);
            }
            if (request.FunctionCode != response.FunctionCode)
            {
                throw new MismatchDataException("Response function code mismatch " + request.FunctionCode);
            }
        }
Ejemplo n.º 2
0
            public override Response <T> Build()
            {
                if (responseBytes == null)
                {
                    throw new InvalidOperationException("ResponseBytes must be initialized");
                }

                if (responseBytes.Length < 8)
                {
                    throw new MissingDataException("ResponseBytes must have at least 8 bytes");
                }

                var response = new TcpResponse <T>
                {
                    ResponseBytes = (byte[])responseBytes.Clone(),
                    TransactionId = BytesNumeric.ToUInt16(responseBytes, 0),
                    ProtocolId    = BytesNumeric.ToUInt16(responseBytes, 2),
                    Length        = BytesNumeric.ToUInt16(responseBytes, 4),
                    SlaveAddress  = responseBytes[6],
                    FunctionCode  = responseBytes[7]
                };

                var data = new byte[responseBytes.Length - 8];

                Array.Copy(responseBytes, 8, data, 0, data.Length);
                response.Data = BytesStructure.FromBytes <T>(data);

                return(response);
            }
Ejemplo n.º 3
0
        public Response <T> SendRequest <T>(Request.BuilderBase builder) where T : struct
        {
            if (State == SessionState.Expired)
            {
                throw new InvalidOperationException("This session already expired");
            }

            if (State == SessionState.Identified)
            {
                builder.SetSlaveAddress(_slaveAddress);
            }

            var tcpBuilder = (TcpRequest.Builder)builder;

            tcpBuilder
            .SetTransactionId(++_transactionId)
            .SetProtocolId(PROTOCOL_ID)
            .AutoComputeLength();

            var request = tcpBuilder.Build();

            var responseBytes =
                _modbusProtocol.SendForResult(request.RequestBytes, TcpResponse <T> .ComputeResponseBytesLength());

            if (responseBytes == null)
            {
                return(null);
            }

            var response = (TcpResponse <T>) new TcpResponse <T> .Builder()
                           .SetResponseBytes(responseBytes)
                           .Build();

            if (EnableCheck)
            {
                CheckResponse(request, response);
            }

            return(response);
        }