Example #1
0
        /// <summary>
        /// Connects the client to the device.
        /// </summary>
        /// <returns>An awaitable task.</returns>
        public Task Connect()
        {
            logger?.LogTrace("ModbusClient.Connect");
            if (isDisposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (isStarted)
            {
                return(ConnectingTask);
            }

            isStarted = true;
            logger?.LogInformation("ModbusClient starting");

            if (DriverEnableRS485 && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                try
                {
                    var rs485 = GetDriverState();
                    serialDriverFlags = rs485.Flags;
                    rs485.Flags      |= RS485Flags.Enabled;
                    rs485.Flags      &= ~RS485Flags.RxDuringTx;
                    SetDriverState(rs485);
                    driverModified = true;
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "ModbusClient.Connect faild to set RS485 serial driver state.");
                    throw;
                }
            }

            wasConnected = false;
            mainCts      = new CancellationTokenSource();

            Task.Run(() => Reconnect(mainCts.Token));
            ConnectingTask = GetWaitTask(mainCts.Token);

            return(ConnectingTask);
        }
Example #2
0
        /// <summary>
        /// Connects the client to the device.
        /// </summary>
        /// <returns>An awaitable task.</returns>
        public Task Connect()
        {
            logger?.LogTrace("ModbusClient.Connect");
            if (isDisposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (!isStarted)
            {
                logger?.LogInformation("ModbusClient starting");
                isStarted = true;

                if (DriverEnableRS485 && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    try
                    {
                        var rs485 = GetDriverState();
                        serialDriverFlags = rs485.Flags;
                        rs485.Flags      |= RS485Flags.Enabled;
                        rs485.Flags      &= ~RS485Flags.RxDuringTx;
                        SetDriverState(rs485);
                        driverModified = true;
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, "ModbusClient.Connect faild to set RS485 serial driver state.");
                        throw;
                    }
                }

                ConnectingTask = Task.Run((Action)Reconnect);
            }

            return(ConnectingTask);
        }
Example #3
0
 static extern _LTRNative.LTRERROR LTR43_RS485_ExchangeEx(ref TLTR43 hnd, short[] PackToSend,
                                                          short[] ReceivedPack, int OutPackSize, int InPackSize,
                                                          RS485Flags flags, out int ReceivedSize);
Example #4
0
 public virtual _LTRNative.LTRERROR RS485_ExchangeEx(short[] PackToSend, short[] ReceivedPack, int OutPackSize, int InPackSize,
                                                     RS485Flags flags, out int ReceivedSize)
 {
     return(LTR43_RS485_ExchangeEx(ref module, PackToSend, ReceivedPack, OutPackSize, InPackSize,
                                   flags, out ReceivedSize));
 }