예제 #1
0
        private Response HandleWriteMultipleCoils(Request request)
        {
            try
            {
                var response = new Response(request);

                var numBytes = (int)Math.Ceiling(request.Count / 8.0);
                if (request.Count < Consts.MinCount || request.Count > Consts.MaxCoilCountWrite || numBytes != request.Data.Length)
                {
                    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 <Coil>();
                        for (int i = 0; i < request.Count; i++)
                        {
                            var addr = (ushort)(request.Address + i);

                            var posByte = i / 8;
                            var posBit  = i % 8;

                            var mask = (byte)Math.Pow(2, posBit);
                            var val  = request.Data[posByte] & mask;

                            var coil = new Coil {
                                Address = addr, Value = (val > 0)
                            };
                            SetCoil(request.DeviceId, coil);
                            list.Add(coil);
                        }
                        CoilWritten?.Invoke(this, new WriteEventArgs(request.DeviceId, list));
                    }
                    catch
                    {
                        response.ErrorCode = ErrorCode.SlaveDeviceFailure;
                    }
                }

                return(response);
            }
            catch
            {
                return(null);
            }
        }
예제 #2
0
        private Response HandleWriteSingleCoil(Request request)
        {
            var response = new Response(request);

            try
            {
                var val = request.Data.GetUInt16(0);
                if (val != 0x0000 && val != 0xFF00)
                {
                    response.ErrorCode = ErrorCode.IllegalDataValue;
                }
                else if (request.Address < Consts.MinAddress || request.Address > Consts.MaxAddress)
                {
                    response.ErrorCode = ErrorCode.IllegalDataAddress;
                }
                else
                {
                    try
                    {
                        var coil = new Coil {
                            Address = request.Address, Value = (val > 0)
                        };

                        SetCoil(request.DeviceId, coil);
                        response.Data = request.Data;

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

            return(response);
        }