Пример #1
0
        protected override IDeviceEventArgs RecivedData(byte[] data, int length)
        {
            CheckDisposed();
            SEDeviceEventArgs result = new SEDeviceEventArgs();

            byte[] temp = new byte[length];
            Array.Copy(data, 0, temp, 0, length);
            this.data.AddRange(temp);

            int lastIndex;

            ModbusResponse[] responses = ModbusResponse.ReadResponses(this.data.ToArray(), this.data.Count, out lastIndex);
            this.data.RemoveRange(0, lastIndex);
            foreach (var response in responses)
            {
                var request = Get(response as ModbusResponse);
                if (request != null && request.IsInternal && !response.Error)
                {
                    packages[(request as ModbusRequest).PackageId].SetData(response.PDU.Data, 0);
                }
                else if (response.Error)
                {
                    InvokeDeviceError(new SEDeviceErrorEventArgs {
                        ErrorCode = response.PDU.Data[0], Description = response.ErrorDescription
                    });
                }
            }
            return(result);
        }
Пример #2
0
        internal override ModbusResponse DeserializeResponse(ResponseBuffer buffer, ModbusRequest request, int timeout)
        {
            ModbusResponse result = null;

            while (result == null ||
                   result is ModbusCommErrorResponse responseCommErrorMessage &&
                   responseCommErrorMessage.ErrorCode != ModbusCommErrorCode.ResponseTimeout)
            {
                if (buffer.Count > 0)
                {
                    errorBuffer.Add(buffer[0]);
                    buffer.RemoveAt(0);
                }

                buffer.Read(timeout);

                try
                {
                    while (buffer[0] != 0x3a)
                    {
                        errorBuffer.Add(buffer[0]);
                        buffer.RemoveAt(0);
                        buffer.Read(timeout);
                    }
                }
                catch
                {
                    return(new ModbusCommErrorResponse(ModbusCommErrorCode.ResponseAsciiStartError, errorBuffer.Concat(buffer), request));
                }

                result = base.DeserializeResponse(buffer, request, timeout);
            }

            if (result is ModbusCommErrorResponse responseCommError)
            {
                result = new ModbusCommErrorResponse(responseCommError.ErrorCode, errorBuffer.Concat(responseCommError.ReceivedBytes), request);
            }
            else
            {
                var asciiEnd = buffer.Read(2, timeout);
                if (!asciiEnd.SequenceEqual(new byte[] { 13, 10 }))
                {
                    return(new ModbusCommErrorResponse(ModbusCommErrorCode.ResponseAsciiEndError, buffer, request));
                }

                if (errorBuffer.Count > 0)
                {
                    RaiseUnrecognized(buffer.Channel, errorBuffer.ToArray());
                    errorBuffer.Clear();
                }
            }

            return(result);
        }
Пример #3
0
        private ModbusRequest Get(ModbusResponse response)
        {
            ModbusRequest result = null;

            if (response != null)
            {
                lock (lockerRequestes)
                {
                    result = requestes.Find(x => x.MBAPHeader.TransactionId == response.MBAPHeader.TransactionId);
                    requestes.Remove(result);
                }
            }
            return(result);
        }
Пример #4
0
        internal override ModbusResponse DeserializeResponse(ResponseBuffer buffer, ModbusRequest request, int timeout)
        {
            ModbusResponse result = base.DeserializeResponse(buffer, request, timeout);

            while (result is ModbusCommErrorResponse responseCommErrorMessage &&
                   responseCommErrorMessage.ErrorCode != ModbusCommErrorCode.ResponseTimeout)
            {
                errorBuffer.Add(buffer[0]);
                buffer.RemoveAt(0);
                result = base.DeserializeResponse(buffer, request, timeout);
            }

            if (result is ModbusCommErrorResponse responseCommError)
            {
                result = new ModbusCommErrorResponse(responseCommError.ErrorCode, errorBuffer.Concat(responseCommError.ReceivedBytes), request);
            }
            else if (errorBuffer.Count > 0)
            {
                RaiseUnrecognized(buffer.Channel, errorBuffer.ToArray());
                errorBuffer.Clear();
            }

            return(result);
        }
Пример #5
0
        private bool IsException(ResponseBuffer buffer, ModbusRequest request, int timeout, out ModbusResponse responseMessage)
        {
            if ((Read(buffer, 1, timeout) & 0x80) == 0x80)
            {
                var codeValue = Read(buffer, 2, timeout);

                if (IsErrorCRC(buffer, 3, request, timeout))
                {
                    throw new ModbusCommException(ModbusCommErrorCode.ErrorCRC, buffer, request);
                }

                ModbusExceptionCode exceptionCode = ModbusExceptionCode.NotDefined;
                if (Enum.IsDefined(typeof(ModbusExceptionCode), codeValue))
                {
                    exceptionCode = (ModbusExceptionCode)codeValue;
                }

                responseMessage = new ModbusExceptionResponse(exceptionCode, request);
                return(true);
            }
            else
            {
                responseMessage = null;
                return(false);
            }
        }
Пример #6
0
 public HoldingRegisters(ModbusResponse response)
 {
     Data = response.Data;
 }
Пример #7
0
 public InputRegisters(ModbusResponse response)
 {
     this.Data = response.Data;
 }
Пример #8
0
 public DiscreteInputs(ModbusResponse response) : this(response.Data)
 {
 }
Пример #9
0
        private bool IsErrorLRC(ResponseBuffer buffer, int messageLength, ModbusRequest request, int timeout, out ModbusResponse responseMessage)
        {
            byte lrc = 0;

            foreach (var b in Read(buffer, 0, messageLength, timeout))
            {
                lrc += b;
            }

            lrc = (byte)(-lrc & 0xff);

            if (lrc != Read(buffer, messageLength, timeout))
            {
                responseMessage = new ModbusCommErrorResponse(ModbusCommErrorCode.ErrorLRC, buffer, request);
                return(true);
            }
            else
            {
                responseMessage = null;
                return(false);
            }
        }
Пример #10
0
 public Coils(ModbusResponse response)
 {
     this.Data   = response.Data;
     this._coils = new BitArray(Data);
 }