Esempio n. 1
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 request = builder.Build();

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

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

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

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

            return(response);
        }
Esempio n. 2
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);
        }
Esempio 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(_settings.ChildSlaveAddress);
            }

            var requestBytes = builder.Build().RequestBytes;

            var parentBuilder = new TcpRequest.Builder()
                                .SetFunctionCode(_settings.ParentFunctionCode)
                                .SetDataBytes(requestBytes);

            var response = _modbusSession.SendRequest <T>(parentBuilder);

            return(response);
        }
Esempio n. 4
0
 public Task <Response <T> > SendRequestAsync <T>(Request.BuilderBase builder) where T : struct
 {
     return(Task.Run(() => SendRequest <T>(builder)));
 }