Example #1
0
        private async Task TryConnect(EasyModbus.ModbusClient client, CancellationToken token)
        {
            int cnt = 1;

            while (client.Connected == false)
            {
                try
                {
                    client.Connect();
                    //client.Disconnect();
                    if (client.Connected)
                    {
                        string connectingInfo = string.IsNullOrEmpty(client.IPAddress) ? $"RTU ({client.SerialPort})" : $"TCP ({client.IPAddress}:{client.Port})";
                        this.logger.Info($"Connecting to Modbus slave at {connectingInfo}");
                    }
                    else
                    {
                        throw new TimeoutException("Connect error");
                    }
                    //await Task.Delay(TimeSpan.FromSeconds(5), token);
                }
                catch (Exception ex)
                {
                    this.logger.Error(ex, ex.Message);
                    this.logger.Info($"Attempting connection to Modbus Slave...Attempt #{cnt++}");
                    await Task.Delay(TimeSpan.FromSeconds(5), token);
                }
            }
        }
        public void GetValues(ConnectionProperties connectionProperties, int functionPropertyID)
        {
            modbusClient = connectionProperties.modbusClient;
            if (!modbusClient.Connected)
            {
                modbusClient.IPAddress = connectionProperties.ModbusTCPAddress;
                modbusClient.Port      = connectionProperties.Port;
                modbusClient.Connect();
            }

            switch (connectionProperties.FunctionPropertiesList[functionPropertyID].FunctionCodeRead)
            {
            case FunctionCodeRd.ReadCoils:
                connectionProperties.FunctionPropertiesList[functionPropertyID].values = modbusClient.ReadCoils(connectionProperties.FunctionPropertiesList[functionPropertyID].StartingAdress, connectionProperties.FunctionPropertiesList[functionPropertyID].Quantity);
                break;

            case FunctionCodeRd.ReadDiscreteInputs:
                connectionProperties.FunctionPropertiesList[functionPropertyID].values = modbusClient.ReadDiscreteInputs(connectionProperties.FunctionPropertiesList[functionPropertyID].StartingAdress, connectionProperties.FunctionPropertiesList[functionPropertyID].Quantity);
                break;

            case FunctionCodeRd.ReadHoldingRegisters:
                connectionProperties.FunctionPropertiesList[functionPropertyID].values = modbusClient.ReadHoldingRegisters(connectionProperties.FunctionPropertiesList[functionPropertyID].StartingAdress, connectionProperties.FunctionPropertiesList[functionPropertyID].Quantity);
                break;

            case FunctionCodeRd.ReadInputRegisters:
                connectionProperties.FunctionPropertiesList[functionPropertyID].values = modbusClient.ReadInputRegisters(connectionProperties.FunctionPropertiesList[functionPropertyID].StartingAdress, connectionProperties.FunctionPropertiesList[functionPropertyID].Quantity);
                break;

            default: break;
            }
            if (valuesChanged != null)
            {
                valuesChanged(this);
            }
        }
Example #3
0
 private void mountModbusMaster()
 {
     modbusClient = new ModbusClient();
     modbusClient.ReceiveDataChanged += new EasyModbus.ModbusClient.ReceiveDataChangedHandler(UpdateReceiveData);
     modbusClient.SendDataChanged    += new EasyModbus.ModbusClient.SendDataChangedHandler(UpdateSendData);
     modbusClient.ConnectedChanged   += new EasyModbus.ModbusClient.ConnectedChangedHandler(UpdateConnectedChanged);
 }
Example #4
0
 public MainForm()
 {
     InitializeComponent();
     modbusClient = new EasyModbus.ModbusClient();
     modbusClient.ReceiveDataChanged += new EasyModbus.ModbusClient.ReceiveDataChangedHandler(UpdateReceiveData);
     modbusClient.SendDataChanged    += new EasyModbus.ModbusClient.SendDataChangedHandler(UpdateSendData);
     modbusClient.ConnectedChanged   += new EasyModbus.ModbusClient.ConnectedChangedHandler(UpdateConnectedChanged);
 }
Example #5
0
 public EasyModbusWrapper(string IPAddress, int PortValue)
 {
     ModbusClient = new EasyModbus.ModbusClient();
     //ModbusClient.SerialPort = "";
     ModbusClient.receiveDataChanged += new EasyModbus.ModbusClient.ReceiveDataChanged(UpdateReceiveData);
     ModbusClient.sendDataChanged    += new EasyModbus.ModbusClient.SendDataChanged(UpdateSendData);
     ModbusClient.connectedChanged   += new EasyModbus.ModbusClient.ConnectedChanged(UpdateConnectedChanged);
     ModbusClient.LogFileFilename     = "AutoTronEasyModbusWrapper.txt";
     ModbusClient.IPAddress           = IPAddress;
     ModbusClient.Port = PortValue;
 }
Example #6
0
        //protected override void OnInitialize()
        //{
        //    base.OnInitialize();
        //    InitializeModbus();
        //}

        private void InitializeModbus(ref EasyModbus.ModbusClient Client, ModbusConfig modbusConfig)
        {
            if (string.IsNullOrEmpty(modbusConfig.SerialPort))
            {
                Client = new EasyModbus.ModbusClient(modbusConfig.IpAddress, modbusConfig.Port);
            }
            else
            {
                Client          = new EasyModbus.ModbusClient(modbusConfig.SerialPort);
                Client.Baudrate = modbusConfig.BaudRate;
                Client.StopBits = modbusConfig.StopBits;
                Client.Parity   = modbusConfig.Parity;
            }
        }