Beispiel #1
0
        internal int RequestBytesToRead(byte[] frameStart)
        {
            byte functionCode = frameStart[1];

            IModbusFunctionService service = ModbusFactory.GetFunctionServiceOrThrow(functionCode);

            return(service.GetRtuRequestBytesToRead(frameStart));
        }
Beispiel #2
0
        internal int ResponseBytesToRead(byte[] frameStart)
        {
            byte functionCode = frameStart[1];

            if (functionCode > Modbus.ExceptionOffset)
            {
                return(1);
            }

            IModbusFunctionService service = ModbusFactory.GetFunctionServiceOrThrow(functionCode);

            return(service.GetRtuResponseBytesToRead(frameStart));
        }
        public static IModbusFunctionService GetFunctionServiceOrThrow(this IModbusFactory factory, byte functionCode)
        {
            IModbusFunctionService service = factory.GetFunctionService(functionCode);

            if (service == null)
            {
                string msg = $"Function code {functionCode} not supported.";
                factory.Logger.Warning(msg);

                throw new NotImplementedException(msg);
            }

            return(service);
        }
Beispiel #4
0
        public IModbusMessage ApplyRequest(IModbusMessage request)
        {
            IModbusMessage response;

            try
            {
                //Try to get a handler for this function.
                IModbusFunctionService handler = _handlers.GetValueOrDefault(request.FunctionCode);

                //Check to see if we found a handler for this function code.
                if (handler == null)
                {
                    throw new InvalidModbusRequestException(SlaveExceptionCodes.IllegalFunction);
                }

                //Process the request
                response = handler.HandleSlaveRequest(request, DataStore);
            }
            catch (InvalidModbusRequestException ex)
            {
                // Catches the exception for an illegal function or a custom exception from the ModbusSlaveRequestReceived event.
                response = new SlaveExceptionResponse(
                    request.SlaveAddress,
                    (byte)(Modbus.ExceptionOffset + request.FunctionCode),
                    ex.ExceptionCode);
            }
#if NET45 || NET46
            catch (Exception ex)
            {
                //Okay - this is no beuno.
                response = new SlaveExceptionResponse(request.SlaveAddress,
                                                      (byte)(Modbus.ExceptionOffset + request.FunctionCode),
                                                      SlaveExceptionCodes.SlaveDeviceFailure);

                //Give the consumer a chance at seeing what the *(&& happened.
                Trace.WriteLine(ex.ToString());
            }
#else
            catch (Exception)
            {
                //Okay - this is no beuno.
                response = new SlaveExceptionResponse(request.SlaveAddress,
                                                      (byte)(Modbus.ExceptionOffset + request.FunctionCode),
                                                      SlaveExceptionCodes.SlaveDeviceFailure);
            }
#endif


            return(response);
        }
        internal int RequestBytesToRead(byte[] frameStart)
        {
            byte functionCode = frameStart[1];

            IModbusFunctionService service = _modbusFactory.GetFunctionService(functionCode);

            if (service == null)
            {
                string msg = $"Function code {functionCode} not supported.";
                Logger.Warning(msg);
                throw new NotImplementedException(msg);
            }

            return(service.GetRtuRequestBytesToRead(frameStart));
        }
Beispiel #6
0
        /// <summary>
        /// Simple Modbus serial RTU slave example.
        /// </summary>
        public static async Task StartModbusSerialRtuSlaveWithCustomMessage(CancellationToken cancellationToken)
        {
            using (SerialPort slavePort = new SerialPort(PrimarySerialPortName))
            {
                // configure serial port
                slavePort.BaudRate = 57600;
                slavePort.DataBits = 8;
                slavePort.Parity   = Parity.Even;
                slavePort.StopBits = StopBits.One;
                slavePort.Open();

                var adapter = new SerialPortAdapter(slavePort);

                var functionServices = new IModbusFunctionService[]
                {
                    new HmiBufferFunctionService()
                };

                var factory = new ModbusFactory(functionServices, true, new ConsoleModbusLogger(LoggingLevel.Debug));

                // create modbus slave
                var slaveNetwork = factory.CreateRtuSlaveNetwork(adapter);

                var acTechDataStore = new SlaveStorage();

                acTechDataStore.InputRegisters.StorageOperationOccurred   += (sender, args) => Console.WriteLine($"ACTECH Input registers: {args.Operation} starting at {args.StartingAddress}");
                acTechDataStore.HoldingRegisters.StorageOperationOccurred += (sender, args) => Console.WriteLine($"ACTECH Holding registers: {args.Operation} starting at {args.StartingAddress}");

                var danfossStore = new SlaveStorage();

                danfossStore.InputRegisters.StorageOperationOccurred   += (sender, args) => Console.WriteLine($"DANFOSS Input registers: {args.Operation} starting at {args.StartingAddress}");
                danfossStore.HoldingRegisters.StorageOperationOccurred += (sender, args) => Console.WriteLine($"DANFOSS Holding registers: {args.Operation} starting at {args.StartingAddress}");

                IModbusSlave actechSlave  = factory.CreateSlave(21, acTechDataStore);
                IModbusSlave danfossSlave = factory.CreateSlave(1, danfossStore);

                slaveNetwork.AddSlave(actechSlave);
                slaveNetwork.AddSlave(danfossSlave);

                await slaveNetwork.ListenAsync(cancellationToken);
            }
        }