Esempio n. 1
0
        public async Task <IQueryResult <bool> > ReadCoilStatusAsync(ushort coilAddress, string dataTitle)
        {
            await _semaphoreSlim.WaitAsync();

            IQueryResult <bool> queryResult = _queryResultFactory.CreateDefaultQueryResult <bool>();

            if (!CheckConnection(queryResult))
            {
                return(queryResult);
            }

            try
            {
                queryResult.Result = (await _currentModbusMaster.ReadCoilsAsync(_slaveId, coilAddress, 1))[0];
                LogQuery(true, dataTitle,
                         "Fun:1" + " Addr:" + coilAddress + " Num:" + 1 + " Data:" + queryResult.Result);
                queryResult.IsSuccessful = true;
            }
            catch (Exception e)
            {
                LogQuery(false, dataTitle, "Fun:1" + " Addr:" + coilAddress + " Num:" + 1, exception: e);
                queryResult.IsSuccessful = false;
                queryResult.AddExeption(e);
            }

            _semaphoreSlim.Release(1);
            return(queryResult);
        }
Esempio n. 2
0
        public async Task <IQueryResult <bool> > ReadCoilStatusAsync(ushort coilAddress, string dataTitle)
        {
            IQueryResult <bool> queryResult = _queryResultFactory.CreateDefaultQueryResult <bool>();

            if (!CheckConnection(queryResult))
            {
                return(queryResult);
            }

            try
            {
                queryResult.Result       = (await _currentModbusMaster.ReadCoilsAsync(_slaveId, coilAddress, 1))[0];
                queryResult.IsSuccessful = true;
            }
            catch (Exception e)
            {
                queryResult.IsSuccessful = false;
            }
            return(queryResult);
        }
Esempio n. 3
0
        public async Task TestCoil()
        {
            var           factory = new ModbusFactory();
            IModbusMaster modbus  = factory.CreateMaster(_client);
            await _client.ConnectAsync(MODBUS_IP, MODBUS_PORT);

            bool[] data = await modbus.ReadCoilsAsync(MODBUS_SLAVE, 0, 1);

            Assert.Single(data);
            await modbus.WriteSingleCoilAsync(MODBUS_SLAVE, 0, true);

            data = await modbus.ReadCoilsAsync(MODBUS_SLAVE, 0, 1);

            Assert.Single(data);
            Assert.True(data[0]);
            await modbus.WriteSingleCoilAsync(MODBUS_SLAVE, 0, false);

            data = await modbus.ReadCoilsAsync(MODBUS_SLAVE, 0, 1);

            Assert.Single(data);
            Assert.False(data[0]);
        }
Esempio n. 4
0
        public async Task TestCoils()
        {
            var           factory = new ModbusFactory();
            IModbusMaster modbus  = factory.CreateMaster(_client);
            await _client.ConnectAsync(MODBUS_IP, MODBUS_PORT);

            bool[] data = await modbus.ReadCoilsAsync(MODBUS_SLAVE, 0, 5);

            Assert.Equal(5, data.Length);
            await modbus.WriteMultipleCoilsAsync(MODBUS_SLAVE, 0, new bool[] { true, true, true, true, true });

            data = await modbus.ReadCoilsAsync(MODBUS_SLAVE, 0, 5);

            Assert.Equal(5, data.Length);
            Assert.Equal(new bool[] { true, true, true, true, true }, data);
            await modbus.WriteMultipleCoilsAsync(MODBUS_SLAVE, 0, new bool[] { false, false, false, false, false });

            data = await modbus.ReadCoilsAsync(MODBUS_SLAVE, 0, 5);

            Assert.Equal(5, data.Length);
            Assert.Equal(new bool[] { false, false, false, false, false }, data);
        }
Esempio n. 5
0
 public Task <bool[]> ReadCoilsAsync(byte slaveAddress, ushort startAddress, ushort number,
                                     CancellationToken cancellationToken)
 {
     return(PerformFuncAsync(() => _master.ReadCoilsAsync(slaveAddress, startAddress, number), cancellationToken));
 }
Esempio n. 6
0
 public async Task <bool[]> ReadCoils(ushort startAddress, ushort pointCount)
 {
     return(await m_modbusConnection.ReadCoilsAsync(m_unitID, startAddress, pointCount));
 }