Exemplo n.º 1
0
        public void ValidateResponse(IMODBUSResponseMessage response)
        {
            var readHistoryArchiveResponse = response as ReadHistoryArchiveResponse;

            Debug.Assert(readHistoryArchiveResponse != null, "Argument response should be of type ReadHistoryArchiveResponse.");

            if (this.RecordSize != (readHistoryArchiveResponse.Data.Length - 1))
            {
                throw new FormatException("History record size does not equal history record specified.");
            }
        }
Exemplo n.º 2
0
        public void ValidateResponse(IMODBUSResponseMessage response)
        {
            var readShortsResponse = response as ReadShortsResponse;

            Debug.Assert(readShortsResponse != null, "Argument response should be of type ReadShortsResponse.");

            if (this.ShortRegisters.Count != (readShortsResponse.Data.Length - 1) / 2)
            {
                throw new FormatException("Number of short registers recieved does not equal number of short registers requested.");
            }
        }
Exemplo n.º 3
0
        public void ValidateResponse(IMODBUSResponseMessage response)
        {
            var readStringResponse = response as ReadStringResponse;

            Debug.Assert(readStringResponse != null, "Argument response should be of type ReadStringResponse.");

            if (this.StringRegister.Length != (readStringResponse.Data.Length - 1))
            {
                throw new FormatException("String length does not equal string length specified.");
            }
        }
Exemplo n.º 4
0
        public void ValidateResponse(IMODBUSResponseMessage response)
        {
            var writeBooleanResponse = response as WriteBooleanResponse;

            Debug.Assert(writeBooleanResponse != null, "Argument response should be of type ReadFloatsResponse.");

            if ((BitConverter.ToUInt32(this.Data, 0) != (BitConverter.ToUInt32(writeBooleanResponse.Data, 0))))
            {
                throw new FormatException("Boolean write response does not match boolean write request.");
            }
        }
Exemplo n.º 5
0
        internal void ValidateResponse(IMODBUSRequestMessage request, IMODBUSResponseMessage response)
        {
            if (request.FunctionCode != response.FunctionCode)
            {
                throw new IOException(String.Format(CultureInfo.InvariantCulture, "Received response with unexpected function code. Expected {0}, received {1}.", request.FunctionCode, response.FunctionCode));
            }

            if (request.UnitId != response.UnitId)
            {
                throw new IOException(String.Format(CultureInfo.InvariantCulture, "Response source unit does not match request. Expected {0}, received {1}.", response.UnitId, request.UnitId));
            }

            request.ValidateResponse(response);
        }
Exemplo n.º 6
0
        public void ValidateResponse(IMODBUSResponseMessage response)
        {
            var readDiscreteInputRegistersResponse = response as ReadDiscreteInputRegistersResponse;

            Debug.Assert(readDiscreteInputRegistersResponse != null, "Argument response should be of type ReadDiscreteInputRegistersResponse.");

            var discreteInputCount = this.DiscreteInputRegisters.Count();

            var expectedByteCount = (discreteInputCount / 8) + (discreteInputCount % 8) > 0 ? 1 : 0;

            if (expectedByteCount != readDiscreteInputRegistersResponse.Data[0])
            {
                throw new FormatException("Number of discrete input registers recieved does not equal number of discrete input registers requested.");
            }
        }
Exemplo n.º 7
0
        public void ValidateResponse(IMODBUSResponseMessage response)
        {
            var writeFloatsResponse = response as WriteFloatsResponse;

            Debug.Assert(writeFloatsResponse != null, "Argument response should be of type WriteFloatsResponse.");

            if (this.FloatRegisters.Count != writeFloatsResponse.Count)
            {
                throw new FormatException("Number of float registers acknowledged does not equal number of float registers written.");
            }

            if (this.FloatRegisters[0].Address.AbsoluteAddress != writeFloatsResponse.StartingAddress.AbsoluteAddress)
            {
                throw new FormatException("Starting address acknowledged does not match starting address written.");
            }
        }
Exemplo n.º 8
0
        public void ValidateResponse(IMODBUSResponseMessage response)
        {
            var readEventArchiveResponse = response as ReadEventArchiveResponse;

            Debug.Assert(readEventArchiveResponse != null, "Argument response should be of type ReadEventArchiveResponse.");
        }
Exemplo n.º 9
0
        internal virtual TResponseMessage UnicastMessage <TResponseMessage>(IMODBUSRequestMessage requestMessage)
            where TResponseMessage : IMODBUSResponseMessage, new()
        {
            if (TracingEnabled)
            {
                Trace.WriteLine(string.Format("Function {0} Request Started At: {1}", requestMessage.FunctionCode.ToString(), DateTime.Now.ToString("O"), "Transport"));
            }
            var lastException               = (Exception)null;
            var transactionStopWatch        = Stopwatch.StartNew();
            IMODBUSResponseMessage response = default(TResponseMessage);
            int  attempt = 0;
            bool success = false;

            lock (channelLock)
            {
                do
                {
                    try
                    {
                        attempt++;
                        if (RequestWriteDelayMilliseconds > 0)
                        {
                            if (TracingEnabled)
                            {
                                Trace.WriteLine(string.Format("Function {0} Request Write Delay: {1}ms", requestMessage.FunctionCode.ToString(), RequestWriteDelayMilliseconds.ToString("0.0")), "Transport");
                            }
                            TimedThreadBlocker.Wait(RequestWriteDelayMilliseconds);
                        }
                        Write(requestMessage);
                        if (ResponseReadDelayMilliseconds > 0)
                        {
                            if (TracingEnabled)
                            {
                                Trace.WriteLine(string.Format("Function {0} Request Read Delay: {1}", requestMessage.FunctionCode.ToString(), ResponseReadDelayMilliseconds.ToString("0.0")), "Transport");
                            }
                            TimedThreadBlocker.Wait(RequestWriteDelayMilliseconds);
                        }
                        response = ReadResponse <TResponseMessage>(requestMessage);
                        if (response is MODBUSErrorResponse)
                        {
                            throw new MODBUSSlaveException(response as MODBUSErrorResponse);
                        }
                        else
                        {
                            ValidateResponse(requestMessage, response);
                            transactionStopWatch.Stop();
                            if (TracingEnabled)
                            {
                                Trace.WriteLine(string.Format("Function {0} Request Completed in: {1}ms", requestMessage.FunctionCode.ToString(), transactionStopWatch.Elapsed.TotalMilliseconds.ToString()), "Transport");
                            }

                            return((TResponseMessage)response);
                        }
                    }
                    catch (Exception e)
                    {
                        lastException = e;
                        Trace.WriteLine(string.Format("Function {0} Request Error: {1}", requestMessage.FunctionCode.ToString(), e.Message), "Transport");
                        if (e is FormatException ||
                            e is NotImplementedException ||
                            e is TimeoutException ||
                            e is IOException)
                        {
                            if (attempt > NumberOfRetries)
                            {
                                goto Finish;
                            }
                            else
                            {
                                TimedThreadBlocker.Wait(WaitToRetryMilliseconds);
                            }
                        }
                        else
                        {
                            throw;
                        }
                    }
                    if (TracingEnabled)
                    {
                        Trace.WriteLine(string.Format("Function {0} Request Retry Number {1}", requestMessage.FunctionCode.ToString(), attempt.ToString()), "Transport");
                    }
                } while (!success && NumberOfRetries > attempt);
            }
Finish:
            throw new TimeoutException("Device not responding to requests", lastException);
        }