Exemplo n.º 1
0
        private static async Task ConnecionLoop(NetworkStream connectedStream, Options options, IConsole diWindow, IConsole doWindow, IConsole inputRegistersWindow, IConsole holdingRegistersWindow)
        {
            var masterMemory = new ModbusMemoryMap();
            var client       = new ModbusClient(new ModbusTCPTransport(connectedStream));
            var device       = new ModbusDevice(masterMemory, options.SlaveId);

            while (!Console.KeyAvailable)
            {
                if (options.InputCount > 0)
                {
                    await client.ReadDiscreteInputsAsync(device, options.InputAddress, options.InputCount, CancellationToken.None);

                    diWindow.PrintAt(0, 0, masterMemory.InputCoils.ToString(options.InputAddress, options.InputCount));
                }

                if (options.CoilsCount > 0)
                {
                    await client.ReadCoilsAsync(device, options.CoilsAddress, options.CoilsCount, CancellationToken.None);

                    doWindow.PrintAt(0, 0, masterMemory.OutputCoils.ToString(options.CoilsAddress, options.CoilsCount));
                }

                if (options.RegisterCount > 0)
                {
                    await client.ReadInputRegistersAsync(device, options.RegisterAddress, options.RegisterCount, CancellationToken.None);

                    inputRegistersWindow.PrintAt(0, 0, masterMemory.InputRegisters.ToString(options.RegisterAddress, options.RegisterCount));
                }

                if (options.HoldingRegisterCount > 0)
                {
                    await client.ReadHoldingRegistersAsync(device, options.HoldingRegisterAddress, options.HoldingRegisterCount, CancellationToken.None);

                    holdingRegistersWindow.PrintAt(0, 0, masterMemory.OutputRegisters.ToString(options.HoldingRegisterAddress, options.HoldingRegisterCount));
                }

                await Task.Delay(options.ScanRate);
            }
        }
Exemplo n.º 2
0
    private async Task <byte[]> ReadBlockAsync(ModbusReadBlockConfiguration readBlock)
    {
        switch (readBlock.Function)
        {
        case ModbusFunctions.ReadCoilStatus:
        {
            var result = await modbusClient.ReadCoilsAsync(readBlock.AddressOffset, readBlock.Count);

            return(result);
        }

        case ModbusFunctions.ReadInputStatus:
        {
            var result = await modbusClient.ReadInputsAsync(readBlock.AddressOffset, readBlock.Count);

            return(result);
        }

        case ModbusFunctions.ReadHoldingRegister:
        {
            var result = await modbusClient.ReadHoldingRegistersAsync(readBlock.AddressOffset, readBlock.Count);

            return(readBlock.ByteSwap ? result.ReadDataByteSwap() : result.RawData);
        }

        case ModbusFunctions.ReadInputRegister:
        {
            var result = await modbusClient.ReadInputRegistersAsync(readBlock.AddressOffset, readBlock.Count);

            return(readBlock.ByteSwap ? result.ReadDataByteSwap() : result.RawData);
        }

        default:
            return(null);
        }
    }