Exemplo n.º 1
0
        public static async Task SendDataByAddressAsync(byte numOfDevice, ushort address, ushort[] value)
        {
            if (_modbusMaster != null)
            {
                //OnWritingStartedAction?.Invoke();
                try
                {
                    await _semaphoreSlim.WaitAsync();

                    OnWritingStartedAction?.Invoke();
                    await _modbusMaster.WriteMultipleRegistersAsync(numOfDevice, address, value);

                    _semaphoreSlim.Release(1);
                }
                catch (Exception e)
                {
                    _semaphoreSlim.Release(1);
                    ConnectionLostAction?.Invoke();
                    throw;
                }
                finally
                {
                    //_semaphoreSlim.Release();
                    OnWritingCompleteAction?.Invoke();
                }
            }
            else
            {
                throw new Exception("Не инициализирован обЪект связи");
            }
        }
Exemplo n.º 2
0
        public static async Task ExecuteFunction15Async(byte numOfDevice, ushort address, bool[] data)
        {
            if (_modbusMaster != null)
            {
                await _semaphoreSlim.WaitAsync();

                OnWritingStartedAction?.Invoke();
                try
                {
                    OnWritingStartedAction?.Invoke();
                    await _modbusMaster.WriteMultipleCoilsAsync(numOfDevice, address, data);

                    _semaphoreSlim.Release(1);
                }
                catch (Exception e)
                {
                    _semaphoreSlim.Release(1);
                    ConnectionLostAction?.Invoke();
                    throw;
                }
                finally
                {
                    //_semaphoreSlim.Release();
                    OnWritingCompleteAction?.Invoke();
                }
            }
            else
            {
                throw new Exception("Не инициализирован обЪект связи");
            }
        }
Exemplo n.º 3
0
        internal static async Task <ushort[]> GetDataByAddress(byte numOfDevice, ushort address, ushort value,
                                                               bool isQueryCritical = true)
        {
            if (_modbusMaster != null)
            {
                await _semaphoreSlim.WaitAsync();

                OnWritingStartedAction?.Invoke();
                try
                {
                    ushort[] result;
                    result = await _modbusMaster.ReadHoldingRegistersAsync(numOfDevice, address, value);

                    _semaphoreSlim.Release(1);
                    return(result);
                }
                catch (Exception e)
                {
                    _semaphoreSlim.Release(1);
                    if (isQueryCritical)
                    {
                        ConnectionLostAction?.Invoke();
                    }
                    throw e;
                }
                finally
                {
                    //_semaphoreSlim.Release();
                    OnWritingCompleteAction?.Invoke();
                }
            }
            else
            {
                throw new Exception("Не инициализирован объект связи");
            }
        }