Ejemplo n.º 1
0
        private Response HandleWriteMultipleRegisters(Request request)
        {
            try
            {
                var response = new Response(request);

                //request.Data contains [byte count] [data]..[data]
                if (request.Count < Consts.MinCount || request.Count > Consts.MaxRegisterCountWrite || request.Count * 2 != request.Data.Length - 1)
                {
                    response.ErrorCode = ErrorCode.IllegalDataValue;
                }
                else if (request.Address < Consts.MinAddress || request.Address + request.Count > Consts.MaxAddress)
                {
                    response.ErrorCode = ErrorCode.IllegalDataAddress;
                }
                else
                {
                    try
                    {
                        var list = new List <Register>();
                        for (int i = 0; i < request.Count; i++)
                        {
                            var addr = (ushort)(request.Address + i);
                            var val  = request.Data.GetUInt16(i * 2 + 1);

                            var register = new Register {
                                Address = addr, Value = val
                            };
                            SetHoldingRegister(request.DeviceId, register);
                            list.Add(register);
                        }
                        RegisterWritten?.Invoke(this, new WriteEventArgs(request.DeviceId, list));
                    }
                    catch
                    {
                        response.ErrorCode = ErrorCode.SlaveDeviceFailure;
                    }
                }

                return(response);
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        private Response HandleWritSingleRegister(Request request)
        {
            var response = new Response(request);

            try
            {
                var val = request.Data.GetUInt16(0);

                if (request.Address < Consts.MinAddress || request.Address > Consts.MaxAddress)
                {
                    response.ErrorCode = ErrorCode.IllegalDataAddress;
                }
                else
                {
                    try
                    {
                        var register = new Register {
                            Address = request.Address, Value = val
                        };

                        SetHoldingRegister(request.DeviceId, register);
                        response.Data = request.Data;

                        RegisterWritten?.Invoke(this, new WriteEventArgs(request.DeviceId, register));
                    }
                    catch
                    {
                        response.ErrorCode = ErrorCode.SlaveDeviceFailure;
                    }
                }
            }
            catch
            {
                return(null);
            }

            return(response);
        }